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 in my Firebase Background Message Handler so I searched on the internet and I found that I can get the context from the navigator key 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 and the desired screen but I always get the null context so couldn't Push. Navigating through GetX also throws error.
What am I missing? Please help!!!!
myFB 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()));
}
}