NotificationPayload.fromRemoteMessage constructor

NotificationPayload.fromRemoteMessage(
  1. RemoteMessage message
)

Creates a NotificationPayload from a Firebase Cloud Messaging RemoteMessage.

This factory extracts notification content from the FCM message and converts it to a standardized payload format.

Implementation

factory NotificationPayload.fromRemoteMessage(RemoteMessage message) {
  final notification = message.notification;
  final data = Map<String, dynamic>.from(message.data);

  // Extract route from multiple possible fields
  final route = data['route'] as String? ??
      data['screen'] as String? ??
      data['deepLink'] as String? ??
      data['url'] as String?;

  // Extract image URL
  final imageUrl = notification?.android?.imageUrl ??
      notification?.apple?.imageUrl ??
      data['imageUrl'] as String?;

  // Extract actions if present
  final actionsList = data['actions'] as List<dynamic>?;
  final actions =
      actionsList?.map((e) => NotificationAction.fromJson(e as Map<String, dynamic>)).toList() ??
          [];

  return NotificationPayload(
    title: notification?.title ?? data['title'] as String?,
    body: notification?.body ?? data['body'] as String?,
    imageUrl: imageUrl,
    route: route,
    data: data,
    actions: actions,
    channelId: notification?.android?.channelId ?? data['channelId'] as String?,
    category: message.category ?? data['category'] as String?,
    sound: notification?.android?.sound ?? data['sound'] as String?,
    badge: notification?.apple?.badge != null
        ? int.tryParse(notification!.apple!.badge.toString())
        : null,
    ttl: message.ttl,
    priority: data['priority'] as String?,
  );
}