0
votes

As instructed in Firebase dev docs, I've implemented a Service that extends FirebaseMessagingService and overrides the onMessageReceived callback. I have put a Log message in the first line inside the onMessageReceived method.

App running in background I don't see the log in logcat but I see a Notification posted in the system try.

App in Foreground I neither see the log nor the notification in system tray

Any idea what's going on?

Manifest

   <service
        android:name=".fcm.MovieMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

Service Class

public class MovieMessagingService extends FirebaseMessagingService {

    private static final String LOG_TAG = MovieMessagingService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


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

    }

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody) {
        Log.d(LOG_TAG, "Presenting Notification with message body: " + messageBody);
//more code
    }
}
2
add your service class code and manifest code to the questionShubhank
is the path correct for the service (package name)Shubhank
Yes, else the app won't even compile. As you can see, I am using a relative path in manifest- ".fcm.MovieMessagingService"Sai
How are you sending the message to the device?Arthur Thompson
From Firebase console. console.firebase.google.com -> Notifications->New MessageSai

2 Answers

2
votes

Actually, app's behavior, while receiving messages including both notification and data payloads, depends on whether the app is in the background or the foreground like:

When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.

When in the foreground, your app receives a message object with both payloads attached.

So, The summary is when app is in background, you can see the notification in system tray and can't see any log until tapping on the notification but you will see only the opening activity log not the service log as it's already executed.

And when the app in foreground you can see the log in logcat but you can't see any notification in the system tray as your app already open state you will receive only data payloads.

1
votes

Here is a code example of how to receive messages and how to handle the different types. Here is the source of the code.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // [START_EXCLUDE]
    // There are two types of messages data messages and notification messages. Data messages are handled
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
    // is in the foreground. When the app is in the background an automatically generated notification is displayed.
    // When the user taps on the notification they are returned to the app. Messages containing both notification
    // and data payloads are treated as notification messages. The Firebase console always sends notification
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
    // [END_EXCLUDE]

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

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

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

}