4

in flutter_local_notification 12.0.2

The onSelectNotification parameter is not anymore. So what is the alternative of this. So that we can handle callback when we click on notification.

Zilaid
  • 464
  • 6
  • 22

2 Answers2

0

you can stream the notification

import 'package:rxdart/rxdart.dart';
...
// use this 
class MyNotifManager {
static final onNotifications = BehaviorSubject<String?>();
...

// then add the payload to your local notification
// exampel for  the foreground notif

  onDidReceiveNotificationResponse: (payload) async {
        onNotifications.add(payload.payload); // add payload to the stream
  },

then to handle the callback:

Future<void> listenNotification() async =>
      MyNotifManager.onNotifications.stream.listen(onClickNotification);

and for your action after click

void onClickNotification(String? payload) {
  Navigator.push();
}

call the stream on your initState

@override
  void initState() {
    super.initState();
    MyNotifManager.init();
    listenNotification();
  }

with this method, you will able to handle calback when click on notification.

pmatatias
  • 3,491
  • 3
  • 10
  • 30
0

It has onDidReceiveNotificationResponse callback when plugin initialize.

    _plugin.initialize(
      initializationSettings,
      onDidReceiveNotificationResponse: (details) {
          // Click Notification Event Here
      },
    );

If you click notification when app is terminated,

    _plugin.getNotificationAppLaunchDetails().then((value) {
         // Click Notification Event Here
    });
  • Does this work better than the old version? Coz honestly, the old version could never really be trusted... And it would trigger the ´onSelectNotification´ event many times, and stuff... – Karolina Hagegård Jul 03 '23 at 17:56
  • This code doesn't support at old version. In old version, plz use `onSelectNotification`. If you use new version of flutter_location_notification, you can use `onDidReceiveNotificationResponse` when app state is foreground. if app is open by clicking notification when app state is terminated, you can use `getNotificationAppLaunchDetails` in app init() or wherever you want to use. I think it would be better than old version. https://pub.dev/packages/flutter_local_notifications#notification-actions – Htet Aung Lin Jul 08 '23 at 15:00
  • Yes, I got that, and it was of great help thanks! I just wanted to hear your spontaneous comment on whether or not it works better after the update or not... – Karolina Hagegård Jul 09 '23 at 17:13