How to show flutter local notification while receiving push notification on foreground? How to show flutter local notification while receiving push notification on foreground? dart dart

How to show flutter local notification while receiving push notification on foreground?


Since I didn't get an answer I tried on my own and figured it out.

Do not forget to add the flutter_local_notifications library to your project for working this codehttps://pub.dev/packages/flutter_local_notifications

TESTED AND WORKING FOR ANDROID

FirebaseMessaging _firebaseMessaging = FirebaseMessaging();FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();  @override  void initState() {var initializationSettingsAndroid =        new AndroidInitializationSettings('@mipmap/ic_launcher');    var initializationSettingsIOS = new IOSInitializationSettings();    var initializationSettings = new InitializationSettings(        initializationSettingsAndroid, initializationSettingsIOS);    flutterLocalNotificationsPlugin.initialize(initializationSettings,        onSelectNotification: onSelectNotification);    _firebaseMessaging.configure(      onMessage: (Map<String, dynamic> message) async {        showNotification(            message['notification']['title'], message['notification']['body']);        print("onMessage: $message");      },      onLaunch: (Map<String, dynamic> message) async {        print("onLaunch: $message");        Navigator.pushNamed(context, '/notify');      },      onResume: (Map<String, dynamic> message) async {        print("onResume: $message");      },    );}Future onSelectNotification(String payload) async {    showDialog(      context: context,      builder: (_) {        return new AlertDialog(          title: Text("PayLoad"),          content: Text("Payload : $payload"),        );      },    );  }void showNotification(String title, String body) async {    await _demoNotification(title, body);  }  Future<void> _demoNotification(String title, String body) async {    var androidPlatformChannelSpecifics = AndroidNotificationDetails(        'channel_ID', 'channel name', 'channel description',        importance: Importance.Max,        playSound: true,        sound: 'sound',        showProgress: true,        priority: Priority.High,        ticker: 'test ticker');    var iOSChannelSpecifics = IOSNotificationDetails();    var platformChannelSpecifics = NotificationDetails(        androidPlatformChannelSpecifics, iOSChannelSpecifics);    await flutterLocalNotificationsPlugin        .show(0, title, body, platformChannelSpecifics, payload: 'test');  }

It works like a charm. Hopefully will help someone out :)


I made this class with a static method init() and call it once from initState of first page of my app

const AndroidNotificationChannel channel = AndroidNotificationChannel(  'my_channel', // id  'My Channel', // title  'Important notifications from my server.', // description  importance: Importance.high,);class Notifications {  static Future init() async {    await Firebase.initializeApp();    FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;        final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =        FlutterLocalNotificationsPlugin();    await flutterLocalNotificationsPlugin        .resolvePlatformSpecificImplementation<            AndroidFlutterLocalNotificationsPlugin>()        ?.createNotificationChannel(channel);    await FirebaseMessaging.instance        .setForegroundNotificationPresentationOptions(      alert: true,      badge: true,      sound: true,    );    FirebaseMessaging.onMessage.listen((RemoteMessage message) {      RemoteNotification notification = message.notification;      AndroidNotification android = message.notification?.android;      if (notification != null && android != null) {        flutterLocalNotificationsPlugin.show(            notification.hashCode,            notification.title,            notification.body,            NotificationDetails(              android: AndroidNotificationDetails(                channel.id,                channel.name,                channel.description,                icon: 'some_icon_in_drawable_folder',              ),            ));      }    });  }}