Home > Blockchain >  both firebase and flutter local notifications handle background notificaiton
both firebase and flutter local notifications handle background notificaiton

Time:01-16

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

CodePudding user response:

  • 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

CodePudding user response:

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 ?? '');

}

  •  Tags:  
  • Related