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:
FirebaseMessagingService
and
overriding onMessageReceived
as explained in the documentation
(also in the manifest and the permissions).data
map and the
notification
object is null (as expected).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)
}
}
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!