I have an app that recieves push notification using OneSignal. I have made a notification opened handler that should open specific screen on click of the notification. How can i navigate to a screen without context. or how can I open specific screen on app startup. My code:
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"]), ), ); } });
I am able to achieve this when the app is opened for the first time but It only opens the homepage If i close the app and even if I re-open it. I guess that is because the context is changed.
Please Help!!
navigatorKey. A key to use when building the Navigator. If a navigatorKey is specified, the Navigator can be directly manipulated without first obtaining it from a BuildContext via Navigator. of: from the navigatorKey, use the GlobalKey. currentState getter.
Replace the current route of the navigator that most tightly encloses the given context by pushing the given route and then disposing the previous route once the new route has finished animating in.
Look at this here: https://github.com/brianegan/flutter_redux/issues/5#issuecomment-361215074
You can set a global key for your navigation:
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
Pass it to MaterialApp:
new MaterialApp( title: 'MyApp', onGenerateRoute: generateRoute, navigatorKey: navigatorKey, );
Push routes:
navigatorKey.currentState.pushNamed('/someRoute');
You can use this wonderful plugin: https://pub.dev/packages/get
Description from the package: A consistent navigation library that lets you navigate between screens, open dialogs, and display snackbars from anywhere in your code without context.
Get.to(NextScreen()); // look at this simplicity :) Get.back(); // pop() Get.off(NextScreen()); // clears the previous routes and opens a new screen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With