首页 > 解决方案 > Is there any way to implement push notifications with action buttons in flutter? Using firebase_messaging

问题描述

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!

标签: firebaseflutterfirebase-cloud-messagingfirebase-in-app-messaging

解决方案


有一个名为awesome_notifications的包,您可以将其用于此目的。要添加自定义操作按钮,您可以这样做:

    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',
        ),
      ],
    );

对于基于操作的操作,您必须收听此流:

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

您可以在应用程序启动并运行时使用此库,但当应用程序终止时您实际上无法使用它,您最好使用 FCM 通知,然后打开应用程序,然后根据通知类型采取措施。


推荐阅读