Solve mismatch sender id issue when migrating from GCM to FCM

Migrating from GCM to FCM

How to do it when GCM and FCM are running on different Google cloud projects?

--

PRELUDE

We started using Google’s cloud messaging in 2013. To engage our users better, we build an array of features on top of GCM. Our push system is capable of sending notifications based on location, time of day, special occasions, user actions and more. We could also send messages to different user segments based on location, language, usage patterns, food preferences etc.

Firebase

Riafy was an early adopter of Firebase. Cookbook’s integration with Firebase on remote configuration, smart lock and custom on-boarding flow was showcased in Google I/O 2016. But our work with Firebase messaging has been limited. This was because we had a GCM based messaging system that had all the capabilities of FCM.

GCM’s deprecation and our move to FCM

Now that GCM has been deprecated, we’ve finally moved to FCM. This is Google’s guide to migrate from GCM to FCM. The steps are pretty straightforward. But we hit a ROADBLOCK.

GCM and FCM on different Google Cloud Projects

We had been running GCM on our first cloud project created in 2013. Since we started working with Google, all our enhancements have been on a different cloud project; one we created for Google Now integration. So, Firebase was also in that project.

This presented a very tricky situation. FCM and GCM had different sender ids. So, we had to use one token for sending messages to our old users on GCM and another one to send messages to new users on FCM.

{"multicast_id":xxxx,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}

What we wanted was to send messages on one key, and receive messages across our entire user base. There was no documented method to do this on Firebase. After several hours of digging, we found a solution.

FirebaseInstanceId.getInstance().getToken(“old-sender-id”, “FCM”)

The above line of code helps to generate a token from the old sender id (from different could project) and use that to receive messages via FCM.

NOTE: Firebase messaging is based on notification channels. So you need to specify a notification channel when sending the messages. Otherwise your messages won’t be displayed when targeting Android Oreo and above after migration from GCM.

Hope this article makes your migration from GCM to FCM a lot smoother. Good luck moving to FCM. If you loved the story, please hit the 👏 clap button and send it across to your fellow Android developers.

--

--