Flutter:如何使用flutter_local_notification将模型从API发送到其他屏幕

时间:2020-09-02 16:18:24

标签: flutter notifications

实施flutter_local_notifications时遇到问题。

问题是,如何从通知中发送自定义数据?因为我在example上看到只发送payload。不像我的情况那样给出如何发送titlebodycustom data的示例(想从API发送模型数据)。

那么如何解决我的情况呢?因为我已经在考虑它了,仍然没有给我解决方案,所以也已经在搜索它了。

如下所示,在我的函数上已经添加了参数模型Articles并将其添加到titlebody中,以使通知中的内容动态基于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);

任何人都可以告诉我如何解决我的问题吗?

1 个答案:

答案 0 :(得分:0)

您可以将模型转换为JSON格式,然后将其解码/编码。我不知道您的ArticlesSource类中包含哪些变量,但是您可以尝试使用类似的方法:

class BundleData {
  final Source source;
  final Articles articles;

  BundleData(this.source, this.articles);

  factory BundleData.fromJson(Map<String, dynamic> json) {
    return BundleData(Source.fromJson(json['source']), Articles.fromJson(json['articles']));
  }

  Map<String, dynamic> toJson() {
    return {
      "source": this.source.toJson(),
      "articles": this.articles.toJson()
    };
  }
}

如果使用此解决方案,则需要为fromJsontoJson创建工厂Source和方法Articles

然后,您可以像这样传递参数:

BundleData bundleData = BundleData(/* some source */, /* some articles */);
await flutterLocalNotificationsPlugin.show(
  0, articles.title, articles.description, platformChannelSpecifics,
  payload: jsonEncode(bundleData.toJson()));

然后像这样恢复模型:

static void configureSelectNotificationSubject(BuildContext context, String route) {
  selectNotificationSubject.stream.listen((String payload) async {
    await Navigator.pushNamed(context, route, arguments: BundleData.fromJson(jsonDecode(payload)));
  });
}
相关问题