There is bad news.
Google will change the source code of Firebase to 'com.google.firebase: firebase-messaging: 11.6.0'.
handelIntent is now the "public final void method". which means we cannot redefine it.
If you want to use the solution, change the version to "com.google.firebase: firebase-messaging: 11.4.2"
Try your way. It works great on the version for building the project - Android 6.0 (api level 23), and I already tried this.
There is a better way than the official tutorial website
The official website said that a notification will be generated by the system when the application is in the background. Thus, you cannot handle this by overriding "onMessageReceived ()". Because "onMessageReceived ()" is launched only when the application is in the foreground.
But there is no truth. In fact, notifications (when the application is in the background) are generated by the Firebase library.
After I traced the firebase library code. I find the best way.
Step 1. Cancel "handleIntent ()" instead of "onMessageReceived ()" in FirebaseMessagingService
why:
Since the method will be launched, either the application is in the foreground or in the background. Thus, we can process the FCM message and create our custom notifications in both cases.
@Override public void handleIntent(Intent intent) { Log.d( "FCM", "handleIntent "); }
Step 2. Parse the message from FCM
as:
If you do not know the format of the message that you set. Print it and try to parse it.
Here is a basic illustration
Bundle bundle = intent.getExtras(); if (bundle != null) { for (String key : bundle.keySet()) { Object value = bundle.get(key); Log.d("FCM", "Key: " + key + " Value: " + value); } }
Step 2. Delete notifications created by the Firebase library when the application is in the background
why:
We can create our custom notification. But the notification generated by the Firebase library will still be (actually it was created by โsuper.handleIntent (intent)". The following is a detailed explanation.). Then we will have two signals. This is rather strange. Therefore, we need to delete the notification created by the Firebase library
how (project build level - Android 6.0 above):
Recognize the notifications we want to remove and receive information. And use "notificationManager.cancel ()" to remove them.
private void removeFirebaseOrigianlNotificaitons() { //check notificationManager is available NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager == null ) return; //check api level for getActiveNotifications() if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { //if your Build version is less than android 6.0 //we can remove all notifications instead. //notificationManager.cancelAll(); return; } //check there are notifications StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications(); if (activeNotifications == null) return; //remove all notification created by library(super.handleIntent(intent)) for (StatusBarNotification tmp : activeNotifications) { Log.d("FCM StatusBarNotification", "StatusBarNotification tag/id: " + tmp.getTag() + " / " + tmp.getId()); String tag = tmp.getTag(); int id = tmp.getId(); //trace the library source code, follow the rule to remove it. if (tag != null && tag.contains("FCM-Notification")) notificationManager.cancel(tag, id); } }
All my sample code is:
public class MyFirebaseMessagingService extends FirebaseMessagingService { private static int notificationCount=0; @Override public void handleIntent(Intent intent) {