Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an existing GCM token within another Firebase project?

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?

like image 398
Arun Venkatesan Avatar asked Jun 03 '16 20:06

Arun Venkatesan


People also ask

Does FCM registration token change?

Similarly to how GCM works, the FCM token can change due to token-rotation. Note: the token rotation is a rare-event. Don't expect to see it often. But still, if you care about the token you should implement onTokenRefresh() to be informed of changes.

Is GCM and FCM same?

Firebase Cloud Messaging(FCM), was formerly known as Google Cloud Messaging(GCM). FCM is a cloud platform that provides messages and push notifications for operating systems- ios and Android, and websites as well.

How do I clone a Firebase project?

There is currently no way (neither through the Console or through an API) to create a project that is a clone of another project. At the moment you will have to re-create the config data in the new project manually. This user is first on the weekly Google Cloud leaderboard.


1 Answers

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.

like image 51
Diego Giorgini Avatar answered Oct 21 '22 08:10

Diego Giorgini