【问题标题】:How To Encode Firebase RemoteMessage to Json in flutter?如何在颤动中将 Firebase RemoteMessage 编码为 Json?
【发布时间】:2022-01-04 07:40:24
【问题描述】:

我想将 FireBase 消息保存在 SharedPreferences 中,并且我需要为 RemoteMessage 提供一个 ToJson 方法。

这是我的方法,但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. 我的方法对吗?将 RemoteMessages 保存在手机上是最好的(或最简单的 ????)方法吗?
  2. 如果是,我需要一些帮助来完成我的扩展。如果没有,请告诉我。谢谢。

【问题讨论】:

    标签: flutter firebase-cloud-messaging


    【解决方案1】:

    我终于完成了我的扩展。现在你可以使用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;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 2020-07-23
      • 2022-06-28
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2020-11-25
      • 2018-05-16
      • 2022-01-09
      相关资源
      最近更新 更多