Jonas
Jonas

Reputation: 250

How to show only one notification with FCM and awesome_notifications

I am using Firebase Messaging to send users notifications from the cloud, but I want some notifications to have action buttons options (text input, button selections) from within the notification, so I'm using the awesome_notifications.

I have set up the package to run this method whenever an notification is received:

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await AwesomeNotifications().createNotification(
    content: NotificationContent(
      id: 1,
      channelKey: 'basic_channel',
      title: message.notification.title,
      body: message.notification.body,
      createdSource: NotificationSource.Firebase,
      notificationLayout: NotificationLayout.BigText,
    ),
    actionButtons: <NotificationActionButton>[
      NotificationActionButton(
        key: 'short_text',
        label: 'Answer',
        buttonType: ActionButtonType.InputField,
        autoCancel: true,
      ),
    ],
  );
}

This does properly generate a notification with the correct title, body, layout, and text input option, but the notification received directly from FCM is also displayed. I only want the notification that I generate with awesome_notifications to be shown but I can't seem to find a way to do this. Thanks for any input.

Upvotes: 1

Views: 1636

Answers (1)

Victor Eronmosele
Victor Eronmosele

Reputation: 7716

Firebase Cloud Messaging(FCM) has two types of messages:

  • Notification messages in which FCM automatically displays the message to end-user devices on behalf of the client app.
  • Data messages in which your app is responsible for processing data messages.

About FCM messages

You should use a data message so your app does not display the automatic notification that comes with the notification message type that you currently use.

So if your cloud function contained this code below to send notification messages:

const message = {
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
};
admin.messaging().send(message);

you can update it to:

const message ={
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data":{
      "title" : "Portugal vs. Denmark",
      "body" : "great match!"
    }
  }
};
admin.messaging().send(message);

Upvotes: 4

Related Questions