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

不羁的心 提交于 2019-11-27 04:32:11
Greg

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.

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

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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!