0

从 OS 11.2.5 开始,我的设备无法注册远程通知(例如,出于静默推送的目的。我在以下代码行中实现了注册过程:

// Ask for notification permission
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {(accepted, error) in
    if !accepted {
        print("Notification access denied.")
    }
}
application.registerForRemoteNotifications()

此外,如您所知,您需要实现以下两种方法,以便在 Apple 注册远程通知:

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

    let tokenParts = deviceToken.map { data -> String in
        return String(format: "%02.2hhx", data)
    }
    let token = tokenParts.joined()
    // Get my token here and do additionally stuff
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // Handling error for registering here
}

所以我的问题如下:此实现一直有效,直到 Apple OS 更新 11.2.4:didRegisterForRemoteNotificationsWithDeviceToken注册设备后成功调用,如果出现错误,didFailToRegisterForRemoteNotificationsWithError则调用其他方法 -> 一切都很完美!

但从 OS 11.2.5 开始,我再也没有得到 Apple 的回应。我花了很多时间研究这个问题。在 Apple 发布 OS 11.2.6 之后,它又像魅力一样工作了 -> 我完全糊涂了。

有人知道这是否是 OS 11.2.5 中的一个已知问题?- 谢谢亚历克斯

4

2 回答 2

0

我猜是注册远程通知时引起的问题,请尝试以下代码:

// Ask for notification permission
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {(accepted, error) in
    if accepted {
        DispatchQueue.main.async {
             UIApplication.shared.registerForRemoteNotifications()
        }
    }else{
        print("Notification access denied.")
    }
}
于 2018-03-01T12:27:17.877 回答
0
use updated methods.


// Push Notifications
    func registerForPushnotifications(application: UIApplication)
    {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
                guard granted else{ return }
                self.getNotificationSetting()
            }
        }
        else
        {
            // Fallback on earlier versions
            let notificationSettings = UIUserNotificationSettings(
                types: [.badge, .sound, .alert], categories: nil)
            application.registerUserNotificationSettings(notificationSettings)
        }
    }
    // Push Notification settings
    func getNotificationSetting()
    {
        if #available(iOS 10.0, *)
        {
            UNUserNotificationCenter.current().getNotificationSettings { (settings) in
                guard settings.authorizationStatus == .authorized else {return}
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
        }
    }
    // Push Notifications Delegates

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
    {
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
    {
        print("Failed to register for remote Notifications due to: \(error.localizedDescription)")
    }
于 2018-03-01T13:08:24.810 回答