both firebase and flutter local notifications handle background notificaiton

185

Solution 1

  • One possible problem is the initialization of your local_notification_package

It should be a topmost initialization, i.e you initialize the flutter_local_notifications package and the android channel before calling _showNotification function

  • Another possible problem is the initialization of firebase. It should be done before the app runs, since it is a foreground notification

Solution 2

Your firebaseMessagingBackgroundHandler will look like.......

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage? message) async {
  await Firebase.initializeApp(); //initialize firebase
  //INITIALIZE YOUR FLUTTER LOCAL NOTIFICATION PACKAGE 
  // INITIALIZE YOUR ANDROID CHANNEL
  //then show the notification
  _showNotification(
      id: 0,
      title: message?.notification?.title ?? '',
      body: message?.notification?.body ?? '');

}

Share:
185
Tabarek Ghassan
Author by

Tabarek Ghassan

Updated on January 03, 2023

Comments

  • Tabarek Ghassan
    Tabarek Ghassan 10 months

    I'm facing an issue with displaying double notifications when the app in the background firebase handle the notification first and then the flutter local notification display a popup on the screen anyway to make firebase not show the notification

    p.s remove the
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

    make the app not handle notifications on background

    FirebaseMessaging.onMessage.listen(
          (RemoteMessage? message) async {
          
            _showNotification(
                id: 0,
                title: message?.notification?.title ?? '',
                body: message?.notification?.body ?? '');
          },
        );
    
        FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    

    firebaseMessagingBackgroundHandler:

    Future<void> firebaseMessagingBackgroundHandler(RemoteMessage? message) async {
    
      _showNotification(
          id: 0,
          title: message?.notification?.title ?? '',
          body: message?.notification?.body ?? '');
    
    }
    

    the firebase package contains title and body in the notification flag even if the app working in the background any way to make the notification only handled by flutter_local_notification package

  • Tabarek Ghassan
    Tabarek Ghassan almost 2 years
    already did that
  • Tabarek Ghassan
    Tabarek Ghassan almost 2 years
    the same it still displays two notifications. any way to stop flutter local notification to display the notification in the device notification center?? it will be a workaround for this issue