8

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,
        );
  }
Muhammad
  • 601
  • 2
  • 9
  • 18

2 Answers2

17
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

Dung Nguyen
  • 214
  • 2
  • 3
  • This solution is Good. But it's not clearing the last payload & that's why if anyone launches this app after the termination you will see the last invoked payload. – Tushar Roy Aug 25 '21 at 17:50
  • 1
    when I click on the motification , payload return empty string .. why? – Ahmed Elsayed Jun 19 '22 at 13:59
4

The above solution is Good. But It is not enough for my application. My scenario is - after clicking on the notification it will redirect to a detail page. foreground & background is working fine but the problem was for the terminated application. So I did like -

Notification_plugin.dart

Future<void> showNotification(RemoteMessage message, [bool importance = true]) async {
    dynamic notification = message.data;
    final prefs = await SharedPreferences.getInstance();

    // check message ID is valid or not
    prefs.setBool('hasMsgId', message.messageId != null ? true : false);

    await flutterLocalNotificationsPlugin.show(
      message.hashCode,
      notification['title'],
      notification['body'],
      _setPlatFormSpecificSettings(importance),
      payload: notification['docId'],
    );
}

ItemScreen.dart

@override
void initState() {
  super.initState();
  _runWhileAppIsTerminated();
}

void _runWhileAppIsTerminated() async {
    var details = await notificationPlugin.flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
    
    if (details.didNotificationLaunchApp) {
      if (details.payload != null) {
        final prefs = await SharedPreferences.getInstance();
        
        if (prefs.getBool('hasMsgId')) {
          prefs.remove('hasMsgId');
          
          if (prefs.get('authId').toString() != null) {
            Navigator.of(context).pushNamed(
              ItemDetailsScreen.routeName,
              arguments: details.payload,
            );
          } else {
            Navigator.of(context).pushReplacementNamed(AuthScreen.routeName);
          }
        }
      }
    }
}

I hope it will be helpful for anyone. Thanks!!!

Tushar Roy
  • 1,018
  • 10
  • 18