1

I think the correct way to register for push notification is to configure the user interactions first then register for push notifications, as bellow

let center = UNUserNotificationCenter.current()
              center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in

                  if granted {

                    // Register with APNs
                    UIApplication.shared.registerForRemoteNotifications()

                  }else{

                      //user did't grant permissino: so we need to send phone ids, as we need to call this function every time the application opened
                      self.sendPhoneIdsToLookitServer()


                  }


              }

but apple shows different way , it doesn't suggest to register for remote notification as callback after configuring user interactions rather it ask to configure user interactions and then register for push notification without waiting for user response, as you can see here

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Configure the user interactions first.
    self.configureUserInteractions()

    NSApplication.shared().registerForRemoteNotifications(matching: [.alert, .sound])
}

which approach is the correct one ?

david
  • 3,310
  • 7
  • 36
  • 59

2 Answers2

3

If you are open to different approach other than UNUserNotificationCenter requestAuthorization then this can surely solve your concern, also its written for iOS 9 and 8.

func registerForNotification(application : UIApplication) {

    if #available(iOS 10.0, *) {
        let setting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(setting)
        UIApplication.shared.registerForRemoteNotifications()
    }
        // iOS 9 support
    else if #available(iOS 9, *) {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
        // iOS 8 support
    else if #available(iOS 8, *) {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("deviceTokenString ======= \(deviceTokenString)")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("didFailToRegisterForRemoteNotificationsWithError \(error)")
}

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
    // Print notification payload data
    print("Push notification received: \(data)")
}
Mr. Bean
  • 4,221
  • 1
  • 19
  • 34
0

It is not about correct for wrong, more like older and newer. Apple combine both receiving of remote and local notification into applicationDidFinishLaunching so if your codes for handling them is similar there will be less duplicate codes. You can watch this video from Apple and learn about the changes.

But take note that if your app supports versions before iOS 10.0, some of these new methods might not work. Make sure your app can still handle the old method in that case - or just use the old method until your app run on iOS 10.0 and later.

Ben Ong
  • 913
  • 1
  • 10
  • 25