0

I find the firebase doc about this subject too elusive: https://firebase.google.com/docs/cloud-messaging/android/receive#backgrounded

I've followed this answer : https://stackoverflow.com/a/47392189/2068732and and I'm able to catch a notification received when the app is in background.

However, it is not satisfactory because the WakefulBroadcastReceiver class is deprecated.

What is the proper way to forward the notification to the MyFirebaseMessagingService's onMessageReceived() method ?

Here is my MyFirebaseMessagingService class:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(getClass().getName(), "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(getClass().getName(), "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(getClass().getName(), "Message Notification Body: " + remoteMessage.getNotification().getBody());

            displayNotification(remoteMessage);
        }
    }
}

and manifest:

<service
    android:name=".firebase.MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
matdev
  • 4,115
  • 6
  • 35
  • 56

3 Answers3

0

Only if you have notification data in the payload remoteMessage.getNotification() != null will be true. If you have data payload then displayNotification() should be outside the if block

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(getClass().getName(), "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(getClass().getName(), "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(getClass().getName(), "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
        displayNotification(remoteMessage);
    }
}
Umang
  • 966
  • 2
  • 7
  • 17
  • I did that already and that's not enough. When the app is in backgroud, the onMessageReceived() method of my FirebaseMessagingService subclass does not get called – matdev Jan 15 '18 at 14:25
  • You might have missed something in the manifest or somewhere, sharing code would be helpful. Both the class and manifest – Umang Jan 15 '18 at 14:36
0

Please add the below permission. <uses-permission android:name="android.permission.WAKE_LOCK" /> for more info you can check this

Nirmal Dhara
  • 2,121
  • 18
  • 27
0

Please do check this and this.

In short, you should modify your fcm payload by removing the notification body from it, leaving out the data payload only.

Well I'm not really not quite sure which one is better, but in my case by disabling notification body, gave me more control over my notification.

nanangarsyad
  • 690
  • 14
  • 23