2

I am trying to implement push notifications in flutter, I am using firebase_messaging for that and flutter_local_notification to show notification when the app is in foreground.

I know the contents of the message sent by firebase it is as follows: { "message":{ "token":"token_1", "data":{}, "notification":{ "title":"FCM Message", "body":"This is an FCM notification message!", } } }

notification and data.

firebase_messaging creates notification automatically when notification is the part of the message when the app is background or terminated state.

There are two notification created by the FCM sdk itself and the flutter_local_notification plugin separately when the app is in background or terminated state, when both notification and data is sent from firebase console or from server.

I want to show the notification from the flutter_local_notification only due to the less customization available in the FCM sdk created notification for that the server is sending only data notification but the app is not able to catch it when the app is in background or terminated state (not running in background).

So, what I want is:

==> To prevent firebase_messaging from creating the notification when both data and notification is sent in the message from server or firebase console.

OR

==> To be able to catch the data only message from the server when the app is background or terminated state and show from the flutter_local_notification plugin.

Struggling through it from months please help.

Lalit Thakare
  • 63
  • 1
  • 10
  • 1
    Set priority to high in message payload. Refer this: https://stackoverflow.com/questions/48451761/firebase-message-with-high-priority-not-waking-device-from-doze-android-6 – Rohit Suthar Mar 09 '23 at 10:00

1 Answers1

2

To prevent Firebase Messaging from automatically creating a notification when both data and notification are sent in the message from the server or Firebase console, you can use the FirebaseMessaging.configure method and set the onMessage callback to handle the incoming messages.

In the onMessage callback, you can check if the message contains both data and notification fields, and if so, you can choose to ignore the notification field and handle the notification using flutter_local_notification instead.

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    if (message.data.isNotEmpty && message.notification != null) {
        // Show notification using flutter_local_notifications
        showNotification(message.data['title'], message.data['body']);
    } else {
        // Handle the message as usual
        // Firebase Messaging will automatically create a notification for messages without both data and notification fields
    }
});

This way, you can customize the notification to your liking using flutter_local_notification, while still receiving messages and handling them appropriately.

Alternatively, if you want to catch the data-only message from the server when the app is in the background or terminated state and show it using the flutter_local_notifications plugin, you can modify your FirebaseMessaging.onBackgroundMessage listener to achieve this:

FirebaseMessaging.onBackgroundMessage((message) async {
  if (message.data != null) {
    // Show notification using flutter_local_notifications
    await showNotification(message.data['title'], message.data['body']);
  }
});
0xdesdenova
  • 192
  • 1
  • 8
  • 1
    but the onMessage method listens to the incoming message only when the app is foreground. What to do when the app is in background?? – Lalit Thakare Mar 21 '23 at 18:09
  • I have edited my answer to reflect this. – 0xdesdenova Mar 28 '23 at 19:09
  • i have done the same but it is not able to catch the data payout only notification when the app is in not in the foreground. and on sending the notification payout message to the device the sdk create one automatically. – Lalit Thakare Apr 02 '23 at 11:01