如何知道是否已显示推送通知权限警报

Jov*_*van 25 push-notification apple-push-notifications ios

我想实现一个自定义屏幕,通知我的用户我为什么要请求推送通知权限.在他们按下该自定义屏幕中的按钮后,我会显示iOS推送通知权限对话框[[UIApplication sharedApplication] registerForRemoteNotificationTypes:

如果用户尚未看到推送通知权限对话框,我只想显示此自定义屏幕一次.我不能使用,[[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone因为如果用户决定不允许推送通知,这也将返回'none'.

任何人的想法?

小智 6

您可以使用NSUserDefaults:

#define kPushNotificationRequestAlreadySeen @"PushNotificationRequestAlreadySeen"

if(![[NSUserDefaults standardUserDefaults] boolForKey:kPushNotificationRequestAlreadySeen]) {

    // Notify the user why you want to have push notifications
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:... delegate:self ...];
    [alertView show];

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kPushNotificationRequestAlreadySeen];
}
else {
    // Already allowed -> register without notifying the user
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
Run Code Online (Sandbox Code Playgroud)

- (void)alertView:(UIAlertView*)alertView didDismissWithButtonIndex:(NSInteger)index {
    // Actually registering to push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
Run Code Online (Sandbox Code Playgroud)

  • 用户卸载应用后,将在新安装中请求权限 (4认同)
  • 同意,但我希望有一个完整的解决方案。 (2认同)
  • @micap仅在卸载和重新安装之间已经过了一天。但是,是的,这应该覆盖所有案例的99.99%,因此这似乎是最好的解决方案。 (2认同)