我有一个使用 OneSignal 接收推送通知的应用。我已经制作了一个通知打开处理程序,该处理程序应在单击通知时打开特定屏幕。如何在没有上下文的情况下导航到屏幕。或者如何在应用启动时打开特定屏幕。我的代码:
OneSignal.shared.setNotificationOpenedHandler((notification) {
var notify = notification.notification.payload.additionalData;
if (notify["type"] == "message") {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DM(user: notify['id']),
),
);
}
if (notify["type"] == "user") {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Profileo(notify["id"]),
),
);
}
if (notify["type"] == "post") {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ViewPost(notify["id"]),
),
);
}
});
我可以在第一次打开应用程序时实现这一点,但它只会打开主页如果我关闭应用程序,即使我重新打开它。我猜那是因为上下文发生了变化。
请帮忙!!
最佳答案
看这里: https://github.com/brianegan/flutter_redux/issues/5#issuecomment-361215074
您可以为导航设置全局键:
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
将其传递给 MaterialApp:
new MaterialApp(
title: 'MyApp',
onGenerateRoute: generateRoute,
navigatorKey: navigatorKey,
);
推送路线:
navigatorKey.currentState.pushNamed('/someRoute');
https://stackoverflow.com/questions/52962112/