Firebase和Flutter本地通知都可以处理后台通知。

5
我在应用程序后台时遇到了一个问题,当Firebase先处理通知,然后Flutter本地通知在屏幕上显示弹出窗口时,会出现双重通知的情况。 有没有办法让Firebase不显示通知?
附注:删除FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler); 可以使应用程序在后台不处理通知。
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 ?? '');

}

即使应用程序在后台运行,Firebase包中的通知标志中仍包含标题和正文。是否有方法使通知仅由flutter_local_notification包处理?

3个回答

1
  • 可能的一个问题是你本地通知包的初始化。

应该是最高级别的初始化,即在调用_showNotification函数之前初始化flutter_local_notifications包和Android通道。

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

已经完成了。 - Tabarek Ghassan

1
您的firebaseMessagingBackgroundHandler应该如下所示...

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

}


它仍然显示两个通知。有没有办法停止Flutter本地通知在设备通知中心显示通知?这将是解决该问题的一种方法。 - Tabarek Ghassan

0

我遇到了相同的问题,直到添加了

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />

在Android清单文件中。
此外,请记住high_importance_channel的值可以是您想要的任何内容,但它应与您在Flutter代码中声明的相同,如下所示:
const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'high_importance_channel', //channel id
  'High Importance Notifications', //title
  'This channel is used for important notifications.', //description
  importance: Importance.max,
);

你可以在这里找到更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接