如何在 flutter 中将 Firebase RemoteMessage 编码为 Json?

How To Encode Firebase RemoteMessage to Json in flutter?

我想在 SharedPreferences 中保存 FireBase 消息,我需要 RemoteMessageToJson 方法。

这是我的方法,但 toJson 不存在。

saveMessage(RemoteMessage message) async {
  final String tag = "tag";
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool setString =
      await prefs.setString(tag, jsonEncode(message.toJson())); 
}

我也在下面写了扩展名,但它不完整,需要更多时间。

extension toJsonRemoteMessage on RemoteMessage {
  Map<String, dynamic> toJson() {
    var map = <String, dynamic>{};
    map['senderId'] = this.senderId;
    map['category'] = this.category;
    map['collapseKey'] = this.collapseKey;
    map['contentAvailable'] = this.contentAvailable;
    map['data'] = this.data;
    map['from'] = this.from;
    map['messageId'] = this.messageId;
    map['messageType'] = this.messageType;
    map['mutableContent'] = this.mutableContent;

    //not true
    map['notification'] = this.notification;  

    map['sentTime'] = this.sentTime?.millisecondsSinceEpoch ?? null;
    map['threadId'] = this.threadId;
    map['ttl'] = this.ttl;
    return map;
  }
}
  1. 我的方法对吗?在 phone 上保存 RemoteMessages 是最好(或最简单)的方法吗?
  2. 如果是,我需要一点帮助来完成我的延期。如果没有,请告诉我。谢谢。

我终于完成了我的扩展。现在你可以使用 toJson .

import 'package:firebase_messaging/firebase_messaging.dart';

extension toJsonRemoteMessage on RemoteMessage {
  Map<String, dynamic> toJson() => <String, dynamic>{
        'senderId': this.senderId,
        'category': this.category,
        'collapseKey': this.collapseKey,
        'contentAvailable': this.contentAvailable,
        'data': this.data,
        'from': this.from,
        'messageId': this.messageId,
        'messageType': this.messageType,
        'mutableContent': this.mutableContent,
        'notification': this.notification?.toJson(),
        'sentTime': this.sentTime?.millisecondsSinceEpoch,
        'threadId': this.threadId,
        'ttl': this.ttl,
      };
}

extension toJsonRemoteNotification on RemoteNotification {
  Map<String, dynamic> toJson() => <String, dynamic>{
        'android': this.android?.toJson(),
        'apple': this.apple?.toJson(),
        // 'web': this.web?.toJson(),
        // TODO RemoteNotification web
        'web': null,
        'title': this.title,
        'titleLocArgs': this.titleLocArgs,
        'titleLocKey': this.titleLocKey,
        'body': this.body,
        'bodyLocArgs': this.bodyLocArgs,
        'bodyLocKey': this.bodyLocKey,
      };
}

extension toJsonAndroidNotification on AndroidNotification {
  Map<String, dynamic> toJson() => <String, dynamic>{
        'channelId': this.channelId,
        'clickAction': this.clickAction,
        'color': this.color,
        'count': this.count,
        'imageUrl': this.imageUrl,
        'link': this.link,
        'priority': convertAndroidNotificationPriorityToInt(this.priority),
        'smallIcon': this.smallIcon,
        'sound': this.sound,
        'ticker': this.ticker,
        'visibility':
            convertAndroidNotificationVisibilityToInt(this.visibility),
        'tag': this.tag,
      };
}

extension toJsonAppleNotification on AppleNotification {
  Map<String, dynamic> toJson() => <String, dynamic>{
        'badge': this.badge,
        'sound': this.sound?.toJson(),
        'imageUrl': this.imageUrl,
        'subtitle': this.subtitle,
        'subtitleLocArgs': this.subtitleLocArgs,
        'subtitleLocKey': this.subtitleLocKey,
      };
}

extension toJsonAppleNotificationSound on AppleNotificationSound {
  Map<String, dynamic> toJson() => <String, dynamic>{
        'critical': this.critical,
        'name': this.name,
        'volume': this.volume,
      };
}

/// Converts an [AndroidNotificationPriority] into it's [int] representation.
int convertAndroidNotificationPriorityToInt(
    AndroidNotificationPriority? priority) {
  switch (priority) {
    case AndroidNotificationPriority.minimumPriority:
      return -2;
    case AndroidNotificationPriority.lowPriority:
      return -1;
    case AndroidNotificationPriority.defaultPriority:
      return 0;
    case AndroidNotificationPriority.highPriority:
      return 1;
    case AndroidNotificationPriority.maximumPriority:
      return 2;
    default:
      return 0;
  }
}

/// Converts an [AndroidNotificationVisibility] into it's [int] representation.
int convertAndroidNotificationVisibilityToInt(
    AndroidNotificationVisibility? visibility) {
  switch (visibility) {
    case AndroidNotificationVisibility.secret:
      return -1;
    case AndroidNotificationVisibility.private:
      return 0;
    case AndroidNotificationVisibility.public:
      return 1;
    default:
      return 0;
  }
}