Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a user has ever seen the dialog asking for push notification permission (ios)

I am aware of enabledremotenotificationtypes, but it does not help me because if I receive enabledremotenotificationtypes == UIRemoteNotificationTypeNone, there's no way for me to tell if the user has 1. accepted push notifications once but then turned it off via setting later OR 2. rejected push notifications OR 3. never seen the blue dialog asking for permission. I need a way to differentiate these three cases.

Any help will be really appreciated.

like image 291
user1657624 Avatar asked Sep 09 '12 04:09

user1657624


People also ask

What is the difference between a push notification and a regular notification?

The main difference between push notification and notification is that the latter are created internally from an application on the device that wants to show user some information, a reminder, some news or updates, and so on.

When should you ask for push permissions?

Ask for user's permission only when he is willing to grant permission. So instead of showing the permission prompt directly, it would be better to ask for user's intent first. For e.g The user should be asked if he would like to give permission to the site to send him regular updates/notifications.

How do I know if push notifications are on?

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

Can you have a custom dialog message when asking for notification permissions?

No, this is system message, you can't change to custom. Save this answer.


1 Answers

The solution is a bit of a hack, but it does work. You need to call registerUserNotificationSettings for two different notificationSettings - one without a notificationCategory and one with a notificationCategory:

    //Request notification permission
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

//Request notification permission again, but with a category with no actions
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = @"com.xyz.markNotificationPopupShownCategoryIdentifier";

UIUserNotificationSettings *notificationSettingsWithCategory = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:[NSSet setWithObject:category]];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettingsWithCategory];

The didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings method in the app delegate will be called two times, and irrespective of the user's answer in the permission notification, after the second call, the current notification settings will contain the category. As long as the category count is greater than 0, you can know for sure that the notifications permission dialog has been shown:

if ([UIApplication sharedApplication].currentUserNotificationSettings.categories.count > 0) {
    NSLog(@"Notifications permissions has been asked");
} else {
    NSLog(@"Notifications permissions hasn't been asked");
}
like image 58
Peter Robert Avatar answered Oct 04 '22 20:10

Peter Robert