TIM*_*MEX 12 objective-c ios swift
if UIApplication.sharedApplication().isRegisteredForRemoteNotifications() == true {
println("Yes, allowed")
println(UIApplication.sharedApplication().isRegisteredForRemoteNotifications())
}else{
//don't do shit
return
}
Run Code Online (Sandbox Code Playgroud)
当我进入设置以完全关闭通知然后返回应用程序时,应用程序仍然打印true,允许.
即使在卸载/重新安装应用程序之后,我似乎无法使其触发错误.
zei*_*sen 10
我为Swift 3做了一个扩展
extension UIApplication {
func remoteNotificationsEnabled() -> Bool {
var notificationsEnabled = false
if let userNotificationSettings = currentUserNotificationSettings {
notificationsEnabled = userNotificationSettings.types.contains(.alert)
}
return notificationsEnabled
}
}
Run Code Online (Sandbox Code Playgroud)
而不是使用它
UIApplication.shared.remoteNotificationsEnabled()
Run Code Online (Sandbox Code Playgroud)
我在https://github.com/onmyway133/notes/issues/219上有关于推送通知的说明
方案
这些是您可以通过的方案
isRegisteredForRemoteNotifications - UIApplication.shared.currentUserNotificationSettings
iOS 9.3.2+
API
从iOS 8+开始,Push Notification API已拆分为registerForRemoteNotifications和registerUserNotificationSettings.
所以当你打电话 registerForRemoteNotifications
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 叫做UIApplication.shared.isRegisteredForRemoteNotifications 返回true这意味着应用程序已收到推送令牌,并准备好接收推送通知.操作系统是否为您的应用程序提供推送取决于user notification settings用户切换的内容Settings
告诉我代码
检查是否启用了推送(表示用户可以看到推送消息)
static var isPushNotificationEnabled: Bool {
guard let settings = UIApplication.shared.currentUserNotificationSettings
else {
return false
}
return UIApplication.shared.isRegisteredForRemoteNotifications
&& !settings.types.isEmpty
}
Run Code Online (Sandbox Code Playgroud)
根据Apple 文档, 如果注册未发生、失败或被用户拒绝,isRegisteredForRemoteNotifications则会返回。如果应用程序已注册远程通知并已收到设备令牌,则将返回。因此,在回答您的问题“否”时,它不会总是返回“否”,如果您的应用程序已注册远程通知并且已收到设备令牌,它也会返回“是”。NOYES
\n\n\n返回值 如果应用已注册远程通知并\n 收到其设备令牌,则返回 YES;如果注册未发生、\n 失败或已被用户拒绝,则返回 NO。
\n\n讨论此方法仅反映调用 registerForRemoteNotifications 方法时开始的远程注册过程的成功完成。由于连接问题,此方法不反映远程通知是否实际可用。此方法返回的值考虑了用户接收远程通知的首选项。
\n
以上点返回到苹果文档中。
\n\n-----已编辑----------
\n\n您可以使用以下方式读取应用程序的权限
\n\nUIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; \nRun Code Online (Sandbox Code Playgroud)\n\n然后对不同类型执行任意操作以查看启用了哪些类型。您还可以致电unregisterForRemoteNotifications禁用通知。
- - -变化 - - -
\n\n\n\n\n\n
- isRegisteredForRemoteNotifications返回一个布尔值,指示应用程序当前是否注册\n以用于远程通知。
\n\n\n\n\n声明 SWIFT
\n\nfunc isRegisteredForRemoteNotifications() -> Bool目标-C\n
\n- (BOOL)isRegisteredForRemoteNotifications返回值 如果应用已注册远程通知并\n 收到其设备令牌,则返回 YES;如果注册未发生、\n 失败或已被用户拒绝,则返回 NO。
\n\n讨论 此方法仅反映调用 registerForRemoteNotifications 方法时开始的远程注册过程的成功完成。由于连接问题,此方法不反映远程通知是否实际可用。此方法返回的值考虑了用户接收远程通知的首选项。
\n
| 归档时间: |
|
| 查看次数: |
8136 次 |
| 最近记录: |