Home > Software engineering >  do an action after click on notification flutter?
do an action after click on notification flutter?

Time:10-10

hello am trying to navigate to a second screen after clicking on notification using flutter_local_notifications https://pub.dev/packages/flutter_local_notifications

final BehaviorSubject<String?> selectNotificationSubject =
      BehaviorSubject<String?>();
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  void initializeNotifcations() async {
    var initialzationSettings =
        InitializationSettings(android: initializationSettingsAndroid);

    await flutterLocalNotificationsPlugin.initialize(initialzationSettings,
        onDidReceiveBackgroundNotificationResponse:
            (NotificationResponse notificationResponse) {
      selectNotificationSubject.add(notificationResponse.payload);
    });

    flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()!
        .requestPermission();
  }

  void configureDidReceiveLocalNotificationSubject() {
    print('test01');
    selectNotificationSubject.stream.listen((String? payload) async {
      print('test02');
    });
  }

not listining to the stream if you got an example of how to make it with the best practice it will help

CodePudding user response:

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await initNotification();

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler;
  ...
}



    Future initNotification() async {
  var initializationSettingsAndroid =
  new AndroidInitializationSettings("@drawable/kin_removebg");
  var initializationSettingsIOS = new IOSInitializationSettings(
      requestAlertPermission: false,
      requestBadgePermission: false,
      requestSoundPermission: false,
      onDidReceiveLocalNotification:
          (int? id, String? title, String? body, String? payload) async {});

  var initializationSettings = new InitializationSettings(
      android: initializationSettingsAndroid, iOS: initializationSettingsIOS);

  final NotificationAppLaunchDetails? notificationAppLaunchDetails =
  await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
  final didNotificationLaunchApp =
      notificationAppLaunchDetails?.didNotificationLaunchApp ?? false;

  if (didNotificationLaunchApp) {
    var payload = notificationAppLaunchDetails!.payload;
    onSelectNotification(payload);
  } else {
     await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse:onSelectNotification,
onDidReceiveBackgroundNotificationResponse:      onSelectNotification,
  }
}

onSelectNotification(NotificationResponse notificationResponse) async {

  var payloadData = jsonDecode(notificationResponse.payload);
  print("payload $payload");
  if(payloadData["type"]=="something" && payloadData["id"]!="something"){
    Navigator.of(navigatorKey.currentContext!).push(
        MaterialPageRoute(
            builder: (context) =>
                SomethingScreen(id: payloadData["id"],)));
  }


}

CodePudding user response:

flutterLocalNotificationsPlugin
        .getNotificationAppLaunchDetails()
        .then((value) {
        .then((value) async {
      if (value.didNotificationLaunchApp) {
        if (value.payload != null) {
          // do something
        }
  • Related