如何在另一个Firebase项目中使用现有的GCM令牌? [英] How to use an existing GCM token within another Firebase project?

查看:139
本文介绍了如何在另一个Firebase项目中使用现有的GCM令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我创建了一个独立的Firebase项目F2,不需要将其导入到GCP1。我也发布F2生产。 (或者,我已经从现有的firebase.com项目中将F2导入新的Firebase控制台)。

我使用后端服务器发送推送通知。当我向F2项目发送一个通过GCP1生成的GCM令牌的推送时,由于客户端ID不正确,它自然会失败。是否有解决方法来启用在F2中为P1生成的GCM令牌? 当从后端服务器发送消息时,需要使用与项目关联的API-KEY对请求进行身份验证( 用于生成GCM / FCM令牌。

由于安全限制,这里没有解决方法。



对于现有的GCM用户最佳迁移包括将旧项目导入Firebase控制台。这将允许您定位旧客户端和新客户端,因为 sender-id 不会更改

步骤:https://developers.google.com/cloud-messaging/android/android-migrate-fcm



如果这不是选项(您已经创建了一个与以前的Google Cloud Project截然不同的新Firebase项目),那么您有两种可能性: b

$ b

  • 更简单和推荐的方法:更改您的后端以存储哪个客户端始发gcm / fcm标记。然后在从后端发送消息时使用正确的API-KEY。 (与旧客户端的旧项目关联的API-KEY,以及使用新Firebase项目的新客户端的新API-KEY)。

  • $ b $如果在FCM中根本无法更改后端,则可以使用API​​为旧的SenderID创建额外的令牌:

    code> FirebaseInstanceId.getInstance()。getToken(old-sender-id,FCM)

    因为这个令牌与old-sender-您的后端将能够使用旧项目的API-KEY向其发送消息。

    注意:这不会影响基于Firebase控制台在新的发件人身份证。
    该控制台将只能定位到新的客户端,包括firebase sdk和相关的google_services.json文件。



    Lets say I have Google Cloud Project (GCP1) with GCM turned on with Client id P1.

    Now I have created a standalone Firebase project F2 WITHOUT importing it to GCP1. I have also released F2 to production. (Alternatively, I have imported F2 from an existing firebase.com project into the new Firebase console).

    I use a backend server to send push notifications. When I send a push to a GCM token generated via GCP1 from the F2 project, it fails (naturally) because of incorrect client ID. Are there work-arounds to enable use of GCM tokens generated for P1 within F2?

    解决方案

    When sending messages from your back-end server, you need to authenticate the request with the API-KEY associated with project (sender-id) used to generate the GCM/FCM token.
    Due to security restrictions here are no workaround for this.

    For existing GCM users the best migration consists in importing the old project into the Firebase Console. This will allow you to target both old and new client, since the sender-id will not change
    Steps here: https://developers.google.com/cloud-messaging/android/android-migrate-fcm

    If that is not option (you have already created a new Firebase Project distinct from the previous Google Cloud Project) you have two possibilities:

    1. Easier and recommended approach: change your back-end to store which client originated the gcm/fcm token. Then use the correct API-KEY when sending messages from your back-end. (the API-KEY associated to the old project for old clients, and the new API-KEY for new clients that are using the new Firebase project).

    2. If you cannot change your back-end at all: in FCM you can create an additional token for the old SenderID, using the API:
      FirebaseInstanceId.getInstance().getToken("old-sender-id", "FCM")
      Because this token is associated to the old-sender-id your back-end will be able to send messages to it using the API-KEY of the old project.
      Note: this doesn't affect the Firebase Console which is based on the new-sender-id.
      That console will be able to target only the new clients that are including the firebase sdk and the associated google_services.json file.

    这篇关于如何在另一个Firebase项目中使用现有的GCM令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆