Push Notification ON or OFF Checking in iOS
Asked Answered
F

4

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

Fairman answered 4/12, 2013 at 11:59 Comment(0)
C
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
Chequer answered 4/12, 2013 at 12:4 Comment(6)
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 KiFairman
Whenever your app get launches Do the registerForRemoteNotificationTypes Then automatically its get enabledChequer
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 falseChequer
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".Fairman
Check with this UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; one can use if (types & UIRemoteNotificationTypeAlert){}Chequer
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. :)Fairman
M
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];
Mandrill answered 18/9, 2014 at 13:35 Comment(2)
For the record: if user disabled notifications or pressed 'don't allow', isRegisteredForRemoteNotifications will still be true the next time.Fikes
If user pressed Don't allow, isRegisteredForRemoteNotifications seems to return false.Lauds
C
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
Chequer answered 4/12, 2013 at 12:4 Comment(6)
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 KiFairman
Whenever your app get launches Do the registerForRemoteNotificationTypes Then automatically its get enabledChequer
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 falseChequer
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".Fairman
Check with this UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; one can use if (types & UIRemoteNotificationTypeAlert){}Chequer
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. :)Fairman
T
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);
   }
}
Tiedeman answered 22/12, 2014 at 3:50 Comment(1)
i think some comments might make it easier to understand than ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire); How about //Push Notifications Are DisabledEichelberger
R
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");
    }
}
Repetition answered 25/9, 2015 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.