iOS 9 : How to detect when user said 'Don' t Allow' to the push notification request?

标签 ios objective-c apple-push-notifications

<分区>

在 iOS 9 中,是否有我可以读取的系统级回调告诉我用户是否在推送通知请求上点击了“不允许”?

我使用自定义屏幕提示用户,告知他们推送通知及其在我的应用中的值(value)。

Custom Tripnary Prompt

他们有两个选择,是或否。如果他们选择是,我请求操作系统推送通知,他们会看到如下图所示的弹出窗口。

iOS Level system prompt for push notifications

现在,如果用户点击"is",则会有一个名为 didRegisterForRemoteNotificationsWithDeviceToken 的函数告诉我此设备已注册推送通知。我可以使用它前进到下一个屏幕(并在注册后将它们带入第一个屏幕)

但是,我如何检测用户是否点击“不允许”?我需要知道这一点,这样我才能相应地将用户移动到下一个屏幕(并将他们带到第一个屏幕注册后)。如果用户点击“不允许”,则不会调用函数 didFailToRegisterForRemoteNotificationsWithError

这个问题不是重复的,因为该问题的答案是特定于 iOS 7 的,而我的问题是特定于 iOS 9 的。

最佳答案

从 iOS 8 开始,通知注册过程发生了变化,不再需要用户仅授予远程通知权限。

从技术上讲,您现在无需获得用户许可即可注册远程通知。您需要权限的是用户通知设置(警报、声音和角标(Badge))。这些现在对本地和远程通知都是通用的,这使得其他答案在技术上是不正确的。

您通过 UIApplication 上的 -[UIApplication registerUserNotificationSettings:] 方法请求权限,根据文档,您会收到对 -[UIApplicationDelegate 应用程序的回调: didRegisterUserNotificationSettings:] 委托(delegate)方法。

在标题中,有一条评论如下:

// This callback will be made upon calling -[UIApplication registerUserNotificationSettings:]. The settings the user has granted to the application will be passed in as the second argument.

这意味着如果用户未授予通知权限(本地和远程),则第二个参数将不包含任何值。


-[UIApplication isRegisteredForRemoteNotifications] 只会告诉您应用程序是否已实际注册到 Apple 的推送服务器并已收到设备 token :

Return Value
YES if the app is registered for remote notifications and received its device token or NO if registration has not occurred, has failed, or has been denied by the user.

UIApplication 文档值得一读,因为它包含您需要的所有信息。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instm/UIApplication

关于iOS 9 : How to detect when user said 'Don' t Allow' to the push notification request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35126361/

相关文章:

ios - Objective-C 从服务器显示 PDF

ios - iTunes Connect 不允许我创建现有应用程序的新版本

ios - prepareForSegue 中的分配 false

iphone - 如何在 Objective-C 中相交两个数组?

objective-c - 当再次滚动表格 View 时,单元格显示为空白?

ios - 当应用程序被用户强制退出时如何处理推送通知? [iOS]

ios - 过滤NSArray以制作新的NSArray以用于tableview方法

ios - 当 UICollectionView 滚动时 UIView 动画停止工作

python - Twisted 和 pyapns 的推送通知错误

ios - 无法通过 APNS 获得推送通知