【发布时间】:2021-11-17 19:05:36
【问题描述】:
我正在用 Flutter 开发一个应用程序,但遇到了一些问题。我想要发生的是
- 通过某些事件,使用 FCM 向用户发送推送通知。我同时发送
notification和data消息,以便调用onBackgroundMessage处理程序。 - 每次收到消息时,处理程序都会将徽章计数增加 1。
我的消息负载如下所示:
const payload = {
notification: {
title: String(data["title"]),
body: String(data["message"]),
},
data: {
title: data["title"],
body: data["message"],
},
}
const payloadBackground = {
data: {
title: data["title"],
body: data["message"],
},
content_available: true,
priority: "high"
}
这是使用 FirebaseMessaging 作为 Firebase 云函数运行的:
admin.messaging().sendToDevice(receiverRegistrationTokens, payloadBackground).catch(err => console.error(err));
return admin.messaging().sendToDevice(receiverRegistrationTokens, payload).then( response => {
console.log(receiver + " has registrationTokens: " + receiverRegistrationTokens);
return null;
})
在客户端,通知是这样处理的:
Future<void> backgroundNotificationHandler(RemoteMessage message) {
FlutterAppBadger.updateBadgeCount(1);
debugPrint("Received notification: " + message.data.toString());
return null;
}
AwesomeNotifications().initialize(
// set the icon to null if you want to use the default app icon
'resource://drawable/res_app_icon',
[
NotificationChannel(
channelKey: 'basic_channel',
channelName: 'Basic notifications',
channelDescription: 'Notification channel for basic tests',
defaultColor: Color(0xFF9D50DD),
ledColor: Colors.white,
enableLights: true,
)
]);
FirebaseMessaging.onBackgroundMessage(backgroundNotificationHandler);
由于某种原因,iOS 上的徽章数量没有增加(根本没有徽章)。但是,推送通知会正确显示。关于如何解决这个问题的任何想法?非常感谢!
【问题讨论】:
-
你在 backgroundNotificationHandler 中得到
debugPrint输出了吗? -
不,我不知道,但我想这是正常的,因为只有在应用程序处于后台或终止时才会调用处理程序
-
onBackgroundMessage是在任何类之外的顶级函数吗?你在iOS模拟器真机上试试吗? -
是的,这是一个顶级功能,我们已经通过 TestFlight 在真实的 iOS 设备上对其进行了测试,以确保推送通知包含所有必要的证书
-
在 iOS 上,后台通知只会在后台到达,而不是在终止时到达。我不确定,但尝试将
content_available放在notification中,之前确实需要一条纯数据消息,但也许它适用于较新版本中的通知。
标签: ios flutter firebase-cloud-messaging