点击通知路由到特定的颤振屏幕
Posted
技术标签:
【中文标题】点击通知路由到特定的颤振屏幕【英文标题】:Routing to Specific flutter Screen on click notification 【发布时间】:2020-02-16 04:18:27 【问题描述】:如何在点击通知上转到特定屏幕?我在 javascript 中使用了云功能,当我点击通知时,它会打开我的应用程序而不是特定屏幕
_fcm.configure(
onMessage: (Map<String, dynamic> message) async
print("onMessage: $message");
getOrder(message['data']['order_id']);
showDialog(
context: context,
builder: (context) => AlertDialog(
content: ListTile(
title: Text(message['notification']['title']),
subtitle: Text(message['notification']['body']),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
,
onLaunch: (Map<String, dynamic> message) async
print("onLaunch: $message");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailOrder()));
,
onResume: (Map<String, dynamic> message) async
print("onResume: $message");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailOrder()));
,
);
【问题讨论】:
【参考方案1】:您可以尝试使用命名路由 + 全局导航键,它在稍微不同的情况下对我有用。 首先像这样设置一个全局导航键:
...
class MyApp extends StatelessWidget
static final navigatorKey = GlobalKey<NavigatorState>();
...
然后在下面定义命名路由管理器:
...
Route routes(RouteSettings settings)
if (settings.name == "/")
return MaterialPageRoute(
builder: (_) => HomePage(),
);
else if (settings.name.startsWith("/details/"))
try
String id = settings.name.split("/")[2];
return MaterialPageRoute(
builder: (_) => DetailPage(id),
);
catch (e)
return MaterialPageRoute(
builder: (_) => HomePage(),
);
else
return MaterialPageRoute(
builder: (_) => HomePage(),
);
...
并将 navigatorKey 和路由添加到 MaterialApp:
...
@override
Widget build(BuildContext context)
return MaterialApp(
initialRoute: "/",
onGenerateRoute: routes,
title: 'MyApp',
navigatorKey: navigatorKey,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
...
设置完所有这些后,您可以使用导航键在主页和详细信息之间导航,如下所示:
MyApp.navigatorKey.currentState.pushNamed("/"); // to navigate to HomePage
MyApp.navigatorKey.currentState.pushNamed("/details/1"); // to navigate to DetailPage
希望它有效,祝你好运!
【讨论】:
@DjamilaJada 如果此答案有帮助,请将其标记为“正确”也是一种表示感谢的方式。以上是关于点击通知路由到特定的颤振屏幕的主要内容,如果未能解决你的问题,请参考以下文章