Android FCM - onMessageReceived not called on background

I'm using FCM. Notifications are working properly and onMessageReceived is called on Foreground, but when I receive a notification while the app is in background/killed that method is never called.

I'm using data messages as Firebase Documentation suggested but no succeed...

Upvotes: 0

Views: 4072

Answers (3)

Sangeet Suresh
Sangeet Suresh

Reputation: 2545

FCM has 3 kind of notifications

1. Notification messages Json body will be only having notification tag FCM will automatically show notification in client app if app is in background. onMessageReceived() will be called both in foreground . In background, onMessageReceived() will not be called.

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}

2. Data messages Json body will be only having data tag Developer have to show notification. onMessageRecieved will be called both in foreground as well as in background. The json should only have data tag in it

 {
      "message":{
        "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
        "data":{
          "Nick" : "Mario",
          "body" : "great match!",
          "Room" : "PortugalVSDenmark"
        }
      }
    }

3. Notification and data messages

The json body will be having both notification and data tags. onMessageReceived() will be called only when app is in foreground. Notifications will be shown automatically shown if app is in background and onMessageReceived() will not be called.

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }
}

For more info please read https://firebase.google.com/docs/cloud-messaging/concept-options

Upvotes: 3

this is the notification I'm sending:

 payload = new JSONObject();
 notification = new JSONObject();

 notification.put("title", "Avisos");
 notification.put("text", "Nuevos avisos disponibles");

 payload.put("data", notification);
 payload.put("to", "/topics/avisos");
 payload.put("priority",10);

Upvotes: -1

amralsaidy
amralsaidy

Reputation: 183

Are you sure you add like this service in AndroidManifest.xml

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

Upvotes: 1

Related Questions