简体   繁体   中英

How to get iOS push notification token after app launch

I am writing an iOS app which registers for push notification. I need to send the device token at regular intervals/whenever server requests.

- (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
   //send to server
}

But this is happening only when the app launches. How to get to this callback on some other part of the app apart from launch so that it reaches the above code and sends the deviceToken to my server ?

I am thinking of this ( has to support iOS8 and also lower )

if( server requests token )
{
   if( [application respondsToSelector:@selector(registerUserNotificationSettings) ] )
   {
      [application registerForRemoteNotifications];
   }
   else
   {
      [application registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert );
   }
}

Is this the right way of getting the deviceToken again in the middle of the app ( ie after app launch ) . Please help

When didRegisterForRemoteNotificationsWithDeviceToken is called, you can save the registered token at Application level.

Create a @property at the AppDelegate level and create a function where you register this token on Your server . Then accessing this property in that function, you can recall the function and re register as many times as you want on your server rather than re-registering on the Apple server .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM