即使我完全禁用了isRegisteredForRemoteNotifications也会返回true

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)

  • 谢谢!我在同一行写了相同的代码:`return currentUserNotificationSettings?.types.contains(.alert)?? FALSE` (3认同)
  • 我宁愿检查这三个之一是否已启用 `(.alert || .badge || .sound)` (2认同)

onm*_*133 6

我在https://github.com/onmyway133/notes/issues/219上有关于推送通知的说明

方案

这些是您可以通过的方案

isRegisteredForRemoteNotifications - UIApplication.shared.currentUserNotificationSettings

iOS 9.3.2+

  • 没有触发推送请求对话框:false - none
  • 根据权限请求启用推送应用:true - 警报,徽章,声音
  • 关闭app app:true - none
  • 启用推送,仅启用警报:true - 警报
  • 再次卸载并安装(24小时内):false - none
  • 拒绝请求许可请求:真实 - 没有
  • 拒绝后重新启用:真实 - 警报,徽章,声音

API

从iOS 8+开始,Push Notification API已拆分为registerForRemoteNotificationsregisterUserNotificationSettings.

所以当你打电话 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)


Nim*_*ekh 3

根据Apple 文档, 如果注册未发生、失败或被用户拒绝,isRegisteredForRemoteNotifications则会返回。如果应用程序已注册远程通知并已收到设备令牌,则将返回。因此,在回答您的问题“否”时,它不会总是返回“否”,如果您的应用程序已注册远程通知并且已收到设备令牌,它也会返回“是”。NOYES

\n\n
\n

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

\n\n

讨论此方法仅反映调用 registerForRemoteNotifications 方法时开始的远程注册过程的成功完成。由于连接问题,此方法不反映远程通知是否实际可用。此方法返回的值考虑了用户接收远程通知的首选项。

\n
\n\n

以上点返回到苹果文档中。

\n\n

-----已编辑----------

\n\n

您可以使用以下方式读取应用程序的权限

\n\n
UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; \n
Run Code Online (Sandbox Code Playgroud)\n\n

然后对不同类型执行任意操作以查看启用了哪些类型。您还可以致电unregisterForRemoteNotifications禁用通知。

\n\n

- - -变化 - - -

\n\n
\n

- isRegisteredForRemoteNotifications

\n\n

返回一个布尔值,指示应用程序当前是否注册\n以用于远程通知。

\n\n
\n

声明 SWIFTfunc isRegisteredForRemoteNotifications() -> Bool

\n\n

目标-C\n - (BOOL)isRegisteredForRemoteNotifications

\n
\n\n

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

\n\n

讨论 此方法仅反映调用 registerForRemoteNotifications 方法时开始的远程注册过程的成功完成。由于连接问题,此方法不反映远程通知是否实际可用。此方法返回的值考虑了用户接收远程通知的首选项。

\n
\n

  • iOS 8 中已弃用“UIRemoteNotificationType”,而是使用应用程序的“currentUserNotificationSettings”“types”(这是新的“UIUserNotificationType”枚举)。 (4认同)
  • 如何更改我的代码以便我知道“用户何时禁用通知”? (2认同)