Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Push Notification Settings - Denied Permission vs Permission Never Requested

Tags:

Is it possible to differentiate between the cases where

  1. an iOS user has explicitly denied user notification permissions, and
  2. an iOS user has never been prompted for permission?

My situation: In the past, I've prompted for user notification permission, but never kept track of requests myself. Later, I stopped attempting to register any notification settings. Now, I'd like to re-introduce user notifications.

After a significant event in the App, my plan is to display some sort of UI that explains the benefit of opting in to user notifications. However, if the user has already declined, I'd prefer to show a separate UI that can take them into Settings.app.

Currently, I'm using -[UIApplication currentUserNotificationSettings] to grab the current settings, but it appears that this returns UIUserNotificationTypeNone for both of the above described cases.

like image 420
Sean Danzeiser Avatar asked Feb 28 '15 17:02

Sean Danzeiser


People also ask

When should I ask for push notification permissions?

You don't have to ask for push notification permissions. While Contacts/Locations are the dangerous permissions because you are accessing user data. So it is always needed to ask the user to allow it.

Should I allow push notifications on my iPhone?

If you allow notifications on your iPhone from every app you use, you may spend more time checking your notifications than getting things done. While some notifications can be very important – phone calls and text messages, for example – too many notifications is bad for our sanity.


1 Answers

Personally I haven't found a way to determine this via a quick query of the iOS SDK.

However I have been able to track this myself recording when -[UIApplication application:didRegisterUserNotificationSettings:] is called.

When iOS calls this method, you can be sure the user has been prompted for user notification permissions and has (importantly) either accepted or denied it.

Storing when this occurs you can later check this value to determine if the prompt has been shown before or not.

Example Code:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ABHasPromptedForUserNotification"];     //... your other notification registration handling...  }  - (BOOL)hasPromptedForUserNotification {     return [[NSUserDefaults standardUserDefaults] boolForKey:@"ABHasPromptedForUserNotification"]; } 

FYI: I've found it preferable to set "ABHasPromptedForUserNotification" as true in the in -[UIApplication application:didRegisterUserNotificationSettings:] rather than when I call -[UIApplication registerForRemoteNotifications] as in some situations the user can be shown the prompt multiple times. This can happen if the user backgrounds the app, or takes a call. In these cases the prompt will be hidden by iOS, and shown again if next time you call -[UIApplication registerForRemoteNotifications]. Setting this setting in the delegate avoids thinking the user has been prompted before and won't be prompted again in these edge cases.

like image 54
Glen T Avatar answered Oct 24 '22 07:10

Glen T