Skip to content

Commit

Permalink
feat(firebase_messaging): add toMap() method to RemoteMessage and…
Browse files Browse the repository at this point in the history
… its properties (#8453)

Co-authored-by: Russell Wheatley <russellwheatley85@gmail.com>
  • Loading branch information
daniellampl and russellwheatley committed Apr 21, 2022
1 parent fdb24c8 commit 047cccd
Show file tree
Hide file tree
Showing 6 changed files with 873 additions and 135 deletions.
Expand Up @@ -52,6 +52,25 @@ class RemoteMessage {
);
}

/// Returns the [RemoteMessage] as a raw Map.
Map<String, dynamic> toMap() {
return <String, dynamic>{
'senderId': senderId,
'category': category,
'collapseKey': collapseKey,
'contentAvailable': contentAvailable,
'data': data,
'from': from,
'messageId': messageId,
'messageType': messageType,
'mutableContent': mutableContent,
'notification': notification?.toMap(),
'sentTime': sentTime?.millisecondsSinceEpoch,
'threadId': threadId,
'ttl': ttl,
};
}

/// The ID of the upstream sender location.
final String? senderId;

Expand Down
Expand Up @@ -25,67 +25,41 @@ class RemoteNotification {

/// Constructs a [RemoteNotification] from a raw Map.
factory RemoteNotification.fromMap(Map<String, dynamic> map) {
AndroidNotification? _android;
AppleNotification? _apple;
WebNotification? _web;

if (map['android'] != null) {
_android = AndroidNotification(
channelId: map['android']['channelId'],
clickAction: map['android']['clickAction'],
color: map['android']['color'],
count: map['android']['count'],
imageUrl: map['android']['imageUrl'],
link: map['android']['link'],
priority:
convertToAndroidNotificationPriority(map['android']['priority']),
smallIcon: map['android']['smallIcon'],
sound: map['android']['sound'],
ticker: map['android']['ticker'],
tag: map['android']['tag'],
visibility: convertToAndroidNotificationVisibility(
map['android']['visibility']),
);
}

if (map['apple'] != null) {
_apple = AppleNotification(
badge: map['apple']['badge'],
subtitle: map['apple']['subtitle'],
subtitleLocArgs: _toList(map['apple']['subtitleLocArgs']),
subtitleLocKey: map['apple']['subtitleLocKey'],
imageUrl: map['apple']['imageUrl'],
sound: map['apple']['sound'] == null
? null
: AppleNotificationSound(
critical: map['apple']['sound']['critical'] ?? false,
name: map['apple']['sound']['name'],
volume: map['apple']['sound']['volume'] ?? 0,
),
);
}

if (map['web'] != null) {
_web = WebNotification(
analyticsLabel: map['web']['analyticsLabel'],
image: map['web']['image'],
link: map['web']['link'],
);
}

return RemoteNotification(
title: map['title'],
titleLocArgs: _toList(map['titleLocArgs']),
titleLocKey: map['titleLocKey'],
body: map['body'],
bodyLocArgs: _toList(map['bodyLocArgs']),
bodyLocKey: map['bodyLocKey'],
android: _android,
apple: _apple,
web: _web,
android: map['android'] != null
? AndroidNotification.fromMap(
Map<String, dynamic>.from(map['android']))
: null,
apple: map['apple'] != null
? AppleNotification.fromMap(Map<String, dynamic>.from(map['apple']))
: null,
web: map['web'] != null
? WebNotification.fromMap(Map<String, dynamic>.from(map['web']))
: null,
);
}

/// Returns the [RemoteNotification] as a raw Map.
Map<String, dynamic> toMap() {
return <String, dynamic>{
'title': title,
'titleLocArgs': titleLocArgs,
'titleLocKey': titleLocKey,
'body': body,
'bodyLocArgs': bodyLocArgs,
'bodyLocKey': bodyLocKey,
'android': android?.toMap(),
'apple': apple?.toMap(),
'web': web?.toMap(),
};
}

/// Android specific notification properties.
final AndroidNotification? android;

Expand Down Expand Up @@ -133,6 +107,42 @@ class AndroidNotification {
this.tag,
this.visibility = AndroidNotificationVisibility.private});

/// Constructs an [AndroidNotification] from a raw Map.
factory AndroidNotification.fromMap(Map<String, dynamic> map) {
return AndroidNotification(
channelId: map['channelId'],
clickAction: map['clickAction'],
color: map['color'],
count: map['count'],
imageUrl: map['imageUrl'],
link: map['link'],
priority: convertToAndroidNotificationPriority(map['priority']),
smallIcon: map['smallIcon'],
sound: map['sound'],
ticker: map['ticker'],
tag: map['tag'],
visibility: convertToAndroidNotificationVisibility(map['visibility']),
);
}

/// Returns the [AndroidNotification] as a raw Map.
Map<String, dynamic> toMap() {
return <String, dynamic>{
'channelId': channelId,
'clickAction': clickAction,
'color': color,
'count': count,
'imageUrl': imageUrl,
'link': link,
'priority': convertAndroidNotificationPriorityToInt(priority),
'smallIcon': smallIcon,
'sound': sound,
'ticker': ticker,
'tag': tag,
'visibility': convertAndroidNotificationVisibilityToInt(visibility),
};
}

/// The channel the notification is delivered on.
final String? channelId;

Expand Down Expand Up @@ -190,6 +200,33 @@ class AppleNotification {
this.subtitleLocArgs = const <String>[],
this.subtitleLocKey});

/// Constructs an [AppleNotification] from a raw Map.
factory AppleNotification.fromMap(Map<String, dynamic> map) {
return AppleNotification(
badge: map['badge'],
subtitle: map['subtitle'],
subtitleLocArgs: _toList(map['subtitleLocArgs']),
subtitleLocKey: map['subtitleLocKey'],
imageUrl: map['imageUrl'],
sound: map['sound'] == null
? null
: AppleNotificationSound.fromMap(
Map<String, dynamic>.from(map['sound'])),
);
}

/// Returns the [AppleNotification] as a raw Map.
Map<String, dynamic> toMap() {
return <String, dynamic>{
'badge': badge,
'subtitle': subtitle,
'subtitleLocArgs': subtitleLocArgs,
'subtitleLocKey': subtitleLocKey,
'imageUrl': imageUrl,
'sound': sound?.toMap(),
};
}

/// The value which sets the application badge.
final String? badge;

Expand Down Expand Up @@ -217,6 +254,24 @@ class AppleNotificationSound {
const AppleNotificationSound(
{this.critical = false, this.name, this.volume = 0});

/// Constructs an [AppleNotificationSound] from a raw Map.
factory AppleNotificationSound.fromMap(Map<String, dynamic> map) {
return AppleNotificationSound(
critical: map['critical'] ?? false,
name: map['name'],
volume: map['volume'] ?? 0,
);
}

/// Returns the [AppleNotificationSound] as a raw Map.
Map<String, dynamic> toMap() {
return <String, dynamic>{
'critical': critical,
'name': name,
'volume': volume,
};
}

/// Whether or not the notification sound was critical.
final bool critical;

Expand Down Expand Up @@ -246,6 +301,24 @@ class WebNotification {
this.link,
});

/// Constructs a [WebNotification] from a raw Map.
factory WebNotification.fromMap(Map<String, dynamic> map) {
return WebNotification(
analyticsLabel: map['analyticsLabel'],
image: map['image'],
link: map['link'],
);
}

/// Returns the [WebNotification] as a raw Map.
Map<String, dynamic> toMap() {
return <String, dynamic>{
'analyticsLabel': analyticsLabel,
'image': image,
'link': link,
};
}

/// Optional message label for custom analytics.
final String? analyticsLabel;

Expand Down
Expand Up @@ -24,6 +24,25 @@ AndroidNotificationPriority convertToAndroidNotificationPriority(
}
}

/// 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 [int] into it's [AndroidNotificationVisibility] representation.
AndroidNotificationVisibility convertToAndroidNotificationVisibility(
int? visibility) {
Expand All @@ -39,6 +58,21 @@ AndroidNotificationVisibility convertToAndroidNotificationVisibility(
}
}

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

/// Converts an [int] into it's [AuthorizationStatus] representation.
AuthorizationStatus convertToAuthorizationStatus(int? status) {
// Can be null on unsupported platforms, e.g. iOS < 10.
Expand Down

0 comments on commit 047cccd

Please sign in to comment.