Home:ALL Converter>How to show flutter local notification while receiving push notification on foreground?

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

Ask Time:2020-03-17T20:08:05         Author:Zero Live

Json Formatter

Currently, I am using alert dialog to show the notification while receiving push notification while the app is in foreground. But I want to show something non-intrusive like local notification in flutter. How do I implement that in my application? Here is my current implementation:

FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  @override
  void initState() {
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        showDialog(
          context: context,
          builder: (context) => AlertDialog(
            content: ListTile(
              title: Text(message['notification']['title']),
              subtitle: Text(message['notification']['body']),
            ),
            actions: <Widget>[
              FlatButton(
                child: Text('Ok'),
                onPressed: () => Navigator.of(context).pop(),
              ),
            ],
          ),
        );
        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");
      },
    );
}

Author:Zero Live,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60722108/how-to-show-flutter-local-notification-while-receiving-push-notification-on-fore
Zero Live :

Since I didn't get an answer I tried on my own and figured it out.\nDo not forget to add the flutter_local_notifications library to your project for working this code\nhttps://pub.dev/packages/flutter_local_notifications\nTESTED AND WORKING FOR ANDROID\nFirebaseMessaging _firebaseMessaging = FirebaseMessaging();\nFlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();\n @override\n void initState() {\nvar initializationSettingsAndroid =\n new AndroidInitializationSettings('@mipmap/ic_launcher');\n var initializationSettingsIOS = new IOSInitializationSettings();\n var initializationSettings = new InitializationSettings(\n initializationSettingsAndroid, initializationSettingsIOS);\n flutterLocalNotificationsPlugin.initialize(initializationSettings,\n onSelectNotification: onSelectNotification);\n _firebaseMessaging.configure(\n onMessage: (Map<String, dynamic> message) async {\n showNotification(\n message['notification']['title'], message['notification']['body']);\n print("onMessage: $message");\n },\n onLaunch: (Map<String, dynamic> message) async {\n print("onLaunch: $message");\n Navigator.pushNamed(context, '/notify');\n },\n onResume: (Map<String, dynamic> message) async {\n print("onResume: $message");\n },\n );\n}\nFuture onSelectNotification(String payload) async {\n showDialog(\n context: context,\n builder: (_) {\n return new AlertDialog(\n title: Text("PayLoad"),\n content: Text("Payload : $payload"),\n );\n },\n );\n }\nvoid showNotification(String title, String body) async {\n await _demoNotification(title, body);\n }\n\n Future<void> _demoNotification(String title, String body) async {\n var androidPlatformChannelSpecifics = AndroidNotificationDetails(\n 'channel_ID', 'channel name', 'channel description',\n importance: Importance.Max,\n playSound: true,\n sound: 'sound',\n showProgress: true,\n priority: Priority.High,\n ticker: 'test ticker');\n\n var iOSChannelSpecifics = IOSNotificationDetails();\n var platformChannelSpecifics = NotificationDetails(\n androidPlatformChannelSpecifics, iOSChannelSpecifics);\n await flutterLocalNotificationsPlugin\n .show(0, title, body, platformChannelSpecifics, payload: 'test');\n }\n\nIt works like a charm. Hopefully will help someone out :)",
2020-03-25T14:02:27
yy