1

I'm using fcm + awesome notifications and I'm displaying progress bar notification when app receives fcm message.

There are two buttons in notification and I wanna cancel notification if user clicks notification action buttons

final timers = <int, Timer>{};

@pragma("vm:entry-point")
Future<void> _actionHandler(ReceivedAction action) async {
  final id = action.id!;

  timers[id]!.cancel();
}

@pragma("vm:entry-point")
Future<void> _messageHandler(RemoteMessage message) async {
  final plugin = AwesomeNotifications();
  final at = DateTime.parse(message.data["at"]);
  const someId = 100;

  timers[someId] = Timer.periodic(
    const Duration(seconds: 1),
    (timer) {
      final diff = at.difference(DateTime.now());

      if (diff.isNegative) {
        timer.cancel();

        plugin.dismiss(someId);
      } else {
        plugin.createNotification(
          content: NotificationContent(
            id: someId,
            channelKey: "some_channel",
            title: "Title",
            notificationLayout: NotificationLayout.ProgressBar,
            progress: (((60 - diff.inSeconds) / 60) * 100).round(),
            actionType: ActionType.SilentAction,
          ),
          actionButtons: [
            NotificationActionButton(
              key: "cancel",
              label: "Cancel",
              actionType: ActionType.SilentAction,
            ),
            NotificationActionButton(
              key: "confirm",
              label: "Confirm",
              actionType: ActionType.SilentAction,
              isDangerousOption: true,
            ),
          ],
        );
      }
    },
  );
}

here's my current sample code but it won't work because _actionHandler function is called in another dart isolate so variable timers won't be accessible

So is there a way to achieve what I want in flutter or should I just use native code

이동헌
  • 41
  • 1
  • 1

0 Answers0