How I migrated to FCM from GCM/APN

ADMAT Bandara
4 min readMay 31, 2019

--

How you doing ? ;)

I am writing this article based on my personal experience I faced while migrating from GCM/APN to FCM.

Related development goes on top of Ruby on Rails ( I just ❤ it ).

GCM — Google Cloud Messaging

APN — Apple Push Notification

FCM — Firebase Cloud Messaging

This is what we had

Push notification Architecture

Talking about above…

We have used the gem Rpush to handle this notification mechanism and two different classes to handle push notifications for iOS/Android separately with GCM and APN. So when the notify method executes we save a record in the Rpush:Notification with the specific class. And the Rest of the process is with the Rpush. It runs as a separate service and sends notifications to the related authority (GCM or APN)

Once Google announce about the migration of GCM to FCM and this FCM supports all device notifications. We were trilled and exited to do the upgrade.

Migrating process starts…

We searched for the libraries we can use first.

  1. Rpush (Same we used)
  2. FCM
  3. Write our own class to the process

Anyway :D , I tried all of the above. Here I write about the 3rd way. Because other libraries has there own documentations which is pretty good :)

Step 01: Create the FCM project

you can use this documentation from Firebase and you’ll find the Server Key.

Step 02: Creates a lib file to handle the FCM service

I am creating that file fcm_sender.rb

this ruby class will be a simple class which has the ability to call external url and grab the response with the provided details.

Basically we to grab the response from the FCM API we need

  1. authentication token
  2. content to be send

Authorization key is the Server ID from the Firebase console. I am going to save it in the application.yml file and Environment Variable. So later i could access it using

ENV['FCM_AUTH_KEY']

Content is the JSON formatted body we should send with the service call.

Generally the request will look like this.

URL:  https://fcm.googleapis.com/fcm/sendheader: {
'Content-Type': 'application/json',
'Authorization': "key=#{@fcm_token}"
}
data: {
'data': data_in_JSON
}

This is my fcm_sender lib file

fcm_sender.rb

Though it has different formats in the content to be send in the body for different operating systems (iOS/Android) I am maintaining two classes for that. I actually said this earlier.

Apple content format

Android content format

Step 03: Create classes to handle both Android/iOS

Here is the class diagram of the notification management.

AppleDevice.rb and AndroidDevice.rb are also some basic classes which is being used to have separate data formats when generating the FCM request.

Here is the class which handles the notification format for the iOS devices.

And same like above I have managed another for the android as well.

Based on the response, I wanted to remove the invalid devices. Thanks to Firebase, they will send that information in the response.

Step 04: Handle the response

All response details can be found in the following url

Here is the response I got from my class.

Success Firebase response be like

{"multicast_id":6953102881003652604,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":""}]}

Error Firebase response be like

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

Under “error” we can identify the invalid or not registered devices.

In our project we saved the device token in the database, so I wrote a common function inside the Device class for this functionality.

Things to Remember

  1. Once you change the application.yml to add the ENV[‘FCM_AUTH_KEY’], you have to restart the rails server or redeploy.
  2. If you are using rpush for this mechanism, you should restart the rpush services too. ( remove the rpush.pid and reinitialize the service again

or else, kill the rpush services. for that first search for rpush

ps aux | grep rpush

kill them

kill -9 the-pid-number

Problems I face

  1. Constant error message saying “MismatchSenderID” in the response

If you get “MismatchSenderID” as the response error message, no matter the changes you are doing and if you are crazy about this error message.. Like I was :D :D :D

Just change the SERVER KEY in Firebase console and reinitialize.

Anyway… This how I successfully migrated form GCM/APN to FCM.

This works for me. If you have any feedback regarding the “MismatchSenderID” issue let me know.

Happy coding :)

--

--