createNotificationFromJsonData method

  1. @override
Future<bool> createNotificationFromJsonData(
  1. Map<String, dynamic> mapData
)
override

Creates a new notification based on a map that is similar to the map produced by the toMap() method of a NotificationModel object.

The mapData parameter is a Map that represents the model of the notification, including the content, schedule, buttons, and localizations.

This method returns a Future that resolves to true if the notification was successfully created, or false if an error occurred.

This method is typically used to recreate a notification from data that was previously saved or transmitted in a different format, such as JSON. To use this method, you must first create a Map of the notification data, using a format that is similar to the output of the toMap() method of a NotificationModel object. Then, pass this map to the createNotificationFromJsonData() method to create the notification.

Implementation

@override
Future<bool> createNotificationFromJsonData(
    Map<String, dynamic> mapData) async {
  try {
    if (mapData[NOTIFICATION_CONTENT] is String) {
      mapData[NOTIFICATION_CONTENT] =
          json.decode(mapData[NOTIFICATION_CONTENT]);
    }

    if (mapData[NOTIFICATION_SCHEDULE] is String) {
      mapData[NOTIFICATION_SCHEDULE] =
          json.decode(mapData[NOTIFICATION_SCHEDULE]);
    }

    if (mapData[NOTIFICATION_BUTTONS] is String) {
      mapData[NOTIFICATION_BUTTONS] =
          json.decode(mapData[NOTIFICATION_BUTTONS]);
    }

    if (mapData[NOTIFICATION_LOCALIZATIONS] is String) {
      mapData[NOTIFICATION_LOCALIZATIONS] =
          json.decode(mapData[NOTIFICATION_LOCALIZATIONS]);
    }

    // Invalid Notification
    NotificationModel? notificationModel =
        NotificationModel().fromMap(mapData);
    if (notificationModel == null) {
      throw Exception('Notification map data is invalid');
    }

    return createNotification(
        content: notificationModel.content!,
        schedule: notificationModel.schedule,
        actionButtons: notificationModel.actionButtons);
  } catch (e) {
    return false;
  }
}