Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 333
Hemang Avatar asked Dec 24 '16 10:12

Hemang


People also ask

How do I know if push notifications are enabled?

Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.

What does enable push from settings mean?

Push notifications are messages that pop up on a user's mobile phone or desktop device via their chosen web browser. These little banners slide into view — whether or not your app or website is open.

How do I know if I have push notifications on my Iphone?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered—immediately or in the scheduled notification summary.


2 Answers

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");
    
      }];
    }
    
like image 90
Mike Critchley Avatar answered Nov 03 '22 19:11

Mike Critchley


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.

like image 21
PlusInfosys Avatar answered Nov 03 '22 17:11

PlusInfosys