简体   繁体   中英

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 {

    application.registerForRemoteNotifications()
    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)

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 NSLog s to both methods to convince yourself that didFinishLaunchingWithOptions is the right place to call .registerForRemoteNotifications .

Answering to your exact question:- you can put the register method in didFinishLaunchingWithOptions without any fear. It is recommended by apple itself.

Case 1:- If in your plist "Application does not run in background" is set to NO, didFinishLaunchingWithOptions will not be called every time the app opens. It will be called if the app launches completely from a terminated state. Only applicationDidBecomeActive will be called whenever the app launches from background state. No need to put register method in applicationDidBecomeActive.

Case 2:- And if in your plist "Application does not run in background" is set to YES, didFinishLaunchingWithOptions will be called every time the app opens. you can put the register method in didFinishLaunchingWithOptions without any fear.

According to the Apple Documentation for registerForRemoteNotifications() ,

After successful APNs registration, the app object contacts APNs only when the device token has changed; otherwise, calling the registerForRemoteNotifications method results in a call to the application:didRegisterForRemoteNotificationsWithDeviceToken: method which returns the existing token quickly.

Calling the registration method does not invoke an HTTP request to APNS if it is not necessary.

This StackOverflow Comment confirms this empirically:

You can test this by putting your phone into airplane mode before starting your app. If I recall correctly, you'll still get a response with a token, because at some point in the past the device will have negotiated with Apple's server what its token is, and that token doesn't change just because you're currently offline.

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