当用户在应用程序处于前台时点击由 onMessage 函数触发的通知时,我尝试使用 Get 导航到不同的屏幕。到目前为止,我还无法做到这一点。
这是我到目前为止所做的:
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/flutter (17567): [错误:flutter/lib/ui/ui_dart_state.cc(199)]
未处理的异常:您正在尝试使用无上下文导航
没有 E/flutter (17567):一个 GetMaterialApp 或 Get.key。
E/flutter (17567):如果您正在测试您的应用程序,您可以使用:
E/flutter (17567): [Get.testMode = true],或者如果你正在运行
E/flutter (17567) 上的应用程序:物理设备或模拟器,
你必须交换你的 [MaterialApp] E/flutter (17567): for a
[获取材料应用程序]。 E/颤振(17567):E/颤振(17567):#0
GetNavigation.global 包:get/…/src/extension_navigation.dart:1052
E/flutter (17567):#1 GetNavigation.to
包:get/…/src/extension_navigation.dart:511 E/flutter (17567): #2
_MyAppState.initState。包:flutter_complete_guide/main.dart:146 E/flutter (17567): #3
_rootRunUnary (dart:async/zone.dart:1362:47) E/flutter (17567): #4 _CustomZone.runUnary (dart:async/zone.dart:1265:19) E/flutter (17567): #5 _CustomZone。 runUnaryGuarded (dart:async/zone.dart:1170:7) E/flutter (17567): #6 _BufferingStreamSubscription._sendData
(dart:async/stream_impl.dart:341:11) E/flutter (17567): #7
_DelayedData.perform (dart:async/stream_impl.dart:591:14) E/flutter (17567): #8 _StreamImplEvents.handleNext
(dart:async/stream_impl.dart:706:11) E/flutter (17567): #9
_PendingEvents.schedule。 (dart:async/stream_impl.dart:663:7) E/flutter (17567): #10
_rootRun (dart:async/zone.dart:1346:47) E/flutter (17567): #11 _CustomZone.run (dart:async/zone.dart:1258:19) E/flutter (17567): #12 _CustomZone. runGuarded (dart:async/zone.dart:1162:7) E/flutter (17567): #13 _CustomZone.bindCallbackGuarded。
(dart:async/zone.dart:1202:23) E/flutter (17567): #14 _rootRun
(dart:async/zone.dart:1354:13) E/flutter (17567): #15
_CustomZone.run (dart:async/zone.dart:1258:19) E/flutter (17567): #16 _CustomZone.runGuarded (dart:async/zone.dart:1162:7) E/flutter (17567): #17 _CustomZone.bindCallbackGuarded。
(dart:async/zone.dart:1202:23) E/flutter (17567): #18
_microtaskLoop (dart:async/schedule_microtask.dart:40:21) E/flutter (17567): #19 _startMicrotaskLoop
(dart:async/schedule_microtask.dart:49:5) E/flutter (17567):
D/ViewRootImpl@874c66fMainActivity:已停止(假)旧=假
答案 0 :(得分:0)
要打开通知,您必须使用它
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
// print('A new onMessageOpenedApp event was published!');
Get.to(OrdersScreen());
});
答案 1 :(得分:0)
我可以使用导航键来做到这一点。
创建全局密钥:
final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator");
在 MaterialApp 中初始化它:
MaterialApp(
navigatorKey: navigatorKey,
在onMessage函数中使用它:
navigatorKey.currentState.push(
MaterialPageRoute(builder: (_) => OrdersScreen()));