如何根据消息标题显示FCM后台通知?

问题描述 投票:0回答:1

我正在使用 fcm 制作一个 flutter 应用程序(Android)。 我想仅当消息标题为“出勤”时才向客户端应用程序发送通知。

我尝试使用if-else语句来解决它。 它在前台运行良好,但在后台,if 语句不起作用。

这是前台消息。效果很好

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
      RemoteNotification? notification = message.notification;

      if (notification != null) {
        String title = notification.title ?? "";
        String body = notification.body ?? "";
        
        // Show forground notification only in that condition 
        if (title == "attendance") {
          FlutterLocalNotificationsPlugin().show(
          notification.hashCode,
          title,
          body,

          // set details in Android
          const NotificationDetails(
            android: AndroidNotificationDetails(
              'high_importance_channel',              
              'high_importance_notification',           
              importance: Importance.max,
              priority: Priority.max,
            ),),
          );
        }
         else {
        }
      }
    });

这是后台消息处理程序。它不起作用 无论标题是什么,它都会显示通知

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
 // Show Background notification only in that condition 
  if (message.notification?.title == "attendance") {
  final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(const AndroidNotificationChannel(     
          'high_importance_channel', 
          'high_importance_notification',
          importance: Importance.max,
      ));
  }
  else {
  }
}

请帮助我! 这是我第一次使用 fcm 所以我不太了解:(

我尝试用 if-else 条件语句来实现它,但它在前台工作,但在后台不起作用。

android flutter dart firebase-cloud-messaging
1个回答
0
投票

删除“notification”键值对并添加“content_available”:true

它看起来像这样

{ 
    "to" : "...",
    "priority": "high",
    "content_available": true,
    "data" : {
      ....
    }
}

这应该发送无声通知。

https://help.twilio.com/articles/4411699713947-FCM-Push-Notifications-only-as-Data-messages

© www.soinside.com 2019 - 2024. All rights reserved.