6

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

任何帮助将不胜感激。

4

2 回答 2

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];

应用委托中的 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");
}
于 2015-01-07T13:14:00.463 回答
-3

这就是我处理这种情况的方式——我是一个新手,所以这可能不是最佳的,但它对我有用。创建一个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 来了解用户做了什么或没有做什么。

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

于 2012-09-10T09:45:54.313 回答