如何使用Flutter插件显示多个通知:Flutter本地通知

p5cysglq  于 2023-05-08  发布在  Flutter
关注(0)|答案(2)|浏览(283)

下面是我的代码:

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);
}

代码对于单个通知工作正常,但每当我向android发送多个通知时,它只在状态栏中显示最后一个通知。
谁能帮我一下如何使用flutter本地通知插件在状态栏中显示多个通知?

hs1ihplo

hs1ihplo1#

如果你想对通知进行分组,flutter_local_notifications中有一个选项。为此,可以在代码中添加以下内容
for ios:threadIdentifier in IOSNotificationDetails
对于Android:groupChannelId,groupChannelName,groupChannelDescription,
你可以在这里的文档中找到这些:https://pub.dev/packages/flutter_local_notifications#displaying-a-notification,位于“分组通知”下。
基本上相同threadId或groupId下的通知都会被分组显示

oxiaedzo

oxiaedzo2#

flutter show通知id将是唯一的

**int id = (DateTime.now().microsecond.toInt() +
    DateTime.now().millisecond.toInt() +
    DateTime.now().second.toInt());**

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(
         **id,**
          message.data["title"].toString(),
          message.data["body"].toString(),
          NotificationDetails(
            android: AndroidNotificationDetails(notificationChannel.id, notificationChannel.name, notificationChannel.description),
          ));
id = (DateTime.now().microsecond.toInt() +
    DateTime.now().millisecond.toInt() +
    DateTime.now().second.toInt());
  } 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);
}
int id = (DateTime.now().microsecond.toInt() +
    DateTime.now().millisecond.toInt() +
    DateTime.now().second.toInt());
 flutterLocalNotificationsPlugin.show(
        id,
        message.data["title"],
        message.data["body"],
        NotificationDetails(
          android: AndroidNotificationDetails(
            channel!.id,
            channel!.name,
            channelDescription: channel!.description,
            icon: '@mipmap/ic_launcher'
          ),
        ),
        payload: json.encode(message.data));
    id = (DateTime.now().microsecond.toInt() +
        DateTime.now().millisecond.toInt() +
        DateTime.now().second.toInt());

Android通知json格式

{
    "message": {
        "data": {
            "title": "Welcome message12",
            "body": "Firebase Nodification Success🙂",
            "click_action": "FLUTTER_NOTIFICATION_CLICK"
        }
    },
    "registrationToken": "fcm token here"
}

这种格式的JSON是自动禁用FCM通知

相关问题