无法调用didRegisterForRemoteNotificationsWithDeviceToken或didFailToRegisterForRemoteNotificationsWithError

时间:2018-01-10 00:51:17

标签: swift swift3 push-notification devicetoken

我的应用程序无法在iOS10设备上调用didRegisterForRemoteNotificationsWithDeviceToken,即使相同的代码也可以在iOS11设备上调用它

我不知道为什么会这样,以及如何解决这个问题。

如果有人知道答案,请帮助我。

我的环境如下: Xcode:9.2 斯威夫特:3.2 部署目标:10.0

我的代码如下:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let center = UNUserNotificationCenter.current()
    center.delegate = self
    center.requestAuthorization(options: [.alert, .badge, .sound]) {granted, error in
        if error != nil {
            return
        }
        if granted {
            DispatchQueue.main.async(execute: {
                UIApplication.shared.registerForRemoteNotifications()
            })
        }
    }

    return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = String(format: "%@", deviceToken as CVarArg) as String
    let tokenWithoutSpace = token.replacingOccurrences(of: "[ |<>]", with: "", options: .regularExpression)
    print(tokenWithoutSpace)
}

感谢您的帮助。

0 个答案:

没有答案