Reading between the lines — FCM for Android(Part II)

Abhishek Tiwari
2 min readDec 7, 2017

--

So, in the the first part I concluded how sending a “data” type message through FCM API can save you from the trouble of handling background and foreground push notification cases!
Well, it surely works but this is not the best solution!
Q.
But why? it solved my problem seamlessly!
A. This approach does work, and solve this peculiar case, but it will only work seamlessly for android and not for iOS.

Sending a “data” message through API enables the developer to intercept received message inside OnMessageReceived method in android for all cases , but in iOS “data” type notifications aren’t intercepted by APNS and hence will not show in notification tray. Now this is a problem.

Having said this, there are still ways to handle this situation, namely, asking your iOS guy to handle the data message he has received internally and create his own notification(he won’t agree, trust me on that!) or to send different types of messages for android and iOS, by asking your backend guy to handle FCM tokens he has saved along with the device type(android or iOS) against it and then send “notification” type message for iOS and “data” type for android.

The mentioned workarounds are, not very feasible and also even with android only app , it may happen that you are forced to use FCM panel for push notification generation, in that case you’re stuck with a “notification” message.

Solution — Stop avoiding notification messages!

Yes, if we sum up all cases, we would realise that in some way or the other we will be forced to use notification message.
But then what am i supposed to do with background and foreground messages?????
The answer is simple — HANDLE IT!!!

Handling background messages

So we do know that foreground messages are intercepted inside OnMessageReceived method and frontend messages are handled by FCM to generate automatic push notification. So, now we only need to know how to customise FCM automatic notification so, it looks exactly like the one made using NotificationBuilder inside OnMessageReceived.

I’ll be creating a new story for the purpose of handling automatically generated push notification.
You can read it here.

--

--

Abhishek Tiwari

Android developer. Python enthusiast. IoT newbie.Open Source Contributor.Anime buff.