How to check if the user has enabled push notification from the settings?
Asked Answered
C

3

11

This question is specific to iOS 10 APNS changes.

This is the flow of my app:

  1. App Installed
  2. App Starts ➝ Login Screen
  3. Successful Login ➝ Home Screen
  4. Push Notification ➝ Request
  5. Push Notification ➝ Don't Allow
  6. App Close
  7. Settings ➝ User enabled Push Notification
  8. App Open
  9. How to check if settings updated?
  10. App Close
  11. Settings ➝ User disabled Push Notification
  12. App Open
  13. How to check if settings updated?

I am only requesting for push notification (step 4.) when the user logs in. So until a user logs out I will not able to re-request for the push.

Is there any neat and clear solution to this so that we can support iOS 10 changes while still supporting iOS 8 or 9?

Cyanosis answered 24/12, 2016 at 10:46 Comment(1)
Did you found any solution for thisGladwin
H
10

UIUserNotificationSettings was deprecated back in iOS8. If you want to access the general status of your apps settings, check out UNUserNotifications, the new framework. My understanding is that it treats push and local as one thing. When you register notifications, you can then call to register push. But for the local permissions -- badging and so on, you still need to request user permission. That is, your device can accept push notifications without user permission in order to received data updates, but you can only show notifications via the center with permissions. Here's how to see what permissions have been granted.

  1. Import the framework into your class

    @import UserNotifications;
    
  2. Query the settings

      - (void)_queryNotificationsStatus
    {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings){
    
        //1. Query the authorization status of the UNNotificationSettings object
        switch (settings.authorizationStatus) {
        case UNAuthorizationStatusAuthorized:
            NSLog(@"Status Authorized");
            break;
        case UNAuthorizationStatusDenied:
            NSLog(@"Status Denied");
            break;
        case UNAuthorizationStatusNotDetermined:
            NSLog(@"Undetermined");
            break;
        default:
            break;
        }
    
    
        //2. To learn the status of specific settings, query them directly
        NSLog(@"Checking Badge settings");
        if (settings.badgeSetting == UNAuthorizationStatusAuthorized)
        NSLog(@"Yeah. We can badge this puppy!");
        else
        NSLog(@"Not authorized");
    
      }];
    }
    
Heine answered 10/7, 2017 at 23:49 Comment(0)
H
8

use this code-

if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
// yes
}else{
// no
}
Hyder answered 24/12, 2016 at 12:6 Comment(1)
Returns true also if user has notifications disabledBwana
C
3

You can use getNotificationSettingsWithCompletionHandler whenever your app enters in forground.

-(void) IsNotifictaionEnabled :(void (^)(BOOL isActive))handler {
    [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        if (settings.alertSetting == UNNotificationSettingEnabled) {
            handler(YES);
        } else {
            handler(NO);
        }
    }];
}

///////////

Following is the original answer, but currentUserNotificationSettings is deprecated now.

you can use currentUserNotificationSettings whenever your app enters in foreground.

 UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
     if (grantedSettings.types == UIUserNotificationTypeNone) {
            NSLog(@"No permiossion granted");
        }
        else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
            NSLog(@"Sound and alert permissions ");
        }
        else if (grantedSettings.types  & UIUserNotificationTypeAlert){
            NSLog(@"Alert Permission Granted");
        }

If you want to check if the status has changed from the previous one, You can keep the previous value of currentUserNotificationSettings to some variable and compare it with current value overtime in applicationWillEnterForeground method.

Clari answered 24/12, 2016 at 11:2 Comment(2)
This seems to be a code for local notifications, would it work for remote notifications?Cyanosis
No, CurrentUserNotificationSettings works for both. - will work for remote notificationsClari

© 2022 - 2024 — McMap. All rights reserved.