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

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

问题描述

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



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





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



但是,如何检测用户点击开关不允许?我需要知道,所以我可以相应地将用户移动到下一个屏幕(并在注册后将它们带入第一个屏幕)。如果用户点击不允许,则不会调用函数 didFailToRegisterForRemoteNotificationsWithError



这个问题不重复,因为该问题的答案是针对iOS 7的,因为我的问题是具体的iOS 9。

解决方案

从iOS 8开始,通知注册流程发生变化,从必须授予权限的用户转移到远程通知。



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



您通过 - [UIApplication registerUserNotificationSettings:] <请求权限/ code> UIApplication 上的方法,根据文档,你得到回调 - [UIApplicationDelegate application:didRegisterUserNotificationSettings:] 委托方法。



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

  //这个回调将在调用时进行 -  [UIApplication registerUserNotificationSettings:]。用户授予应用程序的设置将作为第二个参数传入。 

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






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


返回值

如果应用程序已注册远程通知并收到其设备令牌,则为YES;如果未发生注册,失败或已被用户拒绝,则为NO。


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



https ://developer.apple.com/library/ios/documentation/UI套件/参考/ UIApplication_Class /#// apple_ref / occ / instm / UIApplication


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.

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)

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

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.

解决方案

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.

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

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天全站免登陆