push-notification - 如何在收到远程通知时推送导航到新的 Widget?

标签 push-notification flutter firebase-cloud-messaging

我试图在我的应用程序中实现的是,当用户收到推送通知时,应用程序将推送一个新的小部件。我正确地获取了通知数据,但无法使其正常工作。

这是通知的监听器

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('on message $message');
        _manageNotificationData(message);
      },
      onResume: (Map<String, dynamic> message) async {
        print('on resume $message');
         _manageNotificationData(message);
      },
      onLaunch: (Map<String, dynamic> message) async {
        print('on launch $message');
        _manageNotificationData(message);
      },
    );

我的负载是

data: {
       "id" : "Id for notification state",
       "click_action": "FLUTTER_NOTIFICATION_CLICK","
       "orderId": "SomeId"
      }
void _manageNotificationData(Map<String,dynamic> message){

 //When the user receives a notification, a new widget will appear so that he can rate another user

  if(message["id"] == 1){

  //This data is printed in the console
  print(message["orderId"]);

  //this is not working
    Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => 
    RateProfessionalWidget(
    orderId: message["orderId"])
    ),
  );
  }

}

我猜这是行不通的,因为我在主文件中调用了 Navigator.push,但在这个文件中是我唯一可以访问通知数据的地方。我怎样才能完成这项工作?

最佳答案

确保您的应用(您runApp())是一个StatefulWidget,并在其状态下创建一个GlobalKey

GlobalKey<NavigatorState> navigatorKey =
    GlobalKey<NavigatorState>(debugLabel: 'navKey');

当您创建 MaterialApp 时,将 key 传递给它

  child: MaterialApp(
    navigatorKey: navigatorKey,

在您的应用中,您现在可以使用 navigatorKey.currentState.push()

关于push-notification - 如何在收到远程通知时推送导航到新的 Widget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55819478/

相关文章:

android - 如何在Android Studio中为dart函数自动生成header comments?

android - 是否可以在 Android 中将 Firebase Messaging 与 Google Analytics 结合使用?

android - PhoneGap 构建推送通知 : Android pushes do not work when the app is closed

android - FB Analytics 推送 Activity - "Set up Your Push Campaigns"在实现后仍然存在

websocket - 如何有效地向应用程序中的选定用户发送推送通知?

dart - 如何从 Flutter 的内部和外部存储中获取所有 PDF 文件?

Flutter、Dart 和 Future。这是 Dart Futures 的已知功能并且为空吗?

node.js - 如何阅读使用 Node 在Firestore中创建的最新文档?

ios - 如何在 FCM 中使用 apns-collapse-id?

javascript - Service-Worker addEventListener,事件参数如何使用?