使用 Firebase 进行通知,获取 `app instance has been unregistered`

分享于2023年04月22日 firebase firebase-admin firebase-cloud-messaging go 问答
【问题标题】:Using Firebase for notifications, getting `app instance has been unregistered`使用 Firebase 进行通知,获取 `app instance has been unregistered`
【发布时间】:2023-04-17 05:45:02
【问题描述】:

尝试将 Firebase 用于跨平台移动通知。获取设备令牌的客户端代码似乎可以工作,因为我们可以从 Firebase Web 控制台发送通知。但我不知道如何从我们的服务器发送通知。我们得到错误: app instance has been unregistered; code: registration-token-not-registered; details: Requested entity was not found 在以下 Go 代码 sn-p 中调用 messagingClient.Send 时:

    ctx := r.Context()
    opt := option.WithCredentialsFile("serviceAccountKey.json")
    app, err := firebase.NewApp(context.Background(), nil, opt)
    if err != nil {
      return err
    }

    // Obtain a messaging.Client from the App.
    messagingClient, err := app.Messaging(ctx)
    if err != nil {
      return err
    }
    // See documentation on defining a message payload.
    message := &messaging.Message{
      Notification: &messaging.Notification{
        Title: "Notification title",
        Body:  "Notification body",
      },
      Token: myDeviceToken,
    }

    // Send a message to the device corresponding to the provided
    // registration token.
    response, err := messagingClient.Send(ctx, message)
    if err != nil {
      return err
    }

我们使用的凭据文件来自 Firebase 控制台,位于 Settings、Service Accounts、Firebase Admin SDK 下。


【解决方案1】:

根据 documentation for FCM error codes ,registration-token-not-registered的意思是:

提供的注册令牌未注册。以前有效的 注册令牌可以由于多种原因被注销, 包括:

  • 客户端应用程序已从 FCM 中取消注册。
  • 客户端应用程序已自动取消注册。如果用户卸载应用程序,或者在 iOS 上,如果 APNS 反馈 服务报告 APNS 令牌无效。
  • 注册令牌已过期。例如,Google 可能决定刷新注册令牌,或者 iOS 的 APNS 令牌可能已过期 设备。
  • 客户端应用已更新,但新版本未配置为接收消息。

对于所有这些情况,请移除此注册令牌并停止使用它 发送消息。

鉴于这里有多种可能性,我们真的不可能说出它是哪一种。如果您需要 FCM 故障排除方面的帮助,请 contact Firebase support directly 提供任何人都可以采取的确切步骤来重现该问题。

【讨论】: