getFCMNotifications method

  1. @override
Future<RequestResponse<List<FCMNotification>?>> getFCMNotifications({
  1. bool? seen,
  2. int limitStart = 0,
  3. int limitPageLength = 20,
  4. dynamic filters,
})

Returns a list of FCMNotification of a user.

Optionally, can set seen to true to get only seen notifications.

Using limitStart and limitPageLength, can be used for pagination.

Extra filters can be specified for the doctype Communication based on the DBFilter specifications.

Implementation

@override
Future<RequestResponse<List<FCMNotification>?>> getFCMNotifications(
    {bool? seen,
    int limitStart = 0,
    int limitPageLength = 20,
    dynamic filters}) async {
  await checkAppInstalled(features: ['getFCMNotifications']);

  if (filters != null) {
    if (!DBFilter.isDBFilter(filters)) throw InvalidFrappeFilter();
  }

  var requestData = <String, dynamic>{
    'cmd': 'renovation_core.utils.fcm.get_user_notifications',
    'limit_start': limitStart,
    'limit_page_length': limitPageLength,
    'filters': filters
  };
  if (seen != null) {
    requestData.addAll(<String, dynamic>{
      'filters': {'seen': FrappeDocFieldConverter.boolToCheck(seen)}
    });
  }

  final response = await Request.initiateRequest(
      url: config.hostUrl,
      method: HttpMethod.POST,
      contentType: ContentTypeLiterals.APPLICATION_JSON,
      data: requestData);

  if (response.isSuccess == true) {
    if (response.data != null && response.data!.message is List) {
      List<dynamic> jsonArray = response.data!.message;
      var notifications = List<FCMNotification>.of(jsonArray.map(
          (dynamic notification) => FCMNotification.fromJson(notification)));
      return RequestResponse.success(notifications,
          rawResponse: response.rawResponse);
    }
  }
  response.isSuccess = false;
  return RequestResponse.fail(handleError(
      'fcm_notification',
      response.error ??
          ErrorDetail(
              info: Information(
                  data: response.data,
                  rawResponse: response.rawResponse,
                  httpCode: response.httpCode))));
}