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

2019-08-02 20:08发布

问题:

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.