【问题标题】:How to know if the push notification permission alert has already been shown如何知道推送通知权限警报是否已显示
【发布时间】:2014-10-05 12:24:03
【问题描述】:

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

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

有什么想法吗?

【问题讨论】:

  • 用户怎么可能没有看到推送通知提醒?
  • 当您第一次安装并运行该应用程序时,用户尚未看到该应用程序的推送通知权限警报。
  • 警报是您打开应用程序时显示的第一件事。用户没有办法不看到它
  • 只有在调用 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 时才会显示推送通知权限警报,而不会在应用程序启动时显示

标签: ios push-notification apple-push-notifications


【解决方案1】:

你可以使用 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];
}

还有

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

【讨论】:

  • 这种方法的问题是当用户删除应用程序并再次安装时,NSUserDefaults 中的设置将消失,用户将再次看到自定义警报。一旦他/她选择是,什么都不会发生……
  • 是的,我同意你的看法。但是,卸载并重新安装应用程序并不常见。
  • 同意,但如果存在完整的证明解决方案,我更愿意。
  • 用户卸载应用后,在新安装中请求权限
  • @micap 仅在卸载和重新安装之间经过一天的情况下。但是,是的,这应该涵盖所有情况的 99.99%,所以这似乎是最好的解决方案。
【解决方案2】:

这只是一种变通方法,但在大多数情况下都可以使用。

当 iOS 提示用户允许推送通知时,会调用 applicationWillResignActive。

在调用 registerForRemoteNotificationTypes...启动一个计时器(或 dispatch_after),它将向用户显示一个新警报,说明他们需要使用“设置”应用来启用通知。然后在 applicationWillResignActive 中取消定时器。这样,解释警报只会在 applicationWillResignActive 从未被调用时显示。

我看到的唯一问题是,如果应用程序在计时器运行的确切时间因其他原因退出活动......但这不会让你处于更糟糕的情况下,你已经处于并且具有优势在大多数情况下工作。

【讨论】:

    【解决方案3】:

    我发现的解决方案有点破解,但它确实有效。 您需要调用registerUserNotificationSettings 来获取两种不同的通知设置——一种没有notificationCategory,另一种有notificationCategory:

            //Request notification permission
        UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    
        //Request notification permission again, but with a category with no actions
        UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
        category.identifier = @"com.xyz.markNotificationPopupShownCategoryIdentifier";
    
        UIUserNotificationSettings *notificationSettingsWithCategory = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:[NSSet setWithObject:category]];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettingsWithCategory];
    

    appdelegate中的didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings方法会被调用两次,无论用户在权限通知中的回答如何,第二次调用后,当前通知设置都会包含该类别。只要类别计数大于0,就可以确定已经显示了通知权限对话框:

    if ([UIApplication sharedApplication].currentUserNotificationSettings.categories.count > 0) {
        NSLog(@"Notifications permission has been asked");
    } else {
        NSLog(@"Notifications permission hasn't been asked");
    }
    

    【讨论】:

    • 这告诉我没有询问权限,它显然对我的设备具有哪些权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2013-09-11
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    相关资源
    最近更新 更多