首次加载应用程序时,即使用户在对话框上点击“允许”,推送通知的请求授权也始终返回false。
这是didFinishLaunchingWithOptions中调用的寄存器函数。在下一次发射时是正确的。

func registerForPushNotifications() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            print("Permission granted: \(granted)")
            guard granted else{return}
            self.getNotificationSettings()
        }
}

最佳答案

在didfinishLaunchingWithOptions中注册远程通知,并确保注册了远程通知。

application.registerForRemoteNotifications()

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

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        guard granted else{return}

        self.getNotificationSettings()

    }
    application.registerForRemoteNotifications()
    return true
}

关于ios - 远程通知的请求授权始终在首次启动时返回false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48277849/

10-09 00:08