如何将flutter_local_notification包添加到按钮通知中?

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

我在下面的代码中向我的通知添加了 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,
    );
  }

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

flutter dart button notifications flutter-local-notification
1个回答
0
投票

正如您在文档中所看到的,它需要针对iOS进行一些配置,但是,如果您已经这样做了,或者您使用的是android,则据说您应该声明一个顶级函数来接收通知应用程序并对其采取行动。在那里您可以检查

actionId
notificationResponse
并采取相应行动。

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

@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
  if (notificationResponse.actionId == 'id_2') {
    // do something
  } else if (notificationResponse.actionId == 's') {
    // do something else
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.