首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用FCM onMessage导航到不同的屏幕。

无法使用FCM onMessage导航到不同的屏幕。
EN

Stack Overflow用户
提问于 2021-06-18 17:42:48
回答 2查看 936关注 0票数 0

当用户点击应用程序在前台时从到达函数触发的通知时,我试图使用onMessage导航到另一个屏幕。到目前为止,我还未能做到这一点。

以下是我迄今所做的工作:

代码语言:javascript
复制
  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

EN

回答 2

Stack Overflow用户

发布于 2021-06-18 18:27:12

我可以用导航键来完成这个任务。

创建了全局密钥:

代码语言:javascript
复制
final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator");

在MaterialApp中初始化它:

代码语言:javascript
复制
MaterialApp( 
        navigatorKey: navigatorKey,   

它在onMessage函数中使用:

代码语言:javascript
复制
navigatorKey.currentState.push(
    MaterialPageRoute(builder: (_) => OrdersScreen()));
票数 1
EN

Stack Overflow用户

发布于 2021-06-18 17:49:12

若要打开通知,必须使用以下命令

代码语言:javascript
复制
 FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
        // print('A new onMessageOpenedApp event was published!');

         Get.to(OrdersScreen());
      });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68039449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档