I want to switch to a screen when an FCM background message receives. But for this context
is required which I definitely don't have inside FirebaseBackgroundMessageHandler
so after searching on the internet and I found that I can get the current context from the NavigatorKey
so I created this global variable:
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
and this on my material app:
runApp(GetMaterialApp(
navigatorKey: navigatorKey,
home: const MyHomePage(),));
Now, whenever I receive a background message I try to switch to the desired screen but I always get the null context so couldn't Push
. Navigating through GetX
also throws an error.
What am I missing? Please help!!!!
Firebase background handler:
Future<dynamic> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
if(message.data['test'] == '123'){
Navigator.of(navigatorKey.currentContext!).push(MaterialPageRoute(builder: (context) =>const Wallet()));
}
}