0

According to the documentation:

If you want foregrounded apps to receive notification messages or data messages, you’ll need to write code to handle the onMessageReceived callback.

What I have:

  • I have the service extending FirebaseMessagingService and overriding onMessageReceived as explained in the documentation (also in the manifest and the permissions).
  • The payload of the message is in the data map and the notification object is null (as expected).
  • The notifications when the app is in the background are working as intended.
  • The messages are being sent by SendBird (a 3rd party chat solution). And everything it is fine on their end and in our other clients implementations (ios, web and backend)

The problem is when the app is in the foreground the onMessageReceived callback is not being called.

Am I missing something? Shouldn't be the same service handling both app states of the app for the messages received? I don't want to show a system bar notification when the app is already opened, but I do want to change some elements in the UI to indicate the user that they have new messages in the chat.

Here is the code:

The service:

class ChatMessagingService : FirebaseMessagingService() {

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)

    Timber.e("----------- MESSAGE RECEIVED -------------")

    displayNotification(remoteMessage)

    sendBroadcastToActivity(remoteMessage) // 
}

}

in the manifest

        <service
        android:name=".notifications.ChatMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

firebase & gcm version

implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-messaging:20.1.0'

Thank you all in advance

edit, this method was required by a commenter, but it does not even reach to it so I do not think it is the problem.

private fun sendBroadcastToActivity(remoteMessage: RemoteMessage) {
    val messageData = Gson().fromJson<SendBirdPNPayload>(remoteMessage.data["sendbird"], SendBirdPNPayload::class.java)
    Intent(MESSAGE_RECEIVED_ACTION).also { intent ->
        intent.putExtra("message_id", messageData.messageId)
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
    }
}
Alberto Garrido
  • 485
  • 2
  • 6
  • 15
  • Please look into the answer here, [https://stackoverflow.com/questions/38451235/how-to-handle-the-firebase-notification-when-app-is-in-foreground](https://stackoverflow.com/questions/38451235/how-to-handle-the-firebase-notification-when-app-is-in-foreground) – sadat Jan 08 '20 at 13:02
  • can you show function "sendBroadcastToActivity()"?? also check the data payload you receiving in the log. – Nazim ch Jan 08 '20 at 13:06
  • @sadat I have been reading multiple responses, but the issue is not how to display notification in the foreground, the problem is that onMessageReceived is not being called. So it does not enter and hence does nothing. – Alberto Garrido Jan 08 '20 at 13:07
  • @Nazimch done, but the flow does not reach that method anyway – Alberto Garrido Jan 08 '20 at 13:10
  • check the "remoteMessage().getData()" in your log and see is it giving something. – Nazim ch Jan 08 '20 at 13:22
  • @Nazimch i can check it only when the app is in background, in that case it is getting the data as expected. But there is no way to check it when the app is in foreground, because it does not enter there. – Alberto Garrido Jan 08 '20 at 13:24

2 Answers2

0

The class name which extends FirebaseMessagingService and the name of service you define in your Manifest.xml is different. Try this:

<service
    android:name = ".ChatMessagingService"
    android:stopWithTask="false">
    <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
S.Grain
  • 182
  • 12
  • @AlbertoGarrido I updated my answer please try this. Hope it works – S.Grain Jan 08 '20 at 17:32
  • Yeah that was me trying to hide the company name :D in the real code the name was the same, sorry. Does the package path to the serve change anything? My service is in {$app_id}.notifications.ChatMessagingService should I remove the "notifications" part in the manifest? – Alberto Garrido Jan 09 '20 at 10:12
0

Thank you all for trying to help. We have found that it is not an issue with our code, everything was as it is supposed to. The 3rd party solution for chat would not send a push notification if the user is connected to the chat (via sdk) and the application is in foreground. So we have to use another technique to make changes in the UI.

Again, thank you all for trying to help!

Alberto Garrido
  • 485
  • 2
  • 6
  • 15