sendEach method

Future<BatchResponse> sendEach(
  1. List<Message> messages, {
  2. bool? dryRun,
})

Sends each message in the given array via Firebase Cloud Messaging.

The responses list obtained from the return value corresponds to the order of messages. An error from this method or a BatchResponse with all failures indicates a total failure, meaning that none of the messages in the list could be sent. Partial failures or no failures are only indicated by a BatchResponse return value.

  • messages: A non-empty array containing up to 500 messages.
  • dryRun: Whether to send the messages in the dry-run (validation only) mode.

Implementation

// TODO once we have Messaging.sendAll, add the following:
// Unlike [Messaging.sendAll], this method makes a single RPC call for each message
// in the given array.
///
/// The responses list obtained from the return value corresponds to the order of `messages`.
/// An error from this method or a `BatchResponse` with all failures indicates a total failure,
/// meaning that none of the messages in the list could be sent. Partial failures or no
/// failures are only indicated by a `BatchResponse` return value.
///
/// - [messages]: A non-empty array containing up to 500 messages.
/// - [dryRun]: Whether to send the messages in the dry-run
///   (validation only) mode.
Future<BatchResponse> sendEach(List<Message> messages, {bool? dryRun}) {
  return _requestHandler.v1(
    (client) async {
      if (messages.isEmpty) {
        throw FirebaseMessagingAdminException(
          MessagingClientErrorCode.invalidArgument,
          'messages must be a non-empty array',
        );
      }
      if (messages.length > _fmcMaxBatchSize) {
        throw FirebaseMessagingAdminException(
          MessagingClientErrorCode.invalidArgument,
          'messages list must not contain more than $_fmcMaxBatchSize items',
        );
      }

      final responses = await Future.wait<SendResponse>(
        messages.map((message) async {
          final response = client.projects.messages.send(
            fmc1.SendMessageRequest(
              message: message._toProto(),
              validateOnly: dryRun,
            ),
            _parent,
          );

          return response.then(
            (value) {
              return SendResponse._(success: true, messageId: value.name);
            },
            // ignore: avoid_types_on_closure_parameters
            onError: (Object? error) {
              return SendResponse._(
                success: false,
                error: error is FirebaseMessagingAdminException
                    ? error
                    : FirebaseMessagingAdminException(
                        MessagingClientErrorCode.internalError,
                        error.toString(),
                      ),
              );
            },
          );
        }),
      );

      final successCount = responses.where((r) => r.success).length;

      return BatchResponse._(
        responses: responses,
        successCount: successCount,
        failureCount: responses.length - successCount,
      );
    },
  );
}