- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在构建一个针对 iOS7 和 iOS8 的应用。我请求用户允许发送推送通知。但出于某种原因,iOS7 和 iOS8 都没有调用我的 application:didFailToRegisterForRemoteNotificationsWithError
处理程序,如果我在请求推送权限时单击“否”。
我的问题:在 iOS7 和 iOS8 上,我怎么知道用户何时拒绝了推送通知请求——我怎么知道他们是否拒绝了请求?
我查看了一堆 StackOverflow 答案并实现了他们的建议,但它没有按照记录工作:
application:didRegisterForRemoteNotificationsWithDeviceToken:
处理程序。所以我可以说他们批准了它。但是,如果用户拒绝请求,那么我不会收到 application:didFailToRegisterForRemoteNotificationsWithError
的回调。
。这似乎是一个错误,我无法判断用户是否拒绝了请求。application:didRegisterUserNotificationSettings
处理程序。我可以看看 notificationSettings
参数来查看用户是否批准或拒绝了我的请求,这样很方便。
isRegisteredForRemoteNotifications()
(例如,当应用稍后激活时),
它总是返回 true——即使用户拒绝了请求。所以我得到了一个误报。这似乎是一个错误,我看到了
others have noticed this as well .
let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
, 我可以检查 settings.types
查看是否允许警报。所以对于 iOS8,看起来我已经准备好了。我正在使用 NSUserDefaults
用于跟踪我是否已请求用户授予权限的 bool 值。
我正在使用硬件设备进行测试(iOS7 的 iPhone 4S 和 iOS8 的 iPhone 5),而不是模拟器。
我是resetting my device after each test , 使其再次显示请求警报。
这是我注册推送通知的方法。 if
分支在 iOS8 和 else
上采用分支是在 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()
)。
最佳答案
我正试图找到解决同一问题的方法。
当显示推送通知权限 UIAlert 时,它是从我的应用程序外部显示的。一旦用户选择了“不允许”或“确定”,我的应用就会再次激活。
在提示用户推送权限之前,我有一个要呈现的 View Controller 。在这个 View Controller 中,我监听 UIApplicationDidBecomeActiveNotification
,然后关闭我的 View Controller 。
此方法运行良好,而我见过的所有其他解决方案对我来说根本不起作用。
关于iOS7 和 iOS8 : how to detect when user said No to a request for push notifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161645/
我是一名优秀的程序员,十分优秀!