2
votes

I defined firebase service:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public void onMessageReceived(com.google.firebase.messaging.RemoteMessage remoteMessage) {
        mgr.showNotification("X:" + remoteMessage.getNotification().getTitle(), "X:" + remoteMessage.getNotification().getBody(), data);
    }

    @Override
    public void onDeletedMessages() {}
     @Override
    public void onMessageSent(java.lang.String s) {}
    @Override
    public void onSendError(java.lang.String s, java.lang.Exception e) {}
}

And android manifest:

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

There are two very strange behaviours with the FCM I noticed:

  1. Sometimes onMessageReceived() is called and sometimes not. It depends on whether my app is running or not. Anyway the notification is still displayed (that's why I added "X:")

  2. Sometimes there is no notification and no onMessageReceived is called at all, but I can see in "FCM diagnostics" in developer console that message was acknowledged:

Current Status    Acknowledged
Collapse Key  <key>
TTL   2,419,200
TIME  CURRENT STATUS
20 Jun 17:35
Device connected
20 Jun 21:23
Accepted
20 Jun 21:23
Delivered
20 Jun 21:28
Acknowledged

Can anybody explain this?

1
How are you determining whether or not onMessageReceived is called? What does mgr.showNotification do? - Arthur Thompson
@Marcin Did you ever get this fixed? - Questioner

1 Answers

3
votes

onMessageReceived(...) method will not get called if the app is in background or killed when the message is sent through Firebase Console.

BUT If you send the message via API, it works fine. Message is delivered in onMessageReceived(...) method whether app is in background, foreground or killed.

There are two types of messages in FCM:

  • notification: work when your app is in foreground
  • data payload: work even if your app is in background

Reference: https://firebase.google.com/docs/cloud-messaging/downstream#sample-receive

Update:

In case you don't know how to send a message using API, you can use a tool called AdvancedREST Client, its a chrome extension or similar, and send a message with the following parameters. Reference link: https://firebase.google.com/docs/cloud-messaging/downstream

Sending downstream messages from the server

To address or "target" a downstream message, the app server sets to with the receiving client app's registration token. You can send notification messages with predefined fields, or custom data messages; see Notifications and data in the message payload for details on payload support. Examples in this page show how to send data messages in HTTP and XMPP protocols.

HTTP POST Request

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

You can get the server key (AIzaSyZ-1u...0GBYzPu7Udno5aA), from firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key