Push Notification ON or OFF Checking in iOS

2019-01-21 11:58发布

问题:

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

回答1:

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


回答2:

In iOS 8 you can now use:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

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

[[UIApplication sharedApplication] currentUserNotificationSettings];


回答3:

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


回答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");
    }
}