就我个人而言,我还没有找到通过快速查询 iOS SDK 来确定这一点的方法。
-[UIApplication application:didRegisterUserNotificationSettings:]
但是,当我被调用时,我已经能够自己跟踪这个记录。
当 iOS 调用此方法时,您可以确定用户已被提示用户通知权限,并且(重要的是)已接受或拒绝它。
存储发生这种情况时,您可以稍后检查此值以确定之前是否显示过提示。
示例代码:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ABHasPromptedForUserNotification"];
//... your other notification registration handling...
}
- (BOOL)hasPromptedForUserNotification {
return [[NSUserDefaults standardUserDefaults] boolForKey:@"ABHasPromptedForUserNotification"];
}
仅供参考:我发现最好"ABHasPromptedForUserNotification"
在 in 中设置为 true,-[UIApplication application:didRegisterUserNotificationSettings:]
而不是在我调用时设置,-[UIApplication registerForRemoteNotifications]
因为在某些情况下,用户可以多次显示提示。如果用户后台应用程序或接听电话,就会发生这种情况。在这些情况下,提示将被 iOS 隐藏,并在您下次调用时再次显示-[UIApplication registerForRemoteNotifications]
。在委托中设置此设置可避免认为用户之前已收到提示,并且在这些边缘情况下不会再次收到提示。