Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
israel-carreno-yohana opened this issue Nov 8, 2024 · 0 comments

Comments

@israel-carreno-yohana
Copy link

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

@israel-carreno-yohana israel-carreno-yohana changed the title Push Notifications Automatically Clear on App Open with Firebase and flutter_local_notifications [flutter_local_notifications] Push Notifications Automatically Clear on App Open with Firebase and flutter_local_notifications Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant