带有操作按钮和图像的 Flutter 本地通知

Posted

技术标签:

【中文标题】带有操作按钮和图像的 Flutter 本地通知【英文标题】:Flutter local notification with action buttons and image 【发布时间】:2021-08-19 18:43:35 【问题描述】:

我想通过 Firebase 消息推送此类通知。现在是我使用https://pub.dev/packages/flutter_local_notifications 这个包的普通通知的时候了。但我没有看到有一种方法可以添加操作按钮。以及我想在 45 秒后删除此通知或使用另一条 firebase 消息。我也在为 androidios 开发这个应用程序。如果我的问题不清楚或需要更多信息,请随时发表评论。

我在 *** 上看到了一个类似的问题。

Flutter local notification with action buttons Notification with action buttons

【问题讨论】:

【参考方案1】:

flutter_local_notification 尚未支持ticket 中提到的通知操作按钮。您可能需要同时考虑使用awesome_notifications 插件,因为它支持通知按钮。

要使用操作按钮显示通知,只需使用


void _showNotificationWithButton() 
  AwesomeNotifications().createNotification(
    content: NotificationContent(
        id: 10,
        channelKey: 'basic_channel',
        title: 'Simple Notification',
        body: 'Simple body'),
    actionButtons: <NotificationActionButton>[
      NotificationActionButton(key: 'yes', label: 'Yes'),
      NotificationActionButton(key: 'no', label: 'No'),
    ],
  );

然后,您可以通过在 NotificationActionButton 上添加图标属性来为操作按钮添加图标。图标应该是资产中映射的图像的字符串资源。

要侦听 Action Button 按下,请使用 actionStream 侦听器。你可以在屏幕的initState()添加这个

AwesomeNotifications().actionStream.listen((receivedNotification) 
  // prints the key of the NotificationActionButton pressed
  debugPrint('Notification key pressed: $receivedNotification.buttonKeyPressed');
);

【讨论】:

以上是关于带有操作按钮和图像的 Flutter 本地通知的主要内容,如果未能解决你的问题,请参考以下文章

为啥颤振本地通知会出现异常

在运行时隐藏通知操作按钮

Swift 本地推送通知操作视图

从 IOS 上的通知操作按钮打开特定应用程序?

无法查看 iOS 10 本地通知的操作按钮

后台 FCM 触发时生成 Flutter 本地通知