如何在点击通知时转到特定屏幕? 我在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()));
},
);
您可以尝试使用命名路线+全局导航器键,它在稍微不同的情况下对我有用。
首先设置一个全局导航器键,如下所示:
...
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(),
);
}
}
...
并将导航键和路由添加到材料应用程序:
...
@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
希望它有效,祝你好运!