Flutter 颤振:如何使用颤振本地通知将模型从API发送到其他屏幕
我在执行时遇到问题 问题是,如何从通知发送自定义数据?因为我在屏幕上看到的只是发送一个Flutter 颤振:如何使用颤振本地通知将模型从API发送到其他屏幕,flutter,notifications,Flutter,Notifications,我在执行时遇到问题 问题是,如何从通知发送自定义数据?因为我在屏幕上看到的只是发送一个有效负载。不要给出如何发送标题、正文或自定义数据的示例,如我的案例(希望从API发送模型数据) 那么如何解决我的案子呢?因为我已经考虑过了,还没有给我一个解决方案,也已经在谷歌上搜索过了 正如您在下面所看到的,在my函数中已经添加了参数modelArticles,并将其添加到title和body,以使通知中的内容动态地基于API。但如何将该模型发送到另一个屏幕?因为属性有效负载仅支持字符串,但我的型号不是字符串
有效负载
。不要给出如何发送标题
、正文
或自定义数据
的示例,如我的案例(希望从API发送模型数据)
那么如何解决我的案子呢?因为我已经考虑过了,还没有给我一个解决方案,也已经在谷歌上搜索过了
正如您在下面所看到的,在my函数中已经添加了参数modelArticles
,并将其添加到title
和body
,以使通知中的内容动态地基于API。但如何将该模型发送到另一个屏幕?因为属性有效负载
仅支持字符串
,但我的型号不是字符串
static Future<void> showNotification(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
Articles articles) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, articles.title, articles.description, platformChannelSpecifics,
payload: 'i want to drop articles parameter here'); // the problem here
}
正如您再次看到的,listen仅支持字符串
,但是当单击通知并打开详细信息页面时,它需要来自API中获取的模型的数据
static void configureSelectNotificationSubject(
BuildContext context, String route) {
selectNotificationSubject.stream.listen((String payload) async { // because i need to get the articles model from the notification to arguments
await Navigator.pushNamed(context, route, arguments: BundleData(payload));
});
}
而且,当我们在main
中初始化通知时,属性onSelectNotification
仅支持字符串
,不能支持添加模型等自定义数据
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async { // because this property only support String only, so how to send a model from API?
if (payload != null) {
print('notification payload: ' + payload);
}
selectNotificationSubject.add(payload);
任何人都可以告诉我如何解决我的问题?您可以将模型转换为JSON格式,然后进行解码/编码。我不知道您的
文章
和源代码
类中有哪些变量,但假设您可以尝试这样的方法:
类绑定数据{
最终来源;
最后条款;
BundleData(this.source,this.articles);
工厂BundleData.fromJson(映射json){
返回BundleData(Source.fromJson(json['Source']),Articles.fromJson(json['Articles']);
}
映射到JSON(){
返回{
“source”:this.source.toJson(),
“articles”:this.articles.toJson()
};
}
}
如果使用此解决方案,您将需要为源代码和文章创建一个工厂fromJson
,以及一个方法toJson
然后您可以像这样传递参数:
BundleData BundleData=BundleData(/*一些源代码*/,/*一些文章*/);
等待本地通知。显示(
0,articles.title,articles.description,platformChannelSpecifics,
有效负载:jsonEncode(bundleData.toJson());
然后像这样取回您的模型:
静态无效配置SelectNotificationSubject(构建上下文,字符串路由){
selectNotificationSubject.stream.listen((字符串负载)异步{
wait Navigator.pushNamed(上下文、路由、参数:BundleData.fromJson(jsonDecode(有效负载));
});
}
如果数据来自API(JSON),那么这是唯一的解决方案?但如果数据不是来自API,该怎么办?我的意思是数据不是JSON格式?而且我已经为文章
和源代码
类添加了代码
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async { // because this property only support String only, so how to send a model from API?
if (payload != null) {
print('notification payload: ' + payload);
}
selectNotificationSubject.add(payload);