将操作添加到 AndroidNotificationDetails

问题描述 投票:0回答:2

我想在通知中添加一个按钮,我能够显示它,但我找不到有关如何向按钮按下添加操作的文档

const androidNotification = AndroidNotificationDetails(
      'Bla', 'Bla',
      icon: 'ic_bg_service_small',
      ongoing: true,
      actions: [AndroidNotificationAction("stop", "Stop")]);

  flutterLocalNotificationsPlugin.show(
    888,
    'MyService',
    'bla',
    const NotificationDetails(android: androidNotification),
  );
flutter
2个回答
1
投票

您需要使用此插件在通知中执行操作https://pub.dev/packages/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'),
    ],
  );
}

0
投票

根据通知操作的文档您可以在

flutterLocalNotificationsPlugin
初始化期间设置回调

final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

flutterLocalNotificationsPlugin.initialize(
  initializationSettings,
  onDidReceiveBackgroundNotificationResponse: _onBackgroundAction,
);

其中

_onBackgroundMessage
是一个用
@pragma('vm:entry-point')
注释的函数,如下所示

@pragma('vm:entry-point')
Future<void> _onBackgroundAction(NotificationResponse response) async {
  //Do what you need here
}
© www.soinside.com 2019 - 2024. All rights reserved.