0

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.

Speeder
  • 120
  • 3
  • 12

2 Answers2

0

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?

Speeder
  • 120
  • 3
  • 12
0

The code is not working because, at the time you call your Navigator, the widget is not finished building. A simple fix is to wrap the Navigator.of(context) with Future, like this:

Future.delayed(Duration.zero).then((_){
Navigator.of(context).push(
    MaterialPageRoute(builder: (BuildContext context) {
      return NotificationsView();
    }
  ));
});
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83