JosephT
JosephT

Reputation: 865

detect "Allow Notifications" is on/off for iOS8

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, Sound & Alert.

In the setting, I switch off "Allow Notification" , the app still return me 7 for UIUserNotificationSettings (Badge, Sound & Alert on). Is there a way to detect "Allow Notification" on/off?

- (void)application:(UIApplication *)application
    didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{

    NSLog(@"---notificationSettings.types %d" , notificationSettings.types );
    if(notificationSettings.types!=7){
        UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Please turn on Notification"
                                                         message:@"Go to Settings > Notifications > App.\n Switch on Sound, Badge & Alert"
                                                        delegate:self
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles: nil];
        [alert show];
    }
}

Upvotes: 41

Views: 34278

Answers (11)

Irfan Gul
Irfan Gul

Reputation: 1577

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;
    }
}

Upvotes: 0

Adam Smaka
Adam Smaka

Reputation: 6393

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

Upvotes: 14

Marie Amida
Marie Amida

Reputation: 556

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{

    }

Upvotes: 2

Sagar In
Sagar In

Reputation: 601

UIUserNotificationType notificationType = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];

if(notificationType == UIRemoteNotificationTypeNone)
        {

            NSLog(@"OFF");
        }
        else{

            NSLog(@"ON");
        }

works for me

Upvotes: 2

Manuel
Manuel

Reputation: 15042

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.

Upvotes: 11

Ponf
Ponf

Reputation: 1190

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

Upvotes: 22

mebeDB
mebeDB

Reputation: 147

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.

Upvotes: 13

ACengiz
ACengiz

Reputation: 1315

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"
        }
}

Upvotes: 1

AsTeR
AsTeR

Reputation: 7531

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.

Upvotes: 0

Denis Kanygin
Denis Kanygin

Reputation: 1133

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];

Upvotes: 9

MFP
MFP

Reputation: 1141

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..

Upvotes: -5

Related Questions