0

Hi in my app i have notification section and user can enable notifications using switch.After first launch when ever user on the switch i am getting don't allow or ok alertview from ios.If user select don't allow and switch will be off and user will not get notifications. Now if user try to on the switch i want to show an alert to user with text "Please enable notifications from settings".Can any one please suggest the way to do this.

Naresh
  • 21
  • 5

5 Answers5

0

You can check the the permission using isRegisteredForRemoteNotifications method.

- (void)checkForNotificationPermission
{
    if (!([[UIApplication sharedApplication] isRegisteredForRemoteNotifications] && [self pushNotificationsEnabled])) 
    {
       // Show alert here
    }
}


// For fixing iOS 8 issue mentioned here http://stackoverflow.com/a/28441181/1104384
- (BOOL)pushNotificationsEnabled
{
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)])
    {
        UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
        return (types & UIUserNotificationTypeAlert);
    }
    else
    {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        return (types & UIRemoteNotificationTypeAlert);
    }
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

For UILocalNotification permission check the following, types parameter value will be none incase user has not allowed it.

[[UIApplication sharedApplication] currentUserNotificationSettings]
Muneeba
  • 1,756
  • 10
  • 11
0
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");
                }

            }
Nikunj5294
  • 391
  • 3
  • 14
0
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);

        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [[UIAp

plication sharedApplication] registerUserNotificationSettings:settings];

// [[UIApplicationsharedApplication]registerForRemoteNotifications];

        if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {


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

            if (types == UIUserNotificationTypeNone) {



            [_TransparentView setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.8]];


              lblDescription.text=@"Please enable notifications from settings.";
            }

        }
    }
Ishwar Hingu
  • 562
  • 7
  • 14
0

Try this code. It will work for iOS 8.0 later and before versions.

 if (([[[UIDevice currentDevice] systemVersion] compare:8.0 options:NSNumericSearch] != NSOrderedAscending)) {
    if (![[UIApplication sharedApplication] isRegisteredForRemoteNotifications])
    {
        DisplayAlert(@"Please enable Permission from Settings->App Name->Notifications->Allow Notifications");
        return;
    }
}
else{
    UIRemoteNotificationType status = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (status == UIRemoteNotificationTypeNone)
    {
        DisplayAlert(@"Please enable Permission from Settings->App Name->Notifications->Allow Notifications");
        return;
    }
}
kb920
  • 3,039
  • 2
  • 33
  • 44