why routing is not working when tap on the notification in flutter? ( the project is based on GetX and flutter local push notification package )

Issue

Good day.
I know that we use the onSelectNotification property to get the callback when the user tap on the notification in using the flutter local push notification package.

  Future<void> _initialize() async {
    await _configureLocalTimeZone();
    AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');

    IOSInitializationSettings initializationSettingsIOS =
        IOSInitializationSettings(
            onDidReceiveLocalNotification: _onDidReceiveLocalNotification);

    InitializationSettings initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);

    await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: _onSelectNotification);        // This is for the callback
  }

This is the _onSelectsNotification function.

  Future _onSelectNotification(String payload) async {
    if (payload != null) {
      print('notification payload: $payload');
    }
    BuildContext context;
    await Navigator.of(context)
        .push(MaterialPageRoute(builder: (BuildContext context) {
      return NotificationsView();
    }));                           // ***** This is not working. with no error.
  }

I am not sure what to use for the context. I think the next route to the NotificationView() is not working because of the context value.
Could you please help me with this issue?
I am now using the GetX for the state management.

Thanks.

Solution

I solved this problem with Get.toNamed()
My fault was in not returning in the _onSelectNotification function.

  Future _onSelectNotification(String payload) async {
    if (payload != null) {
      print('notification payload: $payload');
    }
    return Get.toNamed(Routes.NOTIFICATIONS);
  }

This is working well.

But I still don’t know how to get the context from another state class.
In the previous post, I passed the null for the context.
I know that this was wrong. How to get?

Answered By – Speeder

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *