简体   繁体   English

如何注册推送通知? ios10

[英]How to registre for push notification ? ios10

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 ? 哪种方法是正确的?

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. 如果您对UNUserNotificationCenter requestAuthorization以外的其他方法开放态度,那么这肯定会解决您的问题,也就是为iOS 9和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)")
}

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. Apple将远程和本地通知的接收结合到applicationDidFinishLaunching中,因此如果您处理它们的代码相似,则重复代码将会减少。 You can watch this video from Apple and learn about the changes. 您可以在Apple观看此视频并了解更改。

But take note that if your app supports versions before iOS 10.0, some of these new methods might not work. 但请注意,如果您的应用支持iOS 10.0之前的版本,则其中一些新方法可能无效。 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. 在这种情况下,确保您的应用仍然可以处理旧方法 - 或者只是使用旧方法,直到您的应用在iOS 10.0及更高版本上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM