我在下面的代码中添加了2个按钮到我的通知中 但我不知道如何找出哪个按钮被按下,并在此基础上做些什么

  Future<void> zonedScheduleAlarmClockNotification({
    required Duration duration,
    required String title,
    required String message,
    required String type,
  }) async {
    print(duration);
    final random = Random();
    int id = random.nextInt(999999999);
    await flutterLocalNotificationsPlugin.zonedSchedule(
      id,
      title,
      message,
      tz.TZDateTime.now(tz.local).add(duration),
      
      NotificationDetails(
        android: AndroidNotificationDetails(
          'alarm_clock_channel',
          'Alarm Clock Channel',
          channelDescription: 'Alarm Clock Notification',
          subText: type,
          //silent: true,
          color: Colors.amber,
          colorized: true,
          ticker: title,
          playSound: true,
          priority: Priority.high,
          importance: Importance.high,
          
          actions: <AndroidNotificationAction>[
            const AndroidNotificationAction(
              'id_2',
              'Completed',
              titleColor: Color.fromARGB(255, 255, 0, 0),
              icon: DrawableResourceAndroidBitmap('secondary_icon'),
            ),
             const AndroidNotificationAction(
              's',
              'Action 3',
              icon: DrawableResourceAndroidBitmap('secondary_icon'),
              showsUserInterface: true,
              // By default, Android plugin will dismiss the notification when the
              // user tapped on a action (this mimics the behavior on iOS).
              cancelNotification: false,
            ), 
          ],
        ),
      ),
      androidScheduleMode: AndroidScheduleMode.alarmClock,
      payload: title,
      uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
    );
  }

我希望用户在他按下的每个按钮上执行应用程序内的任务 但我不知道如何找出用户点击了哪个按钮以及何时点击

推荐答案

正如你在document中看到的,它需要对iOS进行一些配置,但如果你已经这样做了,或者你使用的是Android,那么据说你应该声明一个顶级函数来接收应用程序中的通知并对其执行操作.在那里,你可以判断notificationResponse中的actionId并采取相应的行动.

例如,在这里的项目中,您可以编写如下内容:

@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
  if (notificationResponse.actionId == 'id_2') {
    // do something
  } else if (notificationResponse.actionId == 's') {
    // do something else
  }
}

Flutter相关问答推荐

我怎样才能判断一个字符串值是否为空(&Q;&Q;)?

如何在Ffltter Chrome应用程序中使用webview_fltter_web显示自定义html而不是外部uri?

如何在Flutter 中不从getX库中初始化GetxController中的变量

material 3芯片,不能go 除输入芯片小部件上的轮廓

脚本具有不受支持的MIME类型(';Text/html';).(messaging/failed-service-worker-registration)

当 Dart SDK 版本范围不匹配时,为什么依赖解析器不会抛出错误?

Flutter:使用不包含导航器的上下文请求导航器操作

有没有办法在 google_map 上移动标记(向左或向右移动一些像素)?

Flutter中pubspec.yaml文件中的name参数是什么?

Flutter firebase_auth 和 firebase_core 依赖错误

Flutter - firebase_auth_web 5.4.0 依赖于 intl ^0.17.0 但我必须使用更高版本 (intl ^0.18.0)

无状态小部件被奇怪地重建

如果不刷新页面,我看不到从 Firestore 中提取的数据

如何在 Flutter 中测试动画图标

Flutter使Perspective Page View无限循环

对话框打开时如何使背景模糊?

容器在发布模式下显示灰色框(抖动)

如何将 void 函数放入单独的 dart 文件的单独页面中

在 Flutter 中单击时切换按钮 colored颜色 没有改变

Flutter AAPT:错误:找不到资源android:attr / lStar