iOS7 和 iOS8:如何检测用户何时拒绝推送通知请求 [英] iOS7 and iOS8: how to detect when user said No to a request for push notifications
问题描述
我正在构建一个面向 iOS7 和 iOS8 的应用程序.我请求用户允许发送推送通知.但出于某种原因,如果我在请求推送权限时单击否",则 iOS7 和 iOS8 都不会调用我的 application:didFailToRegisterForRemoteNotificationsWithError
处理程序.
I am building an app that targets iOS7 and iOS8. I ask the user for permission to send push notifications. But for some reason, neither iOS7 nor iOS8 ever calls my application:didFailToRegisterForRemoteNotificationsWithError
handler if I click "No" when asked for push permission.
我的问题:在 iOS7 和 iOS8 上,我如何知道用户何时拒绝了推送通知请求——我如何知道他们是否拒绝了请求?
My question: how do I know, on both iOS7 and iOS8, when the user has dismissed the request for push notifications--and how do I know if they denied the request?
我查看了一堆 StackOverflow 的答案并实施了他们的建议,但它没有按照文档中的说明工作:
I've looked at a bunch of StackOverflow answers and have implemented what they suggest, but it's not working as documented:
- iOS7:如果用户批准请求,系统确实调用我的
application:didRegisterForRemoteNotificationsWithDeviceToken:
处理程序.所以我可以说他们批准了它.但是,如果用户拒绝请求,则我不会收到对application:didFailToRegisterForRemoteNotificationsWithError
的回调.这似乎是一个错误,我无法判断用户拒绝了该请求. - iOS8:如果用户批准或拒绝请求,系统确实调用我的
application:didRegisterUserNotificationSettings
处理程序.我可以查看notificationSettings
参数以查看用户是批准还是拒绝了我的请求,这很方便.但是,如果我随后调用更新:如果我随后调用isRegisteredForRemoteNotifications()
(例如,当应用程序稍后激活时),它总是返回 true——即使用户拒绝了请求强>.所以我得到了一个误报.这似乎是一个错误,我看到 其他人已经注意到这一点好吧.let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
,我可以检查settings.types
查看是否允许警报.所以对于 iOS8,看起来我已经准备好了.
- iOS7: if the user approves the request, the system does call my
application:didRegisterForRemoteNotificationsWithDeviceToken:
handler. So I can tell that they approved it. But if the user denies the request then I don't get a callback toapplication:didFailToRegisterForRemoteNotificationsWithError
. This seems like a bug, and I can't tell that the user denied the request. - iOS8: if the user approves or denies the request, the system does call my
application:didRegisterUserNotificationSettings
handler. I can look at thenotificationSettings
parameter to see if the user approved or denied my request, so that's handy.However, if I subsequently callUpdate: if I subsequently callisRegisteredForRemoteNotifications()
(e.g., when the app becomes active later on), it always returns true--even if the user had denied the request. So I get a false positive. This seems like a bug and I see that others have noticed this as well.let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
, I can checksettings.types
to see if alerts are allowed. So for iOS8, it looks like I'm all set.
我正在使用 NSUserDefaults
布尔值来跟踪我是否已经要求用户授予权限.
I'm using an NSUserDefaults
boolean to keep track of whether I've already asked the user to grant permission.
我使用硬件设备进行测试(使用 iOS7 的 iPhone 4S 和使用 iOS8 的 iPhone 5),而不是模拟器.
I am using hardware devices for testing (iPhone 4S with iOS7 and iPhone 5 with iOS8), not a simulator.
我是 每次测试后重置我的设备,使其再次显示请求警报.
I am resetting my device after each test, to make it show the request alert again.
这是我注册推送通知的方法.if
分支在 iOS8 上采用,else
分支在 iOS7 上采用:
Here's how I register for push notifications. The if
branch is taken on iOS8 and the else
branch is taken on iOS7:
let application = UIApplication.sharedApplication()
if (application.respondsToSelector("registerUserNotificationSettings:")) {
let settings = UIUserNotificationSettings(forTypes: .Badge | .Alert | .Sound,
categories: nil )
application.registerUserNotificationSettings(settings)
} else {
application.registerForRemoteNotificationTypes(.Badge | .Alert | .Sound)
}
(在 iOS8 中,当 application:didRegisterUserNotificationSettings:
被调用时,我会调用 application.registerForRemoteNotifications()
).
(In iOS8, when application:didRegisterUserNotificationSettings:
is called, I then call application.registerForRemoteNotifications()
).
推荐答案
你的 didFailToRegisterForRemoteNotificationsWithError
方法没有被调用,因为注册没有失败——它甚至从未尝试过,因为用户拒绝了请求.
Your didFailToRegisterForRemoteNotificationsWithError
method isn't called, because the registration didn't fail - it was never even attempted because the user denied the request.
在 iOS7 上,您可以执行以下操作:
On iOS7 you can do a couple of things:
- 假设在调用
didRegisterForRemoteNotificationsWithDeviceToken
之前远程通知不可用 - 检查
UIApplication
对象上的enabledRemoteNotificationTypes
值
- Assume that remote notifications aren't available until
didRegisterForRemoteNotificationsWithDeviceToken
is called - Check the value
enabledRemoteNotificationTypes
on theUIApplication
object
这篇关于iOS7 和 iOS8:如何检测用户何时拒绝推送通知请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!