detect “Allow Notifications” is on/off for iOS8

后端 未结 11 1385
我寻月下人不归
我寻月下人不归 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:43

    check this out. code is tried and tested.

    - (BOOL)isUserNotificationAllowed {
        UIUserNotificationType types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
        if(types & UIUserNotificationTypeBadge || types & UIUserNotificationTypeSound || types & UIUserNotificationTypeAlert){
            return YES;
        }
        else {
            return NO;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 06:44

    You can control the hash value of UIApplication.sharedApplication().currentUserNotificationSettings().

    if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))){
            if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0){
                pushNotificationStatus = "false"
            } else {
                pushNotificationStatus = "true"
            }
    }
    
    0 讨论(0)
  • 2020-12-01 06:48

    Swift 3+

    let isRegisteredForLocalNotifications = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert) ?? false
    

    Swift 2.3

    let isRegisteredForLocalNotifications = UIApplication.sharedApplication().currentUserNotificationSettings()?.types.contains(UIUserNotificationType.Alert) ?? false
    
    0 讨论(0)
  • 2020-12-01 06:48
    UIUserNotificationType notificationType = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
    
    if(notificationType == UIRemoteNotificationTypeNone)
            {
    
                NSLog(@"OFF");
            }
            else{
    
                NSLog(@"ON");
            }
    

    works for me

    0 讨论(0)
  • 2020-12-01 06:50

    Method enabledRemoteNotificationTypes is deprecated since iOS8.

    To check remote notifications status in iOS8 you can call

    [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    

    it will return NO if user disable notifications in Settings. Documentation on isRegisteredForRemoteNotifications

    Or you can retrieve all current notification settings:

    [[UIApplication sharedApplication] currentUserNotificationSettings];
    

    Documentation on currentUserNotificationSettings

    0 讨论(0)
提交回复
热议问题