detect “Allow Notifications” is on/off for iOS8

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

    This should work the very first time your app is launched user will get a dialog, to check if user denies or allows notifications, use:

    -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:   (UIUserNotificationSettings *)notificationSettings
    {
        if (notificationSettings.types) {
            NSLog(@"user allowed notifications");
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }else{
            NSLog(@"user did not allow notifications");
            // show alert here
        }
    }
    

    On consecutive launches, use:

    [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    
    0 讨论(0)
  • 2020-12-01 06:38

    Swift 2

    In AppDelegate:

    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    
        if (notificationSettings.types == .None){ // User did NOT allowed notifications
    
    
        }else{ // User did allowed notifications
    
    
        }
    
    }
    

    From any other ViewController:

        if UIApplication.sharedApplication().currentUserNotificationSettings()!.types.contains(.None){
    
        }else{
    
        }
    
    0 讨论(0)
  • 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.

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

    To check the remote notifications status in iOS8 and iOS9 you call:

    // Obj-C
    [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; 
    // Swift
    UIApplication.sharedApplication().isRegisteredForRemoteNotifications
    

    It returns true if your app can receive remote notifications. However receiving remote notifications does not mean it will also display them to the user.

    The request registerForRemoteNotifications() succeeds in almost every case and the AppDelegate's didRegisterForRemoteNotificationsWithDeviceToken is called which gives you the device token. The app can then receive silent remote notifications that are not displayed to the user. You can for example trigger background processes in the app when such a notification is received. See Documentation.

    To not only receive but also display remote notifications to the user you request permission with:

    // Swift
    let notificatonSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(notificatonSettings)
    

    This will display a dialog to the user where they can allow or deny your request. Indifferent of their decision the app will still be able to receive remote notifications.

    If the users allows, AppDelegate's didRegisterForRemoteNotificationsWithDeviceToken is called.

    To check whether the user allowed or denied your request or in fact changed the notifications permissions later in iOS setting you call:

    // Obj-C
    [[UIApplication sharedApplication] currentUserNotificationSettings];
    // Swift
    UIApplication.sharedApplication().currentUserNotificationSettings()   
    

    See Documentation.

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

    I apologize for this answer/comment if it is in the wrong place. I am REALLY new at iOS programming and have never posted to stack overflow before. I think this should actually be a comment, but without a 50 rating I am not allowed. I also apologize if the explanation is somewhat rudimentary, but again, kind of new :).

    I also wished to test for whether a user has changed what notifications are allowed/required by my app after the initial request. After trying to decipher the Apple documentation (the writers at Apple are either much smarter than I am, or the documentation is purposely obscure) and doing a bit of testing I tested for the value of

    [[UIApplication sharedApplication] currentUserNotificationSettings].hash.
    

    I believe that this returns a three bit hash value where bit one is for Banner notifications, bit 2 is for Sound notifications, and bit 3 is for Alert notifications.

    So...

    000 = 0 = no notifications.
    001 = 1 = only banner,
    010 = 2 = only sound,
    011 = 3 = sound and banner, no alert
    100 = 4 = only alert notifications
    and so on until,
    111 = 7 = all notifications on.
    

    This also shows 0 if Allow Notifications is turned off in the Settings app. Hope this helps.

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

    You can check that user set enable or disable Push Notifications using following code.

    Enable or Disable Iphone Push Notifications

    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types == UIRemoteNotificationTypeNone) 
      // Yes it is..
    

    Hope, this will help you..

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