Home:ALL Converter>Flutter FCM shows empty notification when no notification data is sent and when app is in bg

Flutter FCM shows empty notification when no notification data is sent and when app is in bg

Ask Time:2022-11-16T06:16:07         Author:OutdatedGuy

Json Formatter

I'm sending FCM notification from my cloud functions to my flutter app.

I don't want the FCM system to handle notification display when the app is in background or terminated. Hence, I'm sending the data to show in notification in the data key instead of the notification key.

Cloud Functions:

await admin.messaging().sendMulticast({
  tokens: userFCMTokens,
  data: {
    title: "Notification title to display",
    body: "Notification body to display",
  },
  android: {
    notification: {
      channelId: "some_channel_id",
    },
  },
});

On the client side (i.e. Flutter app) I'm receiving/handling the FCM messages and displaying them with the awesome_notifications plugin.

Flutter:

AwesomeNotifications().createNotification(
  content: NotificationContent(
    id: message.hashCode,
    channelKey: message.notification?.android?.channelId,
    title: message.data['title'],
    body: message.data['body'],
    bigPicture: message.data['imageUrl'],
    wakeUpScreen: true,
    notificationLayout: NotificationLayout.BigPicture,
  ),
  actionButtons: (message.data['actions'] as List<Map>?)
      ?.map(
        (e) => NotificationActionButton(
          key: e['key'],
          label: e['label'],
        ),
      )
      .toList(),
);

This setup works properly when the app is in foreground but when in background or terminated it shows 2 notification. One proper and one totally empty notification.

enter image description here

I don't want the FCM SDK system to handle the notification hence I'm using data instead of notification but then this problem arises.

Author:OutdatedGuy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74452822/flutter-fcm-shows-empty-notification-when-no-notification-data-is-sent-and-when
yy