Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 367
Yaron Levi Avatar asked Apr 05 '15 11:04

Yaron Levi


2 Answers

A 2021 update

In the latest apple documentation, there is no such information about when/ where you should call it. I assume they have simplified the API.

From the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) documentation:

Typically, this method is called only after you call the registerForRemoteNotifications() method of UIApplication, but UIKit might call it in other rare circumstances. For example, UIKit calls the method when the user launches an app after having restored a device from data that is not the device’s backup data. In this exceptional case, the app won’t know the new device’s token until the user launches it.

Using a different document (potentially outdated) from Apple, Configuring Remote Notification Support,

If the device token changes while your app is running, the app object calls the application:didRegisterForRemoteNotificationsWithDeviceToken: delegate method again to notify you of the change

So it seems like it's not your responsibility to call it frequently (e.g. at launch, between app switches) anymore. Just call it once when you want need a deviceToken, and your application:didRegisterForRemoteNotificationsWithDeviceToken will be called when it gets renewed. I've tested that I can call the registerForRemoteNotifications outside of didFinishLaunchingWithOptions and willFinishLaunchingWithOptions, it worked fine. It's harder to test that Apple will call application:didRegisterForRemoteNotificationsWithDeviceToken.


Because Apple docs have been changing many times in the past, I'm reproducing the discussion section below from August 2021:

Discussion

Call this method to initiate the registration process with Apple Push Notification service. If registration succeeds, the app calls your app delegate object’s application:didRegisterForRemoteNotificationsWithDeviceToken: method and passes it a device token. You should pass this token along to the server you use to generate remote notifications for the device. If registration fails, the app calls its app delegate’s application:didFailToRegisterForRemoteNotificationsWithError: method instead.

If you want your app’s remote notifications to display alerts, play sounds, or perform other user-facing actions, you must request authorization to do so using the requestAuthorizationWithOptions:completionHandler: method of UNUserNotificationCenter. If you do not request and receive authorization for your app's interactions, the system delivers all remote notifications to your app silently.

like image 115
Ben Butterworth Avatar answered Sep 27 '22 20:09

Ben Butterworth


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.

like image 21
Rhythmic Fistman Avatar answered Sep 27 '22 22:09

Rhythmic Fistman