iOS 11.2.5 - didRegisterForRemoteNotificationWithDeviceToken - 无响应

问题描述 投票:0回答:2

从操作系统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 Update 11.2.4:在注册设备后成功调用了didRegisterForRemoteNotificationsWithDeviceToken,如果出现错误,则调用另一种方法didFailToRegisterForRemoteNotificationsWithError - >一切都很完美!

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

有人知道,如果这是OS 11.2.5中的已知问题吗? - 谢谢Alex

ios swift push-notification remote-notifications silentpush
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.")
    }
}

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)")
    }
© www.soinside.com 2019 - 2024. All rights reserved.