我希望我的flutter应用程序在单击本地通知时打开一个页面。我在事件监听器中定义了以下代码:

Navigator.push(
   Ccontext,
   MaterialPageRoute(builder: (context) => PendingOrders()),
 );
debugPrint('notification payload: ' + payload);

事件侦听器已成功执行,并在调用本地通知但无法打开PendingOrders路由时打印debugPrint的参数。

这是main.dart中的完整代码
  class _MyAppState extends State<MyApp> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
@override
void initState() {
  super.initState();

  var android = AndroidInitializationSettings('mipmap/ic_launcher');
  var ios =  IOSInitializationSettings();
  var platform = InitializationSettings(android, ios);
  flutterLocalNotificationsPlugin.initialize(platform,onSelectNotification: onSelectNotification);

  _firebaseMessaging.subscribeToTopic('order-created');
  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
      print("onMessage: $message");
      showNotification(message);
    },
    onLaunch: (Map<String, dynamic> message) async {
      print("onLaunch: $message");
      print("Hafijur");
    },
    onResume: (Map<String, dynamic> message) async {
      print("onResume: ${message['notification']['data']['click_action']}");
    },
  );
  _firebaseMessaging.requestNotificationPermissions(
      const IosNotificationSettings(sound: true, badge: true, alert: true));
}

showNotification(Map<String, dynamic> msg) async {
  var android = new AndroidNotificationDetails(
    'sdffds dsffds',
    "CHANNLE NAME",
    "channelDescription",
  );
  var iOS = new IOSNotificationDetails();
  var platform = new NotificationDetails(android, iOS);
  await flutterLocalNotificationsPlugin.show(
      0, "New order arrived", "Hey, hurry up! you have a order to deliver", platform,payload: "order created");
}
 Future onSelectNotification(String payload) async {
  this.build(context);
   Navigator.push(
     context,
     MaterialPageRoute(builder: (context) => PendingOrders()),
   );
   if (payload != null) {
    debugPrint('notification payload: ' + payload);
  }
}

@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Flutter Demo',
    theme: Constants.lightTheme,
    debugShowCheckedModeBanner: false,
    routes: {
      "/": (context) => MainScreen(),
      "/login": (context) => LoginPage(),
      "/dashboard": (context) => Dashboard(),
      "/all_orders": (context) => AllOrders(),
      "/pending_orders": (context) => PendingOrders(),
      "/delivered_orders": (context) => DeliveredOrders(),
      "/order": (context) => Order(),
    },
  );
}
}

最佳答案

我有一个类似的问题,但这解决了我的问题

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) {
        displayNotification(message);
        return;
      },
      onResume: (Map<String, dynamic> message) {
        _navigateToItemDetail(message);
        return;
      },
      onLaunch: (Map<String, dynamic> message) {
        _navigateToItemDetail(message);
        return;
      },
    );
void _navigateToItemDetail(Map<String, dynamic> message) async {
    Navigator.popUntil(context, (Route<dynamic> route) => route is PageRoute);
    await Navigator.of(context).push(PageRouteBuilder(
        opaque: false, pageBuilder: (context, _, __) => NewPage();

  }

10-08 13:49