简体   繁体   中英

How to display multiple notifications using flutter plugin : flutter_local_notifications

Here is my code below :

const AndroidNotificationChannel notificationChannel = AndroidNotificationChannel(
  'high_importance_channel',
  'high importance Notificaion',
  'this channel is used for import notification',
  importance: Importance.high,
  playSound: true,
);

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();


Future<void> firebaseBackgroundMessageHandler(RemoteMessage message) async {
  await firebaseMessageHandler(message);
}

Future<void> firebaseForegroundMessageHandler(RemoteMessage message) async {
  await firebaseMessageHandler(message);
}

Future<void> firebaseMessageHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  try {
    flutterLocalNotificationsPlugin.show(
          message.notification.hashCode,
          message.data["title"].toString(),
          message.data["body"].toString(),
          NotificationDetails(
            android: AndroidNotificationDetails(notificationChannel.id, notificationChannel.name, notificationChannel.description),
          ));
  } catch (_err) {}

}


Future<void> main() async {

  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  FirebaseMessaging.onBackgroundMessage(firebaseBackgroundMessageHandler);
  FirebaseMessaging.onMessage.listen(firebaseForegroundMessageHandler);
  //FirebaseMessaging.onMessageOpenedApp.listen(firebaseMessageHandler);

  final AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('e_app');

  final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid);

  await flutterLocalNotificationsPlugin.initialize(initializationSettings);

  await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(notificationChannel);

  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true);
}

Code is working fine for a single notification but whenever i send multiple notifications to android, it shows only last notification in status bar.

Could any one help me about how to show multiple notifications in status bar using flutter local notification plugin ?

If you want to group your notifications, there is an option to do that in flutter_local_notifications. To do this, you can add the following in your code

for ios : threadIdentifier in IOSNotificationDetails

for android : groupChannelId, groupChannelName, groupChannelDescription,

You can find these in the docs here: https://pub.dev/packages/flutter_local_notifications#displaying-a-notification under 'Grouping notifications'.

Basically notifications under same threadId or groupId will all be grouped and shown

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM