decodeNotificationResponsePayload function

DecodedNotificationResponse? decodeNotificationResponsePayload(
  1. String? payload
)

Implementation

DecodedNotificationResponse? decodeNotificationResponsePayload(
  String? payload,
) {
  if (payload == null || payload.isEmpty) return null;

  final dynamic decoded;
  try {
    decoded = jsonDecode(payload);
  } on FormatException {
    return null;
  }
  if (decoded is! Map) return null;
  final map = Map<String, dynamic>.from(decoded);

  final source = NotificationSource.values.firstWhere(
    (value) => value.name == map[_sourceKey],
    orElse: () => NotificationSource.local,
  );
  final nestedData = map[_dataKey];
  final data = nestedData is Map ? Map<String, dynamic>.from(nestedData) : map;

  return DecodedNotificationResponse(data: data, source: source);
}