0

I am developing flutter application using awesome notification. I want to redirect to notification page when notification is clicked.

Initialize main.dart notification:

AwesomeNotifications().initialize(
      'resource://drawable/ic_stat_ida',
      [
        NotificationChannel(
            channelKey: 'notification_channel',
            channelName: 'Normal Notifications',
            channelDescription: 'Notification channel for normal notifications',
            defaultColor: Color(0xFFffa473),
            ledColor: Colors.white,
            importance: NotificationImportance.High,
            playSound: true,
            enableLights: true,
            enableVibration: true,
            channelShowBadge: false)
      ],
      debug: true);

If the application is closed, I do the following on splashscreen:

void initState() {
    super.initState();
    AwesomeNotifications().actionStream.listen((receivedAction) {
          var payload = receivedAction.payload;
          print(payload);
          result = true;
          Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => OrganizerNotificationScreen(
                  taskId: receivedAction.id.toString(),
                  taskBody: receivedAction.body,
                ),
              ));
        });
}

This is succesful. But I want to redirect when the notification is clicked on any page while the application is open.

void listenOrganizerNotification(context) {
  AwesomeNotifications()
      .actionStream
      .listen((ReceivedNotification receivedNotification) {
    print('event received!');
    print(receivedNotification.toMap().toString());
    Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => OrganizerNotificationScreen(
            taskId: receivedNotification.id.toString(),
            taskBody: receivedNotification.body,
          ),
        ));
  });
}

Where should I write listen and redirect function?

zey
  • 177
  • 2
  • 15

1 Answers1

0

By replacing AutoRouter by Navigator it should work as attended. You must check the current page then compare with the target page.

            const newRouteName = 'ChatRoomScreenRoute';
            bool isNewRouteSameAsCurrent = false;

            AutoRouter.of(context).popUntil((route) {
              if (route.settings.name == newRouteName) {
                isNewRouteSameAsCurrent = true;
              }
              return true;
            });

            if (isNewRouteSameAsCurrent) {
              Navigator.of(context).pop();
              await Future.delayed(const Duration(milliseconds: 500));
              AutoRouter.of(context).push(
                  ChatRoomScreenRoute(chatId: payload['chatId'].toString()));
            } else {
              AutoRouter.of(context).push(
                  ChatRoomScreenRoute(chatId: payload['chatId'].toString()));
            }
mario francois
  • 1,291
  • 1
  • 9
  • 16
  • unfortunately it didn't work. I can explain the problem like this. I put the Listen method in the inital of the page. but it does not listen momentarily, it catches the click on the notification when the page changes. – zey Mar 31 '22 at 06:00