How do I know if user didn't allow push notifications

陌路散爱 提交于 2019-12-13 01:28:18

问题


I saw this question but didn't understand if there is a clear answer.

I can tell if the user pressed "don't allow" on the SECOND launch of the app by setting a flag:

BOOL didRequest = [[NSUserDefaults standardUserDefaults] boolForKey:@"DidRequestPushNotifications"];
    UIRemoteNotificationType types =    [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types == UIRemoteNotificationTypeNone && didRequest)
    {
        [self showAlertToUserToEnableRemoteNotificationsOnDeviceInSettings];
    }
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"DidRequestPushNotifications"];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

But for the first launch of the app - both delegate methods for success and failure aren't called and that means that there is no way to know for sure.

Any work around?


回答1:


In addition to the answer you have linked, a work around I can think of is, once user successfully registered for pushes, You can store the device token in the NSUserDefaults.

This way you can check if the value of that user setting is nil or not.



来源:https://stackoverflow.com/questions/22934729/how-do-i-know-if-user-didnt-allow-push-notifications

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