Flutter-点击通知时打开页面

发布于 2025-01-17 20:09:26 字数 1841 浏览 1 评论 0原文

我正在使用很棒的通知开发颤振应用程序。我想在单击通知时重定向到通知页面。

初始化 main.dart 通知:

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);

如果应用程序关闭,我在启动屏幕上执行以下操作:

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,
                ),
              ));
        });
}

这是成功的。 但我想在应用程序打开时在任何页面上单击通知时进行重定向。

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,
          ),
        ));
  });
}

我应该在哪里编写监听和重定向函数?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2025-01-24 20:09:26

通过用 Navigator 替换 AutoRouter,它应该按有人值守的方式工作。您必须检查当前页面然后与目标页面进行比较。

            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()));
            }

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()));
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文