我正在尝试将Firebase Cloud Messaging通知中的数据添加到列表中。
final List<FCMContent> fcmcrafts = [];
列表是从外部PHP页面下载的JSON数据创建的。
if (response.body.contains('audioid')) {
List fcmcrafts = json.decode(response.body);
return fcmcrafts.map((fcmcraft) => FCMContent.fromJson(fcmcraft)).toList();
}
这是我为FCMContent创建的类;
class FCMContent {
final String audioid, name, title;
FCMContent({
this.audioid,
this.name,
this.title,
});
factory FCMContent.fromJson(Map<String, dynamic> jsonData) =>FCMContent(
audioid: jsonData['audioid'],
name: jsonData['name'],
title: jsonData['title']
);
Map<String, dynamic> toJson() => {
"audioid":audioid,
"name":name,
"title":title
};
}
如何将FCM通知添加到列表中?如果它是一个字符串,我将这样添加它。
@override
void onNotify(RemoteMessage notification) {
_firebaseMessagingBackgroundHandler(notification);
setState(() {
_notification = notification;
fcmcrafts.insert(0, _notification?.notification?.title ?? "");
});
}
但是我如何添加作为JSON代码到达的FCM通知?
你就不能这样做吗
fcmcrafts。插入(0,FCMContent.fromJson (_notification));
你只需要将RemoteMessage转换为FCMContent,也许你可以创建一个自定义构造函数FCMContent. fromremotemessage()并映射字段。