4

I am looking for this functionality:

Text

I am looking for pointers. Could not find any tutorials or packages providing this kind of functionality.

Thank you for your help!

Jo Eder
  • 318
  • 2
  • 9

3 Answers3

8

The firebase_messaging package does not provide any solution for action buttons. You can get your data in your onMessageReceived() method. And you can create your own customized notification.

Akif
  • 7,098
  • 7
  • 27
  • 53
  • 2
    How would I go about creating my own customized notification? – Jo Eder Nov 14 '20 at 09:09
  • 3
    There is a flutter_local_notifications package about this: https://pub.dev/packages/flutter_local_notifications – Akif Nov 14 '20 at 09:10
  • 1
    How do you avoid the notification from FCM showing as well as the notification you create using the plugin with actions? Wouldn't you get duplicate notifications or two when you only want to show one? – Jonas Apr 26 '21 at 18:59
  • don't add notification field on notification data that you send from back-end just add data field – Benyamin Beyzaie Jul 07 '21 at 22:23
7

There is a package called awesome_notifications which you can use for this purpose. for adding custom action button you can do like so:

    await AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: 0,
        channelKey: 'basic_channel',
        title: 'Simple title',
        body: 'Simple body ',
      ),
      actionButtons: [
        NotificationActionButton(
          key: 'accept',
          label: 'Accept',
        ),
        NotificationActionButton(
          key: 'cancel',
          label: 'Cancel',
        ),
      ],
    );

And for doing actions based on actions you must listen to this stream:

    AwesomeNotifications().actionStream.listen((event) {
      if (event.buttonKeyInput == 'cancel') //...
      else // ...
    });

You can use this library when app is up and running but when app is terminated you can't actually use this and you better use FCM notifications and then open the app and then take action based on notification type.

0

**For Adding Button inside background notification ** Add AndroidNotificationAction inside AndroidNotificationDetails inside firebase background handler code is given below

  void showFlutterNotification(RemoteMessage message) {
  RemoteNotification? notification = message.notification;
  AndroidNotification? android = message.notification?.android;
  if (notification != null && android != null && !kIsWeb) {
    flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          channelDescription: channel.description,
          // TODO add a proper drawable resource to android, for now using
          //      one that already exists in example app.
          icon: 'launch_background',
          actions: <AndroidNotificationAction>[
            AndroidNotificationAction('id_1', 'Action 1'),
            AndroidNotificationAction('id_2', 'Action 2'),
            AndroidNotificationAction('id_3', 'Action 3'),
          ],
        ),
      ),
    );
  }
}