我正在使用 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 条件语句来实现它,但它在前台工作,但在后台不起作用。
删除“notification”键值对并添加“content_available”:true
它看起来像这样
{
"to" : "...",
"priority": "high",
"content_available": true,
"data" : {
....
}
}
这应该发送无声通知。
https://help.twilio.com/articles/4411699713947-FCM-Push-Notifications-only-as-Data-messages