Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any notification on didRegisterForRemoteNotificationsWithDeviceToken?

Does iOS trigger a NSNotification (which contains the deviceToken) when didRegisterForRemoteNotificationsWithDeviceToken is called?

I know I can always save the token in NSUserDefaults from within the AppDelegate and access it anywhere, but I am wondering if there is a better way. I'm thinking something along the lines of UIApplicationDidEnterBackgroundNotification.

Update: The use case if I wanted to get the device token without implementing that delegate method in the AppDelegate.

like image 582
pshah Avatar asked Oct 03 '22 14:10

pshah


2 Answers

No. You will need to implement didRegisterForRemoteNotificationsWithDeviceToken . I would imagine one argument against a NSNotification in this situation is that you need to intelligently handle the failure scenarios with didFailToRegisterForRemoteNotificationsWithError.

like image 55
Peter Cetinski Avatar answered Oct 17 '22 17:10

Peter Cetinski


Could not understand your question properly.

 - (void)application:(UIApplication *)application  
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken 

This is a method called by the iOS stack (i.e. your application delegate) when the app successfully registers with APNS server thereby obtaining the device token.

(This assumes that you have had your app registered for push notifications by implementing)

(void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types.

In general, one needs to subsequently send this device token to his Push Notification Payload Generating server to register his device with the server. This can be done inside "didRegisterForRemoteNotificationsWithDeviceToken". Hence I do not really see a need to store the token anywhere. Also, please note that the app registers and obtains the device token from APNS server every time the app launches (not during coming from background to foreground generally- but it depends on your implementation).

like image 38
Subzero Avatar answered Oct 17 '22 19:10

Subzero