Objective-C - Detect when user change the app's notifications settings
Asked Answered
A

4

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?

Aerostat answered 17/4, 2012 at 12:12 Comment(0)
D
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.

Dint answered 17/4, 2012 at 12:24 Comment(6)
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...Aerostat
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.Dint
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.Sodomy
enabledRemoteNotificationTypes always gives me UIRemoteNotificationTypeNone. Any reason ?Machinist
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 tokenFisher
5.3 was removed in June 2016, so I've changed the link to a Wayback Machine link.Kelantan
E
7

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

Ecphonesis answered 17/4, 2012 at 12:16 Comment(0)
L
1

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

Lion answered 3/3, 2020 at 6:48 Comment(0)
K
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;
    }
Kunzite answered 3/1, 2017 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.