firebase - 在 Flutter 中打开推送通知时使用 Navigator

标签 firebase flutter push-notification

我正在使用 Firebase Cloud Messaging 通过云功能向我的 Flutter 应用程序发送推送通知,并在通知中添加数据,这些数据将定义用户点击通知时将打开的页面。

这是我用来处理修改的逻辑:

void _handleMessage(RemoteMessage message) {
     Navigator.pushNamed(context, "/shop", arguments: message.data["id"]);
     return;
}

Future<void> setupInteractedMessage() async {
    // Get any messages which caused the application to open from
    // a terminated state.
    RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();

    // If the message also contains a data property with a "type" of "chat",
    // navigate to a chat screen
    if (initialMessage != null) {
      _handleMessage(initialMessage);
    }

    // Also handle any interaction when the app is in the background via a
    // Stream listener
    FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
}    

在初始化 firebase 应用程序时,我调用了 setupInteractedMessage();

这有效,但是当应用程序在后台并且我点击通知时,我收到以下错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

这意味着我无法访问导航器。有什么方法可以解决此问题,从而在用户点击通知时打开特定页面吗?

最佳答案

将此行移动到启动画面或任何其他启动屏幕构建。

FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);

关于firebase - 在 Flutter 中打开推送通知时使用 Navigator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68754436/

相关文章:

android - 无法将 SHA1 添加到 Firebase 证书指纹中

火力地堡 : Differences between realtime database and file storage

ios - 处理推送通知负载数据

ios - 如何在 iOS 应用程序上实现 Apple 推送通知服务?

firebase - 如何在本地测试 `functions.https.onCall` firebase云功能?

Firebase实时数据库查询Flutter中的所有项目

flutter - 覆盖子 GestureDetectors 的 GestureDetector?

flutter - 如何在Flutter中使用默认情况下的不可为空?

android - 版本解决了使用新的 Flutter 2.2.2 版本运行 Riverpod 时失败 "flutter pub get"的问题

ios - 应用程序在后台时的远程推送通知 swift 3