FCM 错误 400 - 注册令牌不是有效的 FCM 注册令牌

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

我们有一个离子/电容器应用程序,一直在使用 Firebase FCM 进行推送通知,一切正常。我们刚刚更新到 Capacitor 6 并重建了应用程序,重新安装了 @capacitor/push-notifications 插件并配置了各种项目文件。

Android 上一切都运行良好。但在 iOS 上,我们在尝试发送推送通知时收到以下错误:

HTTP/1.1 400 Bad Request
Vary: Origin
Vary: X-Origin
Vary: Referer
Content-Type: application/json; charset=UTF-8

{
"error": {
"code": 400,
"message": "The registration token is not a valid FCM registration token",
"status": "INVALID_ARGUMENT",
"details": [
  {
    "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
    "errorCode": "INVALID_ARGUMENT"
  }
]
}
}

服务器端发送通知的方式没有任何不同,因此我们的 iOS 配置显然有问题(因为它在 Android 上运行良好)。考虑到这一点,以下是我们在 xCode 中完成的文件配置。

在 Podfile 中:

target 'App' do
  capacitor_pods
  # add your pods here
  pod 'Firebase/Messaging'
end

在 AppDelegate 中:

// Push notifications
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDeletage {

func application(_ application: UIApplication, didFinishLaunchingWithOptions
    launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch

    // Push Notifications
    FirebaseApp.configure()

    return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
   NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}

当在本地设备上从 xCode 运行应用程序时,它似乎工作正常,并在应用程序注册并获取令牌时记录以下内容:

[log] FCM - register: granted
[log] FCM - registration: {"value":"7A868F9E49853DA0F175ABDCE683310CC279306B4B00B579DA94690273CE7A3A"}

当检查服务器端以确保使用相同的令牌(几秒钟前获得的)时,它确实是相同的。

我错过了什么?

ios firebase firebase-cloud-messaging
1个回答
0
投票

经过大量尝试、错误和研究,我能够确定 https://capacitorjs.com/docs/apis/push-notifications 上针对 iOS 配置的官方 @capacitor/push-notifications 文档是错误的。具体来说,AppDelegate.swift 的附加代码(如该插件早期版本所述,运行良好)不起作用,因为当前功能实际上检索 Apple APN 令牌。

这是文档中不起作用的代码:

func application(_ application: UIApplication, 
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  NotificationCenter.default.post(name: 
.capacitorDidRegisterForRemoteNotifications, object: deviceToken)
}

func application(_ application: UIApplication, 
didFailToRegisterForRemoteNotificationsWithError error: Error) {
  NotificationCenter.default.post(name: 
.capacitorDidFailToRegisterForRemoteNotifications, object: error)
}

改为使用以下内容:

func application(_ application: UIApplication, 
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Messaging.messaging().apnsToken = deviceToken
  Messaging.messaging().token(completion: { (token, error) in
    if let error = error {
        NotificationCenter.default.post(name: 
.capacitorDidFailToRegisterForRemoteNotifications, object: error)
    } else if let token = token {
        NotificationCenter.default.post(name: 
.capacitorDidRegisterForRemoteNotifications, object: token)
    }
  })
}

func application(_ application: UIApplication, 
didFailToRegisterForRemoteNotificationsWithError error: Error) {
  NotificationCenter.default.post(name: 
.capacitorDidFailToRegisterForRemoteNotifications, object: error)
}

这将检索正确的令牌。希望这可以节省其他人一些时间!

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