firebase 和 flutter 本地通知都处理后台通知

Tab*_*san 5 push-notification flutter firebase-cloud-messaging flutter-local-notification

当后台 firebase 中的应用程序首先处理通知,然后 flutter 本地通知在屏幕上显示弹出窗口以使 firebase 不显示通知时,我面临显示双重通知的问题

ps 删除
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

使应用程序不处理后台通知

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

    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
Run Code Online (Sandbox Code Playgroud)

firebaseMessagingBackgroundHandler:

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage? message) async {

  _showNotification(
      id: 0,
      title: message?.notification?.title ?? '',
      body: message?.notification?.body ?? '');

}
Run Code Online (Sandbox Code Playgroud)

firebase 包在通知标志中包含标题和正文,即使应用程序在后台工作,以任何方式使通知仅由 flutter_local_notification 包处理

Bai*_*kar 1

  • 一个可能的问题是 local_notification_package 的初始化

它应该是最顶层的初始化,即在调用函数之前初始化flutter_local_notifications包和android通道_showNotification

  • 另一个可能的问题是 firebase 的初始化。它应该在应用程序运行之前完成,因为它是前台通知