Reputation: 769
the docs for the firebase_messaging package for Flutter recommends that we call the configure function as early as possible in the app's life-cycle. I understand why this is recommended. Currently my app has this structure, when the user first opens the app they go to "FirstPage" which decides whether to take them to the "LoginPage" or the "MainPageOfApp" if they're already logged in. To Move to either page, I use PushReplacement so the context of "FirstPage" is gone.
Technically, I should call firebase_messaging's configure in "FirstPage" as it is the earliest chance, but this won't work since I will lose the context of first page when I navigate to either of the two subsequent pages (LoginPage or MainPage). This is a problem because I configure firebase_messaging to navigate to a certain page when a notification is received but no the context of when firebase_messaging was configured is gone.
My other solution is to configure firebase_messaging in MainPage but the problem is that when I receive a notification when the app is terminated (on_launch configuration), the app first opens MainPage then it navigates to the page that the notification tells it to open (performs what it was configured to do in on_launch). Expected I guess.
My problem is, how do I configure firebase_messaging in FirstPage given that I will lose its context since I will use Navigator.pushReplacement to go to the next page (either "LoginPage" or "MainPage").
Thanks!
Upvotes: 1
Views: 1169
Reputation: 301
I had this problem too. there is a package called get: https://pub.dev/packages/get
in your main you can change MaterialApp to GetMaterialApp and import: import 'package:get/get_navigation/get_navigation.dart';
after go to where you have: _firebaseMessaging.configure(); then just create a function:
goToNext(){
Get.to("The page you want to go to");
}
then in _firebaseMessaging.configure(); call it
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
goToNext();
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
goToNext();
},
Upvotes: 0
Reputation: 1152
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_navigateToItemDetail(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_navigateToItemDetail(message);
},
);
and you navigation function would be like :
void _navigateToItemDetail(Map<String, dynamic> message) {
final Item item = _itemForMessage(message);
// Clear away dialogs
Navigator.popUntil(context, (Route<dynamic> route) => route is PageRoute);
if (!item.route.isCurrent) {
Navigator.push(context, item.route);
}
}
Upvotes: 1
Reputation: 51
I found a solution.
Use :
GlobalKey<NavigatorState>
According to this : How to navigate without context in flutter app?
And pass the key to your FirebaseMessaging.configure() method.
Then when message is coming, the context is not there but you have the reference key to the navigator and you can use it.
Upvotes: 1