Shongsu
Shongsu

Reputation: 1023

How to detect ios push notification permissions/settings programmatically

In Notification settings (Settings->Notifications->AnyAppName), there are 5 items and each has a switch button, Sounds,Badge App Icon, Show on Lock Screen, Show in History, Show as Banners.

I am using [[[UIApplication sharedApplication] currentUserNotificationSettings] types] to get user's settings and promote corresponding alert to use. It may return value 0~7 represents any combination of Sound, Badge and Banners. The question is, are we able to detect the states of Show on Lock Screen, Show in History?

Also, at the bottom of the setting page, theres is an OPTIONS option called Show Previews, it has three options: Always(Default), When Unlocked and Never. Are we able to get user's setting programmatically for this?

Upvotes: 4

Views: 2410

Answers (1)

thijsonline
thijsonline

Reputation: 1227

You should use the UserNotifications framework, supported starting from iOS 10. This will allow you to retrieve UNNotificationSettings via the getNotificationSettingsWithCompletionHandler: function of UNUserNotificationCenter. In UNNotificationSettings you can check some values:

  • notificationCenterSetting (Show in History)
  • lockScreenSetting (Show on Lock Screen)
  • alertSetting (Show as Banners)
  • alertStyle (the type of the banner)

For instance:

[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
    if(settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
        //notifications are enabled for this app

        if(settings.notificationCenterSetting == UNNotificationSettingEnabled ||
          settings.lockScreenSetting == UNNotificationSettingEnabled ||
          (settings.alertSetting == UNNotificationSettingEnabled && settings.alertStyle != UNAlertStyleNone)) {
            //the user will be able to see the notifications (on the lock screen, in history and/or via banners)

            dispatch_async(dispatch_get_main_queue(), ^(){

                //now for instance, register for remote notifications
                //execute this from the main queue
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            });
        }
        else {
            //the user must change notification settings in order te receive notifications
        }
    }
    else {
        //request authorization
        [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if(granted) {
                dispatch_async(dispatch_get_main_queue(), ^(){

                    //now for instance, register for remote notifications
                    //execute this from the main queue
                    [[UIApplication sharedApplication] registerForRemoteNotifications];
                });
            }
            else {
                //user denied the authorization request
                //the user must change notification settings in order te receive notifications
            }
        }
    }
}];

Upvotes: 1

Related Questions