@@ -28,6 +28,7 @@ import { FirebaseMessagingRequestHandler } from './messaging-api-request-interna
2828
2929import {
3030 BatchResponse ,
31+ FidMulticastMessage ,
3132 Message ,
3233 MessagingTopicManagementResponse ,
3334 MulticastMessage ,
@@ -274,50 +275,84 @@ export class Messaging {
274275 }
275276
276277 /**
277- * Sends the given multicast message to all the FCM registration tokens
278+ * Sends the given multicast message to all the FCM registration tokens or fids
278279 * specified in it.
279280 *
280281 * This method uses the {@link Messaging.sendEach} API under the hood to send the given
281282 * message to all the target recipients. The responses list obtained from the
282- * return value corresponds to the order of tokens in the `MulticastMessage`.
283+ * return value corresponds to the order of tokens/fids in the `MulticastMessage`.
284+ * If both `tokens` and `fids` are provided, `tokens` are processed first, followed by `fids`.
283285 * An error from this method or a `BatchResponse` with all failures indicates a total
284- * failure, meaning that the messages in the list could be sent. Partial failures or
285- * failures are only indicated by a `BatchResponse` return value.
286+ * failure, meaning that the messages in the list could not be sent. Partial failures
287+ * are only indicated by a `BatchResponse` return value.
286288 *
287- * @param message - A multicast message
288- * containing up to 500 tokens.
289+ * @deprecated Use the overload accepting {@link FidMulticastMessage} instead.
290+ *
291+ * @param message - A multicast message containing up to 500 tokens and/or fids.
289292 * @param dryRun - Whether to send the message in the dry-run
290293 * (validation only) mode.
291294 * @returns A Promise fulfilled with an object representing the result of the
292295 * send operation.
293296 */
294- public sendEachForMulticast ( message : MulticastMessage , dryRun ?: boolean ) : Promise < BatchResponse > {
295- const copy : MulticastMessage = deepCopy ( message ) ;
297+ public sendEachForMulticast ( message : MulticastMessage , dryRun ?: boolean ) : Promise < BatchResponse > ;
298+
299+ /**
300+ * Sends the given multicast message to all the FCM fids specified in it.
301+ *
302+ * This method uses the {@link Messaging.sendEach} API under the hood to send the given
303+ * message to all the target recipients. The responses list obtained from the
304+ * return value corresponds to the order of fids in the `FidMulticastMessage`.
305+ * An error from this method or a `BatchResponse` with all failures indicates a total
306+ * failure, meaning that the messages in the list could not be sent. Partial failures
307+ * are only indicated by a `BatchResponse` return value.
308+ *
309+ * @param message - A multicast message containing up to 500 fids.
310+ * @param dryRun - Whether to send the message in the dry-run (validation only) mode.
311+ * @returns A Promise fulfilled with an object representing the result of the send operation.
312+ */
313+ public sendEachForMulticast ( message : FidMulticastMessage , dryRun ?: boolean ) : Promise < BatchResponse > ;
314+
315+ public sendEachForMulticast (
316+ message : MulticastMessage | FidMulticastMessage ,
317+ dryRun ?: boolean ,
318+ ) : Promise < BatchResponse > {
319+ const copy : any = deepCopy ( message ) ;
296320 if ( ! validator . isNonNullObject ( copy ) ) {
297321 throw new FirebaseMessagingError (
298322 messagingClientErrorCode . INVALID_ARGUMENT , 'MulticastMessage must be a non-null object' ) ;
299323 }
300- if ( ! validator . isNonEmptyArray ( copy . tokens ) ) {
324+
325+ const { tokens, fids, ...baseMessage } = copy ;
326+
327+ if ( tokens !== undefined && ! validator . isArray ( tokens ) ) {
328+ throw new FirebaseMessagingError (
329+ messagingClientErrorCode . INVALID_ARGUMENT , 'tokens must be a valid array' ) ;
330+ }
331+ if ( fids !== undefined && ! validator . isArray ( fids ) ) {
332+ throw new FirebaseMessagingError (
333+ messagingClientErrorCode . INVALID_ARGUMENT , 'fids must be a valid array' ) ;
334+ }
335+
336+ const tokenList : string [ ] = tokens || [ ] ;
337+ const fidList : string [ ] = fids || [ ] ;
338+
339+ if ( tokenList . length === 0 && fidList . length === 0 ) {
301340 throw new FirebaseMessagingError (
302- messagingClientErrorCode . INVALID_ARGUMENT , 'tokens must be a non-empty array' ) ;
341+ messagingClientErrorCode . INVALID_ARGUMENT , 'Either tokens or fids must be a non-empty array' ) ;
303342 }
304- if ( copy . tokens . length > FCM_MAX_BATCH_SIZE ) {
343+
344+ const totalLength = tokenList . length + fidList . length ;
345+ if ( totalLength > FCM_MAX_BATCH_SIZE ) {
305346 throw new FirebaseMessagingError (
306347 messagingClientErrorCode . INVALID_ARGUMENT ,
307- `tokens list must not contain more than ${ FCM_MAX_BATCH_SIZE } items ` ) ;
348+ `The total number of tokens and fids must not exceed ${ FCM_MAX_BATCH_SIZE } . ` ) ;
308349 }
309350
310- const messages : Message [ ] = copy . tokens . map ( ( token ) => {
311- return {
312- token,
313- android : copy . android ,
314- apns : copy . apns ,
315- data : copy . data ,
316- notification : copy . notification ,
317- webpush : copy . webpush ,
318- fcmOptions : copy . fcmOptions ,
319- } ;
320- } ) ;
351+ const messages : Message [ ] = [
352+ ...tokenList . map ( ( token ) => ( { ...baseMessage , token } as Message ) ) ,
353+ ...fidList . map ( ( fid ) => ( { ...baseMessage , fid } as Message ) ) ,
354+ ] ;
355+
321356 return this . sendEach ( messages , dryRun ) ;
322357 }
323358
0 commit comments