I try to push a new route when user opens a notification but I get the following error:
FlutterFire Messaging: An error occurred in your background messaging handler:
You are trying to use contextless navigation without
a GetMaterialApp or Get.key.
If you are testing your app, you can use:
[Get.testMode = true], or if you are running your app on
a physical device or emulator, you must exchange your [MaterialApp]
for a [GetMaterialApp].
This is how I implemented my notifcations:
main.dart
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
await setupFlutterNotifications();
showFlutterNotification(message);
Get.toNamed(message.data['view']); // I get the error in this line
print('Handling a background message ${message.data}');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
if (!kIsWeb) {
await setupFlutterNotifications();
}
runApp(
GetMaterialApp(
title: 'e-Rădăuți',
debugShowCheckedModeBanner: false,
builder: EasyLoading.init(),
getPages: AppPages.routes,
initialRoute:
await IsFirstRun.isFirstRun() ? Routes.ONBOARD : Routes.HOME,
initialBinding: AppBindings(),
navigatorObservers: [
AnalyticsController().getAnalyticsObserver(),
],
theme: ThemeData(
scaffoldBackgroundColor: Color(0xFFFFFFFF),
primaryColor: Color(0xFFFFFFFF),
),
navigatorKey: Get.key,
),
);
}
If I try to push a new screen (not from notifications) it works like expected.
Any idea on what I've done wrong? I've added navigatorKey
and I have replaced [MaterialApp] with [GetMaterialApp]