Swift ios check if remote push notifications are enabled in ios9 and ios10

后端 未结 11 1295
悲哀的现实
悲哀的现实 2020-12-01 02:26

How can I check if the user has enabled remote notifications on ios 9 or ios 10?

If the user has not allowed or clicked No I want to toggle a message asking if they

11条回答
  •  一向
    一向 (楼主)
    2020-12-01 03:03

    Here's a solution for getting a string describing the current permission that works with iOS 9 trough iOS 11, with Swift 4. This implementation uses When for promises.

    import UserNotifications
    
    private static func getNotificationPermissionString() -> Promise {
        let promise = Promise()
    
        if #available(iOS 10.0, *) {
            let notificationCenter = UNUserNotificationCenter.current()
            notificationCenter.getNotificationSettings { (settings) in
                switch settings.authorizationStatus {
                case .notDetermined: promise.resolve("not_determined")
                case .denied: promise.resolve("denied")
                case .authorized: promise.resolve("authorized")
                }
            }
        } else {
            let status = UIApplication.shared.isRegisteredForRemoteNotifications ? "authorized" : "not_determined"
            promise.resolve(status)
        }
    
        return promise
    }
    

提交回复
热议问题