parseMap method

NotificationMessage parseMap(
  1. Map<String, Object?> input, {
  2. Map<String, Object?>? rawJson,
})

Implementation

NotificationMessage parseMap(
  Map<String, Object?> input, {
  Map<String, Object?>? rawJson,
}) {
  final normalized = _jsonMap(input);
  final encoded = utf8.encode(jsonEncode(normalized));
  if (encoded.length > maxPayloadBytes) {
    throw InvalidNotificationPayloadException(
      'Payload is ${encoded.length} bytes; maximum is $maxPayloadBytes.',
    );
  }

  final messageId =
      _string(normalized['message_id']) ??
      _string(normalized['messageId']) ??
      sha256.convert(encoded).toString();
  final notificationId =
      _int(normalized['notification_id']) ?? _stableInt(messageId);
  final actions = _parseActions(normalized['actions']);
  final category =
      _string(normalized['category_id']) ??
      (actions.isEmpty ? null : _categoryFor(actions));

  return NotificationMessage(
    notificationId: notificationId,
    messageId: messageId,
    title: _string(normalized['title']),
    body: _string(normalized['body']),
    imageUrl:
        _string(normalized['image_url']) ?? _string(normalized['imageUrl']),
    data: normalized,
    actions: actions,
    channelId: _string(normalized['channel_id']) ?? defaultChannelId,
    categoryId: category,
    routeType:
        _string(normalized['type']) ?? _string(normalized['route_type']),
    deepLink:
        _string(normalized['deep_link']) ?? _string(normalized['deepLink']),
    sentAt: _dateTime(normalized['sent_at']),
    topic: _string(normalized['topic']),
    collapseKey: _string(normalized['collapse_key']),
    silent:
        _bool(normalized['silent']) ||
        (_string(normalized['content_available']) == '1'),
    rawJson: rawJson ?? normalized,
  );
}