Is it possible to differentiate between the cases where
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With