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