objective c - Detect when user change the app'

2019-01-09 01:08发布

问题:

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?

回答1:

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:

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.



回答2:

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



回答3:

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;
    }