在 didRegisterForRemoteNotificationsWithDeviceToken 中未收到设备令牌

问题描述 投票:0回答:1

在我的 AppDelegate 中,我使用

didRegisterForRemoteNotificationsWithDeviceToken
方法。但是,尽管在日志中下面的代码中看到了成功消息,但我没有看到设备令牌被打印或保存到 UserDefaults 中,如方法中所写。

class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate  {
  func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    FirebaseApp.configure()
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) {(success, error) in
      if success {
        print("Success in APNS registry")
          DispatchQueue.main.async {
            application.registerForRemoteNotifications()
          }
      } else {
        print("error \(error?.localizedDescription ?? "error")")
      }
    }
    NetworkMonitor.shared.startMonitoring()
    return true
  }

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    let userDefaults = UserDefaults.standard
    print("Token is \(tokenString)")
    userDefaults.set(tokenString, forKey: "deviceToken")
  }
}

我已经尝试了几次,从手机中删除该应用程序并重新安装。每次我接受提示时,我都会看到成功消息,但没有打印令牌。在我使用 Firebase GCM 之前,但我现在走上了不同的道路,并且不再想使用它。我不确定是否有一些配置需要删除,日志中没有任何内容,但说我需要删除它。

ios swift apple-push-notifications
1个回答
0
投票

我已经在这个问题上停留了几个小时了。最后我意识到这是因为 Firebase 完成的 method swizzling (即使我不使用云消息传递)。为了解决这个问题,您应该添加

FirebaseAppDelegateProxyEnabled = NO (boolean)

在您的 Info.plist 文件中。

© www.soinside.com 2019 - 2024. All rights reserved.