【问题标题】:How can I determine if a user has ever seen the dialog asking for push notification permission (ios)如何确定用户是否曾看到请求推送通知权限的对话框 (ios)
【发布时间】:2012-09-09 04:14:10
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

  • 如果你有 iOS 开发者证书,请阅读 iOS 6 的更新日志。我想你会找到答案的。
  • 转到 developer.apple.com。转到 iOS 6 测试版页面。阅读变更日志。由于许可协议,我无法在此详细说明。
  • 我如何在 ios 5 或更早版本上使用它?
  • 我帮不了你,但我怀疑你不能
  • 感谢您如此含糊,以至于我现在不知道您当时的意思,因为在我能找到的任何 iOS 6 差异文档中几乎没有关于推送通知的内容...

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


【解决方案1】:

解决方案有点小技巧,但确实有效。您需要为两种不同的 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];

appdelegate中的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");
}

【讨论】:

    【解决方案2】:

    这就是我处理这种情况的方式——我是一个新手,所以这可能不是最佳的,但它对我有用。创建一个int 属性pushNotificationSeen。如果用户看到对话并拒绝它,则将pushNotificationSeen 设置为1。如果用户看到对话并接受它,则将pushNotificationSeen 设置为2。然后,在下一行代码中,调用这样的函数(定义代码中的其他地方):

    -(void)saveData
    {
    if (self.pushNotificationSeen)
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setInteger:self.pushNotificationSeen forKey:@"seen?"];
        [defaults synchronize];
    }
    }
    

    然后将以下行添加到viewDidLoad

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.pushNotificationSeen = [defaults integerForKey:@"seen?"];
    

    此时,您可以通过查看 self.pushNotificationSeen 是 0、1 还是 2 来了解用户做了什么或没有做什么。

    我希望这是足够的信息——我的睡眠时间并不长。如果我一直感到困惑,请告诉我,我可以澄清一下。

    【讨论】:

    • 问题是:如何知道推送通知何时被看到?
    • 如何知道用户是否授予权限?
    猜你喜欢
    • 2023-03-18
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多