سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top