registerForRemoteNotifications

registerForRemoteNotifications

本文介绍了iOS 推送通知设置 - 拒绝权限 vs 从未请求权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

是否可以区分以下情况

  1. iOS 用户明确拒绝了用户通知权限,并且
  2. 从未提示 iOS 用户获得许可?

我的情况:过去,我提示用户通知权限,但我自己从未跟踪请求.后来,我停止尝试注册任何通知设置.现在,我想重新引入用户通知.

My situation: In the past, I've prompted for user notification permission, but never kept track of requests myself. Later, I stopped attempting to register any notification settings. Now, I'd like to re-introduce user notifications.

在应用发生重大事件后,我的计划是显示某种用户界面,解释选择接收用户通知的好处.但是,如果用户已经拒绝,我更愿意显示一个单独的 UI,可以将他们带入 Settings.app.

After a significant event in the App, my plan is to display some sort of UI that explains the benefit of opting in to user notifications. However, if the user has already declined, I'd prefer to show a separate UI that can take them into Settings.app.

目前,我正在使用 -[UIApplication currentUserNotificationSettings] 来获取当前设置,但对于上述两种情况,这似乎都会返回 UIUserNotificationTypeNone.

Currently, I'm using -[UIApplication currentUserNotificationSettings] to grab the current settings, but it appears that this returns UIUserNotificationTypeNone for both of the above described cases.

推荐答案

我个人还没有找到通过快速查询 iOS SDK 来确定这一点的方法.

Personally I haven't found a way to determine this via a quick query of the iOS SDK.

然而,当 -[UIApplication application:didRegisterUserNotificationSettings:] 被调用时,我已经能够自己跟踪这个记录.

However I have been able to track this myself recording when -[UIApplication application:didRegisterUserNotificationSettings:] is called.

当 iOS 调用此方法时,您可以确定已提示用户提供用户通知权限,并且(重要的是)接受或拒绝了它.

When iOS calls this method, you can be sure the user has been prompted for user notification permissions and has (importantly) either accepted or denied it.

在发生这种情况时进行存储,您可以稍后检查此值以确定之前是否显示过提示.

Storing when this occurs you can later check this value to determine if the prompt has been shown before or not.

示例代码:

- (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"];
}

仅供参考:我发现最好在 -[UIApplication application:didRegisterUserNotificationSettings:] 中将 "ABHasPromptedForUserNotification" 设置为 true,而不是在调用 时>-[UIApplication registerForRemoteNotifications] 因为在某些情况下可以多次向用户显示提示.如果用户后台应用程序或接听电话,就会发生这种情况.在这些情况下,提示将被 iOS 隐藏,并在您下次调用 -[UIApplication registerForRemoteNotifications] 时再次显示.在委托中设置此设置可避免认为用户之前已被提示并且在这些边缘情况下不会再次提示.

FYI: I've found it preferable to set "ABHasPromptedForUserNotification" as true in the in -[UIApplication application:didRegisterUserNotificationSettings:] rather than when I call -[UIApplication registerForRemoteNotifications] as in some situations the user can be shown the prompt multiple times. This can happen if the user backgrounds the app, or takes a call. In these cases the prompt will be hidden by iOS, and shown again if next time you call -[UIApplication registerForRemoteNotifications]. Setting this setting in the delegate avoids thinking the user has been prompted before and won't be prompted again in these edge cases.

这篇关于iOS 推送通知设置 - 拒绝权限 vs 从未请求权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:28