繁体   English   中英

如何确定用户是否曾见过要求推送通知权限的对话框(ios)

[英]How can I determine if a user has ever seen the dialog asking for push notification permission (ios)

我知道enableremotenotificationtypes,但它没有帮助我,因为如果我收到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];

应用程序委托中的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");
}

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

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

然后viewDidLoad添加到viewDidLoad

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

此时,您可以通过检查self.pushNotificationSeen是0,1还是2来确定用户已执行或未执行的操作。

我希望这是足够的信息 - 我的睡眠不是很多。 如果我一直困惑,请告诉我,我可以澄清一下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM