16

I need to always know which options the user choose on the push notification settings.
(The options are - alert, sound and badges)

So when my app launch I call:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

and detect what the user chose.

But how can I detect if the user change the settings later during the app life time?
Is there some delegate method that get called when a change occur with this settings?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Eyal
  • 10,777
  • 18
  • 78
  • 130

4 Answers4

22

There is no delegate. You need to query the UIApplication property enabledRemoteNotificationTypes periodically, for example in applicationDidBecomeActive:.

For details check these answers:

Determine on iPhone if user has enabled push notifications

View In Lock Screen and enabledRemoteNotificationTypes - iOS5

Edit:
If you need to reset the push notification setting and the permission alert, have a look at the Apple technical note TN2265. In section "Resetting the Push Notifications Permissions Alert on iOS" they explain how to reset the setting on iOS. However, many developers complain that the procedure doesn't work. Not sure if this link will work, you will need to have access to the Apple forum, but it is one of the threads about this exact issue.

I was myself wondering if maybe Apple has removed the permission dialog in iOS 5.1. Otherwise why would they require the application to show the alert? According to AppStore review guidelines until June 2016:

5.3 Apps that send Push Notifications without first obtaining user consent will be rejected

For example Path (application) asks the user to opt-in for push notification in the middle of the sing-up process, not when the application starts for the first time.

Not sure what should be the purpose of the prompt anyway as the application can't query the state of the notification setting. In particular, the application can check which notification types (using enabledRemoteNotificationTypes) are enabled but NOT if push notifications for a particular application are enabled or disabled (the Notification Center ON/OFF switch at the top). At least that's the behavior in iOS 5.1. Even if user disables notifications for that application, the application can still register for push notifications (using registerForRemoteNotificationTypes) and WILL receive an APNS token.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Greg
  • 8,230
  • 5
  • 38
  • 53
  • thanks:) do u know what should I do to get the "allow push for this app" pop box that user get at the first time he launch the app? I need it for debugging different scenarios, I tried to delete the app and re-install but did't got the pop box... – Eyal Apr 17 '12 at 19:20
  • I edited the answer to include some links that maybe useful. However many developers complain that the procedure as outlined by Apple doesn't work. I've never seen the permission dialog when developing my application so I didn't even knew it existed until reading some posts from other developers. – Greg Apr 18 '12 at 09:46
  • Last part isn't true, at least on iOS 7, enabledRemoteNotificationTypes is for your app only and if the user set the notifications to none you won't receibe the APNs token. – jcesarmobile Aug 01 '14 at 12:19
  • enabledRemoteNotificationTypes always gives me UIRemoteNotificationTypeNone. Any reason ? – Brijesh Thakur Nov 11 '14 at 05:12
  • If you are seeing that in the Simulator then that's normal. Push notifications don't work in the Simulator so you won't get a token – intractve Nov 16 '14 at 11:10
  • 5.3 was removed in June 2016, so I've changed the link to a Wayback Machine link. – Cœur Jul 11 '19 at 10:18
7

Check it when your app becomes active rather than just at launch.

Nick Bull
  • 4,276
  • 1
  • 18
  • 25
0

This is an example when Push is implemented through UrbanAirship. Every time when user opt-in/opt-out for push following delegate fires and with method below this you can check for (YES/NO).

Same can be achieved with UIApplication delegate if not using UrbanAirship.

- (void)registrationSucceededForChannelID:(NSString )channelID deviceToken:(NSString )deviceToken
    {
        NSLog(@"registrationSucceededForChannelID : %@",[self appRegisterForPushNotification]?@"YES":@"NO");
    }


    - (BOOL)appRegisterForPushNotification {
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
            UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
            return ((types & UIUserNotificationTypeAlert) || (types & UIUserNotificationTypeSound));
        }
        return NO;
    }
Venu Gopal Tewari
  • 5,672
  • 42
  • 41
0

In addition to the answer @Nick Bull subscribe your sensitive ViewController to the UIApplicationWillEnterForegroundNotification an listen when the user done his Settings job and return to your app:

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(appEnterForeground:)
                                             name: UIApplicationWillEnterForegroundNotification object:nil];

Swift variant is here: How to check that user is back from Settings

A. Petrov
  • 910
  • 13
  • 18