且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift ios 检查是否在 ios9 和 ios10 中启用了远程推送通知

更新时间:2022-04-23 01:34:00

iOS 10 使用 UNUserNotificationCenter 后更新的答案.

Updated answer after iOS 10 is using UNUserNotificationCenter .

首先你需要import UserNotifications 然后

let current = UNUserNotificationCenter.current()
        current.getNotificationSettings(completionHandler: { permission in
            switch permission.authorizationStatus  {
            case .authorized:
                print("User granted permission for notification")
            case .denied:
                print("User denied notification permission")
            case .notDetermined:
                print("Notification permission haven't been asked yet")
            case .provisional:
                // @available(iOS 12.0, *)
                print("The application is authorized to post non-interruptive user notifications.")
            case .ephemeral:
                // @available(iOS 14.0, *)
                print("The application is temporarily authorized to post notifications. Only available to app clips.")
            @unknown default:
                print("Unknow Status")
            }
        })


这段代码会一直工作到 iOS 9,对于 iOS 10,请使用上面的代码片段.


this code will work till iOS 9, for iOS 10 use the above code snippet.

let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
     // User is registered for notification
} else {
     // Show alert user is not registered for notification
}