iOS 9:如何检测用户何时对推送通知请求说“不允许"? [英] iOS 9: How to detect when user said 'Don't Allow' to the push notification request?

查看:23
本文介绍了iOS 9:如何检测用户何时对推送通知请求说“不允许"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In iOS 9, is there a system level callback I can read which tells me whether the user has tapped on 'Don't allow' on the push notification request?

我用一个自定义屏幕提示用户,通知他们推送通知及其在我的应用程序中的价值.

I prompt the user with a custom screen informing them about push notifications and the value it has in my app.

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

They have two choices, yes or no. If they select Yes, I request the operating system for push notification and they see a pop up like the image below.

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

Now, if the user taps on YES, then there is a function called didRegisterForRemoteNotificationsWithDeviceToken which tells me the this device has been registered for push notifications. I can use this to move ahead to the next screen (and take them into the first screen after signing up)

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

However, how do I detect if the user taps on DON'T allow? I need to know that so I can accordingly move the user to the next screen (and take them into the first screen after signing up). The function didFailToRegisterForRemoteNotificationsWithError is not called if the user taps on 'Don't Allow'.

此问题不是重复的,因为该问题接受的答案特定于 iOS 7,而我的问题特定于 iOS 9.

This question is not a duplicate because the answer accepted for that question is specific to iOS 7, where as my question is specific is iOS 9.

推荐答案

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

As of iOS 8, the notification registration process changed and moved away from the user having to grant permission to just remote notifications.

您现在可以在技术上注册远程通知,而无需获得用户的许可.您需要许可的是用户通知设置(警报、声音和徽章).这些现在适用于本地和远程通知,使其他答案在技术上不正确.

You can now technically register for remote notifications without having to get permission from the user. What you do need permission for is the user notification settings (alerts, sounds and badges). These are now generic to both local and remote notifications making the other answers technically incorrect.

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

You request permission via the -[UIApplication registerUserNotificationSettings:] method on UIApplication and as per the documentation, you get a callback to the -[UIApplicationDelegate application: didRegisterUserNotificationSettings:] delegate method.

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

In the header, there is a comment saying the following:

// 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.

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

This means that if the user did not grant permissions for notifications (both local and remote) then the second parameter won't contain any values.

-[UIApplication isRegisteredForRemoteNotifications] 只会告诉你应用程序是否真的在 Apple 的推送服务器上注册并收到了设备令牌:

-[UIApplication isRegisteredForRemoteNotifications] will just tell you if the applicaiton has actually registered with Apple's push servers and has received a device 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 文档,因为它包含您需要的所有信息.

It's worth reading the UIApplication documentation as it has all the info you need.

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

这篇关于iOS 9:如何检测用户何时对推送通知请求说“不允许"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆