Flutter : flutter_local_notifications onSelectNotification not working when app is terninated

Issue

flutter_local_notifications is working fine when the app is in foreground or background

but when the app is terminated

onSelectNotification not working as expected , instead it’s just open the application

static void initializeLocalNotification() async {
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('ic_launcher');
    final IOSInitializationSettings initializationSettingsIOS =
        IOSInitializationSettings(
      requestSoundPermission: false,
      requestBadgePermission: false,
      requestAlertPermission: false,
    );

    final InitializationSettings initializationSettings =
        InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
    );
    await flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (String payload) async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      var res = await prefs.setString(kNotificationPayLoad, payload);
      // navigatorKey.currentState
      //     .push(MaterialPageRoute(builder: (_) => LoginScreen()));
      // navigatorKey.currentState.pop();
    });

    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            IOSFlutterLocalNotificationsPlugin>()
        ?.requestPermissions(
          alert: true,
          badge: true,
          sound: true,
        );
  }

Solution

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

Use this code in the first page it can get notification tapped payload

Answered By – Dung Nguyen

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.