【问题标题】:Get deviceToken when user enable push notification manually when app is running in background当应用程序在后台运行时用户手动启用推送通知时获取 deviceToken
【发布时间】:2018-05-06 12:00:00
【问题描述】:

当应用程序在后台时,当用户从设置中手动启用推送通知令牌时,有没有办法获取推送通知令牌。 Swift 中是否有一个功能可以在用户返回应用程序时获取推送通知令牌。

【问题讨论】:

  • 您可以在不询问用户的情况下注册推送通知并接收令牌。授权请求是显示通知,而不是接收通知。
  • 不明白,所以我可以在不询问用户权限的情况下获得令牌。任何示例代码如何工作

标签: ios swift swift3


【解决方案1】:

您可以使用观察者进行系统设置更改。查看更多信息this thread

【讨论】:

  • 那么这将如何被调用和设置令牌呢? public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { UserDefaults.standard.set(deviceToken, forKey: "deviceToken") }
【解决方案2】:

在ios的常规设置中允许用户图灵推送通知时, 该应用程序可以调用

UIApplication.shared.registerForRemoteNotifications()

并且可以从

获取设备令牌
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

据我了解,没有任何直接的方法可以手动确定用户设置推送通知,所以我想出了两种替代方法

  1. 使用 NSNotification 中心观察者来确定任何设置更改,如已解释的 here
  2. 或在 Appdelegate 应用程序WillEnterForeground 中

调用波纹管函数(例如)

    func registerRemoteNotification() {
     let notificationCenter = UNUserNotificationCenter.current()
     notificationCenter.getNotificationSettings { (settings) in
        if settings.authorizationStatus == .authorized {
            //"authorized"
            DispatchQueue.main.async {
              UIApplication.shared.registerForRemoteNotifications()
             }
        } else {
          //"not authorized")
        }
      } 
    }

【讨论】:

    猜你喜欢
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多