1

I am following this answer to handle what happens when a user clicks on a notification: https://stackoverflow.com/a/74288639/4838216

When the user clicks my basic notification, I want it to take them to the notifications page. However, using Navigator.push(... requires a BuildContext. I don't think I have one in this case because this is a helper class called FirebaseHelper. How do I get the context to use here? Or is there another way to do this?

//This method will call when the app is in background state
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage? message) {
      if (message != null) {
        //Handle push notification redirection here
        print('##MyApp## _setUpNotificationListener onMessageOpenedApp.listen');
        Navigator.push(context, MaterialPageRoute(builder: (context) => ScreenNotifications()),);
      }
    });
whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50

1 Answers1

1

little bit cumbersome when using default flutter navigation ,because it deals with context , So I suggest you to use Getx package which has many functionalities need of context to navigate. Speed up the dev process.

https://pub.dev/packages/get

Get.to(NextScreen());
ex: Get.to(ScreenNotifications())

just move you to next page. see more on the documentation.

 FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage? message) {
      if (message != null) {
        //Handle push notification redirection here
        print('##MyApp## _setUpNotificationListener onMessageOpenedApp.listen');
        Get.to(ScreenNotifications());
      }
    });
Thusitha Deepal
  • 1,392
  • 12
  • 19