0

I am trying to navigate to a different screen using Get when a user taps on the notification triggered from the onMessage function while the app is on foreground. So far I have not been able to do that.

Here's what I have done so far:

  void initState(){ 
    super.initState();
    var initializationSettingsAndroid  = AndroidInitializationSettings('@drawable/splash');
    var initializationSettings = InitializationSettings(android:initializationSettingsAndroid);
    flutterLocalNotificationsPlugin.initialize(initializationSettings);
    
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channel.description,
                icon: '@drawable/splash',
                playSound: true
              ),
            ));
            Get.to(OrdersScreen());
      }
    });
  }

I get the notification but when I tap, nothing happens. I also get the following exception in the logs.

E/flutter (17567): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: You are trying to use contextless navigation without E/flutter (17567): a GetMaterialApp or Get.key. E/flutter (17567): If you are testing your app, you can use: E/flutter (17567): [Get.testMode = true], or if you are running your app on E/flutter (17567): a physical device or emulator, you must exchange your [MaterialApp] E/flutter (17567): for a [GetMaterialApp]. E/flutter (17567): E/flutter (17567): #0
GetNavigation.global package:get/…/src/extension_navigation.dart:1052 E/flutter (17567): #1 GetNavigation.to package:get/…/src/extension_navigation.dart:511 E/flutter (17567): #2 _MyAppState.initState. package:flutter_complete_guide/main.dart:146 E/flutter (17567): #3
_rootRunUnary (dart:async/zone.dart:1362:47) E/flutter (17567): #4 _CustomZone.runUnary (dart:async/zone.dart:1265:19) E/flutter (17567): #5 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7) E/flutter (17567): #6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11) E/flutter (17567): #7
_DelayedData.perform (dart:async/stream_impl.dart:591:14) E/flutter (17567): #8 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11) E/flutter (17567): #9
_PendingEvents.schedule. (dart:async/stream_impl.dart:663:7) E/flutter (17567): #10
_rootRun (dart:async/zone.dart:1346:47) E/flutter (17567): #11 _CustomZone.run (dart:async/zone.dart:1258:19) E/flutter (17567): #12 _CustomZone.runGuarded (dart:async/zone.dart:1162:7) E/flutter (17567): #13 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1202:23) E/flutter (17567): #14 _rootRun (dart:async/zone.dart:1354:13) E/flutter (17567): #15
_CustomZone.run (dart:async/zone.dart:1258:19) E/flutter (17567): #16 _CustomZone.runGuarded (dart:async/zone.dart:1162:7) E/flutter (17567): #17 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1202:23) E/flutter (17567): #18
_microtaskLoop (dart:async/schedule_microtask.dart:40:21) E/flutter (17567): #19 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5) E/flutter (17567): D/ViewRootImpl@874c66fMainActivity: stopped(false) old=false

mcfred
  • 1,183
  • 2
  • 29
  • 68
  • 1
    My understanding is that onMessageOpenedApp is fired when the app is in the background state. – mcfred Jun 18 '21 at 17:49

2 Answers2

1

I was able to do this using the navigatorkey.

Created the global key:

final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator");

Initialized it in the MaterialApp:

MaterialApp( 
        navigatorKey: navigatorKey,   

The used it inside onMessage function:

navigatorKey.currentState.push(
    MaterialPageRoute(builder: (_) => OrdersScreen()));
mcfred
  • 1,183
  • 2
  • 29
  • 68
0

to open a notification you have to use this

 FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
        // print('A new onMessageOpenedApp event was published!');

         Get.to(OrdersScreen());
      });
Fatima ayaa
  • 610
  • 6
  • 16