简体   繁体   English

didRegisterForRemoteNotificationsWithDeviceToken 未在 ios 10 上调用

[英]didRegisterForRemoteNotificationsWithDeviceToken not getting called on ios 10

I've written this code in appDelegate我已经在 appDelegate 中编写了这段代码

import UserNotifications
var registerId = String()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.registerForPushNotifications(application)
    return true   
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""
    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }
    registerId = tokenString
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}
func registerForPushNotifications(application: UIApplication) {

    if #available(iOS 10.0, *){
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in
            if (granted)
            {
               application.registerForRemoteNotifications()
            }
            else{
                registerId = "111111"
                //Do stuff if unsuccessful...
            }
        })
    }

    else{
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)

    }

}
}

With this code I'm able to get the device token in ios version < 10 , but for ios 10 and higher the didRegisterForRemoteNotificationsWithDeviceToken is not getting called.使用此代码,我可以在 ios 版本 < 10 中获取设备令牌,但对于 ios 10 及更高版本,不会调用 didRegisterForRemoteNotificationsWithDeviceToken。 Any help appreciated任何帮助表示赞赏

One last thing, add framework:最后一件事,添加框架:

import UserNotifications

class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate {

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
    registerForPushNotifications(application)
...
}


    func registerForPushNotifications(application: UIApplication) {

    if #available(iOS 10.0, *){
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
            if (granted)
            {
                UIApplication.shared.registerForRemoteNotifications()
            }
            else{
                //Do stuff if unsuccessful...
            }
        })
    }
    else { //If user is not on iOS 10 use the old methods we've been using
        let notificationSettings = UIUserNotificationSettings(
            types: [.badge, .sound, .alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)

    }

}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

 }
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    //Handle the notification
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    //Handle the notification
}

}

Apart from all these you have to enable your push notifications from capabilities.除了所有这些,您还必须启用来自功能的推送通知。

Click on your project target you will see the screen and on capabilities enable push notifications.单击您的项目目标,您将看到屏幕和启用推送通知的功能。

在此处输入图片说明

Use this for ios 10 -将此用于 ios 10 -

func registerForPushNotifications(_ application: UIApplication) {
      let notificationSettings = UIUserNotificationSettings(
                types: [.badge, .sound, .alert], categories: nil)
            application.registerUserNotificationSettings(notificationSettings)
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
      if notificationSettings.types != UIUserNotificationType() {
                application.registerForRemoteNotifications()
      }
}
func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) {

       var token: String = ""
       for i in 0..<deviceToken.count {
           token += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
       }

       UserDefaults.standard.setValue(token, forKey: "kDeviceToken")
       UserDefaults.standard.synchronize()
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {}

PS:- Create proper certificate and bundle id also .Follow this tutorial - https://www.raywenderlich.com/123862/push-notifications-tutorial PS:- 创建适当的证书和捆绑 ID 也。按照本教程 - https://www.raywenderlich.com/123862/push-notifications-tutorial

Use //Register for push Notifications使用 //Register 推送通知

registerForPushNotifications(application)

in didFinishLaunchingWithOptions methoddidFinishLaunchingWithOptions方法中

暂无
暂无

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

相关问题 在ios 9中没有调用didRegisterForRemoteNotificationsWithDeviceToken - didRegisterForRemoteNotificationsWithDeviceToken is not called up in ios 9 iOS 11 didRegisterForRemoteNotificationsWithDeviceToken未调用 - IOS 11 didRegisterForRemoteNotificationsWithDeviceToken not called 在iOS7上没有调用didRegisterForRemoteNotificationsWithDeviceToken - didRegisterForRemoteNotificationsWithDeviceToken not called on iOS7 应用程序:didRegisterForRemoteNotificationsWithDeviceToken:未称为ios 10.3.2 - application:didRegisterForRemoteNotificationsWithDeviceToken: not called ios 10.3.2 在iOS 8中未调出didRegisterForRemoteNotificationsWithDeviceToken - didRegisterForRemoteNotificationsWithDeviceToken is not called up in ios8 IOS 13 APNS didRegisterForRemoteNotificationsWithDeviceToken 未调用 - IOS 13 APNS didRegisterForRemoteNotificationsWithDeviceToken not called 没有使用简单的 iOS 应用程序调用 didRegisterForRemoteNotificationsWithDeviceToken - didRegisterForRemoteNotificationsWithDeviceToken not called with simple iOS app didRegisterForRemoteNotificationsWithDeviceToken 没有被调用 Swift 5,Xcode 10.2 - didRegisterForRemoteNotificationsWithDeviceToken is not getting called Swift 5, Xcode 10.2 iOS 13.x didRegisterForRemoteNotificationsWithDeviceToken 从未被调用 - iOS 13.x didRegisterForRemoteNotificationsWithDeviceToken is never called 没有为 iOS 13 调用 didRegisterForRemoteNotificationsWithDeviceToken(目标 C) - didRegisterForRemoteNotificationsWithDeviceToken not called for iOS 13 (Objective C)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM