ios - 我如何确定用户是否曾经看到过请求推送通知权限的对话框 (ios)

标签 ios push-notification apple-push-notifications

我知道 enabledremotenotificationtypes,但它对我没有帮助,因为如果我收到 enabledremotenotificationtypes == UIRemoteNotificationTypeNone,我无法判断用户是否有 1. 接受过一次推送通知但随后通过稍后设置将其关闭或2. 拒绝推送通知或 3. 从未见过请求许可的蓝色对话框。我需要一种方法来区分这三种情况。

任何帮助将不胜感激。

最佳答案

该解决方案有点麻烦,但确实有效。您需要为两种不同的 notificationSettings 调用 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];

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

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

关于ios - 我如何确定用户是否曾经看到过请求推送通知权限的对话框 (ios),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12336466/

相关文章:

ios - 在 Swift 中将数据传递给 ViewModel

ios - 从 Swift 中的 UIActivityViewController 中排除注释和提醒

ios - 检查推送通知权限后,在completionHandler block 中从nib 文件添加 subview

php推送通知失败

ios - 如何在点击它们时将 View 带到前面? ios

ios - 为什么我在 tableView 中看到动画前的静态文本

ios - 使用 Amazon SNS 向 iOS 发送推送通知并使用本地化格式化字符串处理它们

iphone - 是否可以在 iPhone 上发送 "silent"推送通知(不向用户发出任何警报)?

ios - 预期返回 'Bool' parse.com 推送通知的函数中缺少返回

wcf - 如何验证 Apple APN 设备 token - WCF .NET