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 overridingonMessageReceived
as explained in the documentation (also in the manifest and the permissions). - The payload of the message is in the
data
map and thenotification
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)
}
}