2

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

Mikel Tawfik
  • 658
  • 3
  • 9
  • 23

3 Answers3

2

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()));
Ping Woo
  • 1,423
  • 15
  • 21
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

Dung Nguyen
  • 214
  • 2
  • 3
1

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(),
    ),
  );
}
Mikel Tawfik
  • 658
  • 3
  • 9
  • 23
  • 2
    this is confused, onSelectNotification vs selectNotificationSubject? can you give more specific details? – Ping Woo Aug 15 '20 at 07:15