MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.47k stars 1.4k forks source link

[flutter_local_notifications] Push Notifications Automatically Clear on App Open with Firebase and flutter_local_notifications #2447

Open israel-carreno-yohana opened 1 week ago

israel-carreno-yohana commented 1 week ago

Describe the bug

When receiving multiple push notifications using Firebase and displaying them only when the app is in the background (using flutter_local_notifications), all notifications are unexpectedly cleared when opening any single notification or upon returning to the app. This occurs even though each notification has a unique notificationID, and no code explicitly clears notifications from the notification center.

To Reproduce

firebase_messaging: ^15.1.4 flutter_local_notifications: 18.0.0

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.22.3, on macOS 15.1 24B83 darwin-arm64, locale en-CO)

  1. Integrate Firebase for push notifications and set up flutter_local_notifications to display notifications only in the background.
  2. Send multiple push notifications with unique notificationIDs to ensure they are treated as separate notifications.
  3. Open any one of the notifications from the notification center.
  4. Return to the app and observe the notification center.

Expected behavior

Each notification should remain in the notification center until explicitly dismissed by the user, without automatically clearing other notifications when one is opened

Sample code to reproduce the problem

I have Firebase configured to receive push notifications and flutter_local_notifications to display them in the background. The notification center displays notifications correctly when the app is backgrounded. However, when returning to the app or tapping any notification, all notifications are cleared. No code calls clearAllNotifications or similar actions.

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print('### Handling a background message, MessageID: ${message.messageId}');
  await showNotification(message);
}

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(

);

Future<void> setupFlutterNotifications() async {
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('@mipmap/ic_launcher');
  const DarwinInitializationSettings initializationSettingsIOS =
      DarwinInitializationSettings();
  const InitializationSettings initializationSettings = InitializationSettings(
    android: initializationSettingsAndroid,
    iOS: initializationSettingsIOS,
  );

  await flutterLocalNotificationsPlugin.initialize(
    initializationSettings,

  );
}
Future<void> showNotification(RemoteMessage message) async {

  final channelId =  message.data['channel_id'];

  final AndroidNotificationDetails androidNotificationDetails =
      AndroidNotificationDetails(
    channelId,
    channelId+'description',
    importance: Importance.high,
    priority: Priority.max,
    showWhen: true,
    autoCancel: false, 
  );

  final NotificationDetails notificationDetails = NotificationDetails(
    android: androidNotificationDetails,
  );

  int notificationId = DateTime.now().millisecondsSinceEpoch.remainder(100000);
  log('### Notification ID: $notificationId');

  await flutterLocalNotificationsPlugin.show(
    notificationId,
   notificationId.toString(),
    '${notificationId.toString()} body' ,
    notificationDetails,
    payload: message.data['deep_link'],

  );
}

void main() async {
.
.
.

  await setupFlutterNotifications();

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
}