【问题标题】:Flutter: How to send a model from the API to other screen using flutter_local_notificationFlutter:如何使用 flutter_local_notification 将模型从 API 发送到其他屏幕
【发布时间】:2020-09-02 16:18:24
【问题描述】:

我在执行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);

谁能告诉我如何解决我的问题?

【问题讨论】:

    标签: flutter notifications


    【解决方案1】:

    您可以将模型转换为您将解码/编码的 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()
        };
      }
    }
    

    如果您使用此解决方案,您将需要为 SourceArticles 创建一个工厂 fromJson 和一个方法 toJson

    然后你可以像这样传递你的参数:

    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)));
      });
    }
    

    【讨论】:

    • 如果数据来自API(JSON),那么这是唯一的解决方案吗?但是如果数据不是来自 API 怎么办?我的意思是数据不是 JSON 格式?
    • 而且我已经添加了ArticleSource 类的代码
    猜你喜欢
    • 1970-01-01
    • 2022-12-12
    • 2021-09-30
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2021-10-24
    • 2020-06-03
    相关资源
    最近更新 更多