iOS7和iOS8上:当用户说不推送通知的要求如何检测 [英] iOS7 and iOS8: how to detect when user said No to a request for push notifications

查看:192
本文介绍了iOS7和iOS8上:当用户说不推送通知的要求如何检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了针对iOS7和iOS8上的应用程序。我询问用户许可发送推送通知。如果我点击否时,要求推许可didFailToRegisterForRemoteNotificationsWithError 处理程序:但由于某些原因,既不iOS7也不iOS8上不断叫我的应用程序。

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?

我看了一堆计算器的答案,并实施了他们建议的东西,但它不工作的记录:

I've looked at a bunch of StackOverflow answers and have implemented what they suggest, but it's not working as documented:


  • iOS7:如果用户批准了请求,系统的确实的叫我的应用程序:didRegisterForRemoteNotificationsWithDeviceToken:处理程序。所以,我可以告诉大家,他们同意了。但是,如果用户拒绝该请求然后点击我没有得到一个回调应用程序:didFailToRegisterForRemoteNotificationsWithError 。这似乎是一个错误,我不能告诉用户拒绝了这一要求。

  • iOS8上:如果用户批准的或拒绝的要求,系统的确实的叫我的应用程序:didRegisterUserNotificationSettings 处理程序。我可以看看 notificationSettings看参数,如果用户批准或拒绝我的要求,所以这是得心应手。 <击>但是,如果我继续调用 isRegisteredForRemoteNotifications()(例如,当应用程序被激活以后),它总是返回true - 即使用户已经拒绝了这一要求。所以,我得到一个假阳性。这似乎是一个错误,我看到<一个href=\"http://stackoverflow.com/questions/25017429/delayed-push-notifications-and-checking-if-user-enabled-it\">others已经注意到这个问题,以及 更新:如果我继续调用让设置= UIApplication.sharedApplication()currentUserNotificationSettings(),我可以检查<。 code> 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 to application: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 the notificationSettings parameter to see if the user approved or denied my request, so that's handy. However, if I subsequently call isRegisteredForRemoteNotifications() (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. Update: if I subsequently call let settings = UIApplication.sharedApplication().currentUserNotificationSettings(), I can check settings.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的和iPhone 5与iOS8上),而不是一个模拟器。

I am using hardware devices for testing (iPhone 4S with iOS7 and iPhone 5 with iOS8), not a simulator.

我是<一个href=\"https://developer.apple.com/library/ios/technotes/tn2265/_index.html#//apple_ref/doc/uid/DTS40010376-CH1-TNTAG42\">resetting每次测试后我的设备,使之再次显示请求警报。

I am resetting my device after each test, to make it show the request alert again.

下面是我如何注册推送通知。在如果分支采取iOS8上和其他分支采取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上,当应用程序:didRegisterUserNotificationSettings:叫,我然后调用 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:


  1. 假设远程通知才会可用 didRegisterForRemoteNotificationsWithDeviceToken 被称为

  2. 检查值 enabledRemoteNotificationTypes 的UIApplication 对象

  1. Assume that remote notifications aren't available until didRegisterForRemoteNotificationsWithDeviceToken is called
  2. Check the value enabledRemoteNotificationTypes on the UIApplication object

这篇关于iOS7和iOS8上:当用户说不推送通知的要求如何检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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