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

后端 未结 11 1317
悲哀的现实
悲哀的现实 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:23

    Even though user doesn't allow the push notifications, the device token is available. So it would be also a good idea to check if it's allowed to receive the push notifications.

    private func checkPushNotificationAllowed(completionHandler: @escaping (Bool) -> Void) {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().getNotificationSettings { (settings) in
                if settings.authorizationStatus == .notDetermined || settings.authorizationStatus == .denied {
                    completionHandler(false)
                }
                else {
                    completionHandler(true)
                }
            }
        }
        else {
            if let settings = UIApplication.shared.currentUserNotificationSettings {
                if settings.types.isEmpty {
                    completionHandler(false)
                }
                else {
                    completionHandler(true)
                }
            }
            else {
                completionHandler(false)
            }
        }
    }
    

提交回复
热议问题