如何使用FCM +数据消息从后台打开Flutter应用?

问题描述 投票:0回答:1

我已经在我的应用程序中实现了Google FCM,并在所有4个消息处理程序(onMessage,onLaunch,onResume和onBackground)上成功接收了消息。我的主要功能是导航到应用程序的特定屏幕。我使用NavigatorState键实现此功能,它对除onBackground之外的4个处理程序中的3个非常有用。当我向我的应用发送数据消息时,会触发onBackground并尝试导航到特定屏幕时出现此错误:

I / flutter(17050):NoSuchMethodError:方法'push'在null上调用。

I / flutter(17050):接收者:null

I / flutter(17050):尝试调用:push(“ MaterialPageRoute”的实例)

我的主要问题是,除了Navigator.of(context)或GlobalKey(NavigatorState)之外,还有什么其他方法可以导航到屏幕,即使在最小化应用程序后仍会保留应用程序的导航状态?

android ios flutter flutter-layout
1个回答
0
投票
您只需要在通知有效负载中放入一些变量,以标识您希望应用导航到的位置,然后像下面在

onResume和onLaunch中进行检查即可

onResume: (Map<String, dynamic> message) async { String type = message['data']['type']; if(type != null){ if(type == 'chat'){ Navigator.push(context, MaterialPageRoute(builder: (context)=> YourPageRoute())); } } } onLaunch: (Map<String, dynamic> message) async { String type = message['data']['type']; if(type != null){ if(type == 'chat'){ Navigator.push(context, MaterialPageRoute(builder: (context)=> YourPageRoute())); } } }
© www.soinside.com 2019 - 2024. All rights reserved.