当用户点击应用程序在前台时从到达函数触发的通知时,我试图使用onMessage导航到另一个屏幕。到目前为止,我还未能做到这一点。
以下是我迄今所做的工作:
void initState(){
super.initState();
var initializationSettingsAndroid = AndroidInitializationSettings('@drawable/splash');
var initializationSettings = InitializationSettings(android:initializationSettingsAndroid);
flutterLocalNotificationsPlugin.initialize(initializationSettings);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: '@drawable/splash',
playSound: true
),
));
Get.to(OrdersScreen());
}
});
}我收到通知,但当我点击时,什么都不会发生。我还在日志中得到以下异常。
E/颤振(17567):错误:flutter/lib/ui/ui_dart_state.cc(199)未处理的异常:您尝试使用没有E/flutter (17567)的无内容导航:一个GetMaterialApp或Get.key。E/flutter (17567):如果您正在测试您的应用程序,您可以使用:e/ MaterialApp (17567):MaterialApp= true,或者如果您正在E/flutter (17567)上运行您的应用程序:一个物理设备或模拟器,您必须将您的MaterialApp E/flutter (17567):交换为GetMaterialApp。E/颤振(17567):E/颤振(17567):#0 GetNavigation.global包:get/…/src/ExtensionNavigation.DART:1052E/flutter (17567):#1 GetNavigation.to包:get/…/src/extension_navigation.dart:511 E/颤振(17567):#2 _MyAppState.initState。包装:颤振_完整_指南/主要。飞镖:146E/颤振(17567):#3 _rootRunUnary (省道:1362:47)E/颤振(17567):#4 _CustomZone.runUnary (飞镖:异步/zone.dart:1265:19)E/ _CustomZone.runUnaryGuarded (17567):#5 _CustomZone.runUnaryGuarded(飞镖:异步/左转:1170:7)E/颤振(17567):#6 _BufferingStreamSubscription._sendData (飞镖:异步/流__CustomZone.runUnaryGuarded:341:11) _DelayedData.perform (省:异步/流_隐式:591:14)E/颤振(17567):#8 _StreamImplEvents.handleNext (省:异步/流_隐式:706:11)E/颤振(17567):#9 _PendingEvents.schedule。(省:异步/流_隐式:663:7)E/颤振(17567):#10 _rootRun (飞镖:异步/zone.dart:1346:47)E/ _CustomZone.run (17567):#11 _CustomZone.run(飞镖:异步/zone.dart:1258:19)E/_CustomZone.run (17567):#12 _CustomZone.runGuarded (飞镖:异步/飞镖:1162:7)E/颤振(17567):#13 _CustomZone.bindCallbackGuarded。(省道:异步/左转:1202:23)E/颤振(17567):#14 _rootRun (飞镖:异步/zone.dart:1354:13) _CustomZone.run (飞镖:异步/zone.dart:1258:19)E/ _CustomZone.runGuarded (17567):#16 _CustomZone.runGuarded(飞镖:异步/zone.dart:1162:7)E/颤振(17567):#17 _CustomZone.bindCallbackGuarded。(飞镖:异步/左转:1202:23)E/颤振(17567):#18 _microtaskLoop (省道:异步/调度_微任务.dart:40:21)E/ _startMicrotaskLoop (17567):#19 _startMicrotaskLoop(省道:异步/调度_微任务.dart:49:5)E/颤振(17567):D/ViewRootImpl@874c66fMainActivity: stopped(false) old=false
发布于 2021-06-18 18:27:12
我可以用导航键来完成这个任务。
创建了全局密钥:
final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator");在MaterialApp中初始化它:
MaterialApp(
navigatorKey: navigatorKey, 它在onMessage函数中使用:
navigatorKey.currentState.push(
MaterialPageRoute(builder: (_) => OrdersScreen()));发布于 2021-06-18 17:49:12
若要打开通知,必须使用以下命令
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
// print('A new onMessageOpenedApp event was published!');
Get.to(OrdersScreen());
});https://stackoverflow.com/questions/68039449
复制相似问题