onSelectNotification Navigator not working if app closed flutter

4,416

Solution 1

the Navigator has to be wrapped as child in MaterialApp widget, otherwise, you have to use navKey to refer you materialApp.

1. final navKey = new GlobalKey<NavigatorState>();

2. MaterialAPP(title: 'title', ..., navigatorKey: navKey)

3. And then use the key to route to your widget, 
navKey.currentState.push(MaterialPageRoute(builder: (context) => NewRoute()));

Solution 2

var details = await NotificationService()
    .flutterLocalNotificationsPlugin
    .getNotificationAppLaunchDetails();
if (details.didNotificationLaunchApp) {
    print(details.payload);
}

Use this code in the app entry it can get notification tapped payload

Solution 3

i contact support of plugin

Here

Future<void> main() async {
  // needed if you intend to initialize in the `main` function
  WidgetsFlutterBinding.ensureInitialized();

  notificationAppLaunchDetails =
      await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();

  var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
  // Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
  // of the `IOSFlutterLocalNotificationsPlugin` class
  var initializationSettingsIOS = IOSInitializationSettings(
      requestAlertPermission: false,
      requestBadgePermission: false,
      requestSoundPermission: false,
      onDidReceiveLocalNotification:
          (int id, String title, String body, String payload) async {
        didReceiveLocalNotificationSubject.add(ReceivedNotification(
            id: id, title: title, body: body, payload: payload));
      });
  var initializationSettings = InitializationSettings(
      initializationSettingsAndroid, initializationSettingsIOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: (String payload) async {
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
    selectNotificationSubject.add(payload);
  });
  runApp(
    MaterialApp(
      home: HomePage(),
    ),
  );
}
Share:
4,416
Mikel Tawfik
Author by

Mikel Tawfik

ceo of egphp.com php developer servers managements nginx support from Egypt cairo

Updated on December 12, 2022

Comments

  • Mikel Tawfik
    Mikel Tawfik over 1 year

    onSelectNotification when good when app open

      Future<void> onSelectNotification(String payload) async {
        Category category = Category();
        category.id = notification.idNew;
        category.email = notification.mainphoto;
        category.since = notification.since;
        category.name = notification.title;
        await Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => FourthRoute(
                      category: category,
                    )));
      }
    

    when open and click at notification it's go to FourthRoute

    but when closed it's just open app Navigator not working

    problem at ios and android

    i was think to use SharedPreferences inside onSelectNotification to save key and check it later

    but i read dart not working when app closed

  • Ping Woo
    Ping Woo over 3 years
    this is confused, onSelectNotification vs selectNotificationSubject? can you give more specific details?