0
votes

I understand that Firebase Cloud Messaging facilitates sends push notifications and data messages. I am using firebase_messaging pub from Flutter to receive the notifications in my device. The onMessage, onLaunch, and onResume callbacks are invoked only once the notification is dismissed or clicked on.

However, I would like to access the message received, regardless of whether the notification is dismissed or not. Is there a way to do this, regardless whether the app is in foreground, background or terminated ?

1

1 Answers

1
votes

You can immediately send your message to Broadcast receiver and here parse it

UPDATED:

Your should create BroadcastReceiver:

class YourBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        //some code..
    }
}

Then register it in Manifest :

<receiver
    android:name="com.yourApp.YourBroadcastReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="YourBroadcastReceiver " />
    </intent-filter>
</receiver>

The next and final step is in your FirebaseMessagingService in method onMessageReceived you can take ur data and send it to YourBroadcastReceiver:

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)
    val messageText = remoteMessage.data[yourData]
    val intent = Intent(this, YourBroadcastReceiver ::class.java)
    intent.putExtra(messageText, messageText)
    sendBroadcast(intent)
}