anon_nerd
anon_nerd

Reputation: 1281

iOS Local Notifications: app is not asking for permissions to notify the user

I am not sure if this is a weird bug, but in my

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

I have the following lines of code to ask for user permissions:

// Register for Push Notitications, if running iOS 8
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                        UIUserNotificationTypeBadge |
                                                        UIUserNotificationTypeSound);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                                 categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    } else {
        // Register for Push Notifications before iOS 8
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                         UIRemoteNotificationTypeAlert |
                                                         UIRemoteNotificationTypeSound)];
    }

On the simulator, everything works fine, but on my iPhone, when I install the app, it does not ask for these permissions. I have initiated breakpoints to make sure the lines execute and they do.

When I go into Settings > Notifications > My App the notifications are on, so it seems that the system knows I have granted permission before, so it doesn't ask, but that seems weird.

Has anyone seen this before?

Upvotes: 4

Views: 2061

Answers (1)

anon_nerd
anon_nerd

Reputation: 1281

I actually found the answer after Googling again (I didn't find the answer before)

Straight from Apple:

Resetting the Push Notifications Permissions Alert on iOS The first time a push-enabled app registers for push notifications, iOS asks the user if they wish to receive notifications for that app. Once the user has responded to this alert it is not presented again unless the device is restored or the app has been uninstalled for at least a day.

If you want to simulate a first-time run of your app, you can leave the app uninstalled for a day. You can achieve the latter without actually waiting a day by following these steps:

  1. Delete your app from the device.
  2. Turn the device off completely and turn it back on.
  3. Go to Settings > General > Date & Time and set the date ahead a day or more.
  4. Turn the device off completely again and turn it back on.

After doing this, asking for permission worked again.

Upvotes: 6

Related Questions