detect “Allow Notifications” is on/off for iOS8

后端 未结 11 1382
我寻月下人不归
我寻月下人不归 2020-12-01 06:32

I am trying to detect the Local notification settings for the App in iOS 8

for UIUserNotificationSettings, it returns me 7 as I have turned on all Badge

11条回答
  •  温柔的废话
    2020-12-01 06:39

    Using .isRegisteredForRemoteNotifications() doesn't work (though it should). However, when the notifications are disabled or one of the type is not present, the following code does work.

    func notificationsAreOk() -> Bool {
        let wishedTypes = UIUserNotificationType.Badge |
            UIUserNotificationType.Alert |
            UIUserNotificationType.Sound;
        let application = UIApplication.sharedApplication()
        let settings = application.currentUserNotificationSettings()
        if settings == nil {
            return false
        }
        if settings.types != wishedTypes {
            return false
        }
        return true
    }
    

    EDIT: after some tests is does not always work when notifications are disabled. I am considering to send a test notification to know when they work.

提交回复
热议问题