【发布时间】:2020-09-02 16:18:24
【问题描述】:
我在执行flutter_local_notifications时遇到问题。
问题是,如何从通知中发送自定义数据?因为我在example上看到只发了payload。不举例说明如何发送title、body或custom data,比如我的情况(想从API发送模型数据)。
那么如何解决我的问题?因为我已经在考虑它仍然没有给我一个解决方案,也已经在谷歌上搜索它。
如下所示,在我的函数上已经添加了参数模型Articles 并将其添加到title 和body 以使我的通知内容动态基于API。但是如何将该模型发送到另一个屏幕?因为属性payload 只支持String 但我的模型不是String
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
}
这是我的Article 模型
class BundleData {
final Source source;
final Articles articles;
BundleData(this.source, this.articles);
}
class Articles {
Articles({
this.source,
this.author,
this.title,
this.description,
this.url,
this.urlToImage,
this.publishedAt,
this.content,
});
Source source;
String author;
String title;
String description;
String url;
String urlToImage;
String publishedAt;
String content;
factory Articles.fromJson(Map<String, dynamic> json) => Articles(
source: Source.fromJson(json["source"]),
author: json["author"],
title: json["title"],
description: json["description"],
url: json["url"],
urlToImage: json["urlToImage"],
publishedAt: json["publishedAt"],
content: json["content"] == null ? null : json["content"],
);
}
class Source {
Source({
this.id,
this.name,
});
int id;
String name;
factory Source.fromJson(Map<String, dynamic> json) => Source(
id: json["id"],
name: json["name"],
);
}
再次看到,监听只支持String,但是当点击通知打开详情页时,需要从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只支持String,不支持自定义数据,比如添加模型。
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);
谁能告诉我如何解决我的问题?
【问题讨论】:
标签: flutter notifications