I used firebase to build My project.
It will also use the FCM (firebase cloud message).
But there is a problem.
I can't handle the FCM (create my custom notificaion) when app is in background.
The official site tutorial said that
case 1: App foreground -> override the "onMessageReceived()" to create your custom notification.
case 2: App background -> System will create the notification directly. We needn't and can't do anything. Because it doesn't trigger the "onMessageReceived()" in this case.
However if I can do nothing when app is background, I can't create my custom notification. (e.g. After Users click the notification and it will pop up a window to show detail information.)
So how do I handle notifications with FCM when app is in background?
You need to use FCM data messages in order to create custom notification in a android app.Even your app is in background,
onMessageReceived
will be called, so you can process the data and show the custom notification.https://firebase.google.com/docs/cloud-messaging/android/receive
Data message format which has to be sent from server:
FCM Won't send a background notification if your app is killed any more, and as you described in your answer about the
handleIntent()
solution It may work for some devices and for some old version of the FCM, also if you@override
method that doesn't described in the official doc's of firebase you may struggle some problems here, and you use it on your own risk!.What is the solution?
You need to use your own push-notification-service beside FCM like Telegram.
OR using SyncAdapter beside GCM like Gmail.
So if you need it to work successfully like those apps, you have to use your own hack.
There is a bad news.
Google change the Firebase source code in version 'com.google.firebase:firebase-messaging:11.6.0'.
handelIntent is "public final void method" now. which means we can't override it .
If you want to use the solution, change the version to be "com.google.firebase:firebase-messaging:11.4.2"
Try my way. It can perfectly work on the project build version is Android 6.0 above(api level 23) and I have tried it already.
There is better way than official site tutorial
The official site said that the notification will be created by system when app is in background. So you can't handle it by overriding the "onMessageReceived()". Because the "onMessageReceived()" is only triggered when app is in foreground.
But the truth is not. Actually the notificaions (when app is in background) are created by Firebase Library.
After I traced the firebase library code. I find a better way.
First, you need to create correct message payload that you send to fcm server. Example:
data
payload is actual data you want to show as message details after user clicks on notification,notification
payload represents how generated notification should look (there are much more attributes possible to set), you don't need to build notification by yourself, you only need to set it properties here.To show your activity after user taps on notication, you need to set intent filter corresponding to
click_action
:so activity that have above intent filter will be launched automatically when user taps to notification. Last step is to retrieve data when activity is launched after notification tap. It's pretty easy. Custom data is passed to activity via bundle. Inside
onCreate
method for your activity do something like that:All of above is valid if app is not running or it's in background. If your app is foreground, no notification will be created. Instead, you will receive
onMessageReceived()
event so you can handle the same data there (I guess you know how).Reference:
https://firebase.google.com/docs/cloud-messaging/http-server-ref https://github.com/firebase/quickstart-android/tree/master/messaging