简体   繁体   中英

Remote Notifications in iOS 10

So we all know that Apple has introduced the UserNotifications Framework this summer and it's great. And according to Apple's API references and a WWDC Session about this framework, we now have a new way to register for push notification as follows:

 UNUserNotificationCenter.current().requestAuthorization(options: [options], completionHandler: {})

But clearly this method is not gonna get a deviceToken from the APNs server so Apple has kept some of the API in the UIKit as follows:

UIApplication.shared.registerForRemoteNotifications()
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){}

That's the two method for registering and getting the deviceToken for Remote Notifications.

So it's getting ambiguous that if I only want to register for Remote Notifications will I still have to do the requestAuthorization thing? Or is the requestAuthorization just for Registering Local Notifications?

Plus if I need to implement both of them even if I only want to register for Remote Notifications, where should I write the registerForRemoteNotifications() ? Should I write it in the completionHandler in requestAuthorization or should I just write it outside as if this two request and register thing are running in parallel?

If you only want to register for remote notifications, you still need to requestAuthorization , because this is how you ask the user if you may display alerts and sounds, which applies to both local and remote notifications.

You can call registerForRemoteNotifications outside of your requestAuthorization completion handler and allow them to run in parallel.

Update:

registerForRemoteNotifications gets you the user's token whether or not they've approved push notifications.

How is this useful if the user declined?

At any time in the future (after your app has called requestAuthorization to indicate that it would like to send notifications), the user can go into the Settings app, tap your app, and turn on the Allow Notifications setting.

If you are already sending notifications using the user's token, those notifications will begin coming through when the user turns on permission, without any action needed from you.

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