-->

How to use an existing GCM token within another Fi

2019-01-17 18:55发布

问题:

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?

回答1:

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.