Yaron Levi Yaron Levi - 2 years ago 83
iOS Question

iOS 8 Remote notifications - When should I call registerForRemoteNotifications()?

As the apple's official info page about push notification states:

"Device tokens can change, so your app needs to reregister every time it is launched."

I trying to understand what they meant by "every time it is launched".

Does it mean I have to call it in the AppDelegate, in didFinishLaunchingWithOptions() like so:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

return true

Putting this code here will cause it to execute every time the user opens the app, which could be many times a minute if the user is multi tasking between apps (going back and forth between them).

And since calling the registration method invokes an HTTP request to APNS, there is a risk of getting temporary ban.

Are those observations are correct, or I can put the register method like so without any fear?

(I am using xcode 6.2 with Swift)

Answer Source

didFinishLaunchingWithOptions is not called every time the user switches to your app, as often your app is still running. What you're describing sounds more like applicationDidBecomeActive.

Add some NSLogs to both methods to convince yourself that didFinishLaunchingWithOptions is the right place to call .registerForRemoteNotifications.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download