22

I want to check "Push Notification option" in iOS device, any time if the application is running (or ON from resume mode). I use the following code to check, if the option is OFF:

-(void)PushNotificationServiceChecking
{
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

    if (types == UIRemoteNotificationTypeNone)
    {
        NSString *msg = @"Please press ON to enable Push Notification";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ON", nil];
        alert.tag = 2;
        [alert show];
    }
}

Then i use the following code for going to the "Settings tab >> Notification center", so that user can on it manually :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2)
    {
        if (buttonIndex == 0)
        {
            // this is the cancel button
        }
        else if (buttonIndex == 1)
        {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];
        }
    }

}

But, now the problem that I am facing is, it only appears at the 1st time after launching the application. It works as I want. But after that, if I turn OFF the "Push Notification option" from "settings" it gives me no "Alert Message".

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tulon
  • 4,011
  • 6
  • 36
  • 56

4 Answers4

41

In iOS 8 you can now use:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

And to check how the settings are setup you could use:

[[UIApplication sharedApplication] currentUserNotificationSettings];
thijsai
  • 1,795
  • 1
  • 18
  • 26
  • 4
    For the record: if user disabled notifications or pressed 'don't allow', `isRegisteredForRemoteNotifications` will still be true the next time. – CularBytes Sep 16 '15 at 20:20
  • If user pressed Don't allow, isRegisteredForRemoteNotifications seems to return false. – Mikael May 08 '17 at 09:05
21

If the App once got registered with the registerForRemoteNotification, then you can disable as well as enable . Once you disable and you are about to Re-Regigister with it, then this will enable the registerForRemoteNotification, without Popup for a alert.

Technical Note TN2265: Troubleshooting Push Notifications

The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

If you want to simulate a first-time run of your app, you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by setting the system clock forward a day or more, turning the device off completely, then turning the device back on.

Fore More Info: INFO && Info 2

Edit : For checking with alert enable -

use

 if (types & UIRemoteNotificationTypeAlert){} 

instead of

if (types == UIRemoteNotificationTypeNone){}

Edit : Latest update from the doc for iOS 8 or later, You can check out by :

- (BOOL)isRegisteredForRemoteNotifications
Pang
  • 9,564
  • 146
  • 81
  • 122
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • Yes, it is. But, after once `registerForRemoteNotification`, it couldn't detect the "Push Notification option" is ON or OFF second time. How can i detect that every time, if my application is running. That's the problem. Thanks for commenting. @Kumar Ki – Tulon Dec 04 '13 at 12:09
  • Whenever your app get launches Do the **registerForRemoteNotificationTypes** Then automatically its get enabled – Kumar KL Dec 04 '13 at 12:12
  • What exactly you are doing is that, You are just disable the alerts Not the Disabling with the **RemoteNotification**. So that mean, The app has already registered for it., So **if (types == UIRemoteNotificationTypeNone)** gets false – Kumar KL Dec 04 '13 at 12:17
  • yes, i got it. But i have to check, is the device "Push Notification option" ON or OFF for my particular application or For the whole device? Though my application is registered for "Push Notification". – Tulon Dec 04 '13 at 12:22
  • Check with this **UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; one can use if (types & UIRemoteNotificationTypeAlert){}** – Kumar KL Dec 04 '13 at 12:28
  • Nope, i am trying with `UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; one can use if (types & UIRemoteNotificationTypeAlert){}` The "AlertView" showing every time, if i turn on "Push Notification" And also try with `UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; one can use if (types == UIRemoteNotificationTypeAlert){}` But it behaves the same. Thanks for your all effort dear @Kumar KI. This is really full of information which is really appreciable. :) – Tulon Dec 04 '13 at 12:41
8

It's work for me. Hope this help! :D

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)


if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
    UIUserNotificationType type = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
    if (type == UIUserNotificationTypeNone){
        ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire);
    }
}
else {
   UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
   if (types == UIRemoteNotificationTypeNone) {
       ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire);
   }
}
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
777Q
  • 375
  • 3
  • 5
  • i think some comments might make it easier to understand than `ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire);` How about `//Push Notifications Are Disabled` – Daniel Galasko Nov 02 '15 at 09:12
4
NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
float versionVal = [prefix floatValue];

if (versionVal >= 8)
{
    if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone)
    {
        NSLog(@" Push Notification ON");
    }
    else
    {
        NSString *msg = @"Please press ON to enable Push Notification";
        UIAlertView *alert_push = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Setting", nil];
        alert_push.tag = 2;
        [alert_push show];

        NSLog(@" Push Notification OFF");
    }
}
else
{
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types != UIRemoteNotificationTypeNone)
    {
        NSLog(@" Push Notification ON");
    }
    else
    {
        NSString *msg = @"Please press ON to enable Push Notification";
        UIAlertView *alert_push = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Setting", nil];
        alert_push.tag = 2;
        [alert_push show];

        NSLog(@" Push Notification OFF");
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Maulik Salvi
  • 269
  • 3
  • 9