我知道enableremotenotificationtypes,但它对我没有帮助,因为如果我收到enabledremotenotificationtypes == UIRemoteNotificationTypeNone,我就无法判断用户是否有1.接受推送通知一次,但之后通过设置将其关闭或者2.拒绝推送通知或3.从未见过要求许可的蓝色对话框。我需要一种方法来区分这三种情况。
任何帮助都将非常感激。
答案 0 :(得分: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");
}
答案 1 :(得分:-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来确定用户已经完成或未完成的操作。
我希望这是足够的信息 - 我的睡眠不是很多。如果我一直在困惑,请告诉我,我可以澄清。