Messaging class

Messaging service bound to the provided app.

Signature:

exportdeclareclassMessaging

Properties

PropertyModifiersTypeDescription
appAppTheApp associated with the currentMessaging service instance.

Methods

MethodModifiersDescription
enableLegacyHttpTransport()Enables the use of legacy HTTP/1.1 transport forsendEach() andsendEachForMulticast().
send(message, dryRun)Sends the given message via FCM.
sendEach(messages, dryRun)Sends each message in the given array via Firebase Cloud Messaging.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 ofmessages. An error from this method or aBatchResponse 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 aBatchResponse return value.
sendEachForMulticast(message, dryRun)Sends the given multicast message to all the FCM registration tokens specified in it.This method uses theMessaging.sendEach() API under the hood to send the given message to all the target recipients. The responses list obtained from the return value corresponds to the order of tokens in theMulticastMessage. An error from this method or aBatchResponse with all failures indicates a total failure, meaning that the messages in the list could be sent. Partial failures or failures are only indicated by aBatchResponse return value.
subscribeToTopic(registrationTokenOrTokens, topic)Subscribes a device to an FCM topic.SeeSubscribe to a topic for code samples and detailed documentation. Optionally, you can provide an array of tokens to subscribe multiple devices.
unsubscribeFromTopic(registrationTokenOrTokens, topic)Unsubscribes a device from an FCM topic.SeeUnsubscribe from a topic for code samples and detailed documentation. Optionally, you can provide an array of tokens to unsubscribe multiple devices.

Messaging.app

TheApp associated with the currentMessaging service instance.

Signature:

getapp():App;

Example

varapp=messaging.app;

Messaging.enableLegacyHttpTransport()

Warning: This API is now obsolete.

This will be removed when the HTTP/2 transport implementation reaches the same stability as the legacy HTTP/1.1 implementation.

Enables the use of legacy HTTP/1.1 transport forsendEach() andsendEachForMulticast().

Signature:

enableLegacyHttpTransport():void;

Returns:

void

Example

constmessaging=getMessaging(app);messaging.enableLegacyTransport();messaging.sendEach(messages);

Messaging.send()

Sends the given message via FCM.

Signature:

send(message:Message,dryRun?:boolean):Promise<string>;

Parameters

ParameterTypeDescription
messageMessageThe message payload.
dryRunbooleanWhether to send the message in the dry-run (validation only) mode.

Returns:

Promise<string>

A promise fulfilled with a unique message ID string after the message has been successfully handed off to the FCM service for delivery.

Messaging.sendEach()

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

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 ofmessages. An error from this method or aBatchResponse 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 aBatchResponse return value.

Signature:

sendEach(messages:Message[],dryRun?:boolean):Promise<BatchResponse>;

Parameters

ParameterTypeDescription
messagesMessage[]A non-empty array containing up to 500 messages.
dryRunbooleanWhether to send the messages in the dry-run (validation only) mode.

Returns:

Promise<BatchResponse>

A Promise fulfilled with an object representing the result of the send operation.

Messaging.sendEachForMulticast()

Sends the given multicast message to all the FCM registration tokens specified in it.

This method uses theMessaging.sendEach() API under the hood to send the given message to all the target recipients. The responses list obtained from the return value corresponds to the order of tokens in theMulticastMessage. An error from this method or aBatchResponse with all failures indicates a total failure, meaning that the messages in the list could be sent. Partial failures or failures are only indicated by aBatchResponse return value.

Signature:

sendEachForMulticast(message:MulticastMessage,dryRun?:boolean):Promise<BatchResponse>;

Parameters

ParameterTypeDescription
messageMulticastMessageA multicast message containing up to 500 tokens.
dryRunbooleanWhether to send the message in the dry-run (validation only) mode.

Returns:

Promise<BatchResponse>

A Promise fulfilled with an object representing the result of the send operation.

Messaging.subscribeToTopic()

Subscribes a device to an FCM topic.

SeeSubscribe to a topic for code samples and detailed documentation. Optionally, you can provide an array of tokens to subscribe multiple devices.

Signature:

subscribeToTopic(registrationTokenOrTokens:string|string[],topic:string):Promise<MessagingTopicManagementResponse>;

Parameters

ParameterTypeDescription
registrationTokenOrTokensstring | string[]
topicstringThe topic to which to subscribe.

Returns:

Promise<MessagingTopicManagementResponse>

A promise fulfilled with the server's response after the device has been subscribed to the topic.

Messaging.unsubscribeFromTopic()

Unsubscribes a device from an FCM topic.

SeeUnsubscribe from a topic for code samples and detailed documentation. Optionally, you can provide an array of tokens to unsubscribe multiple devices.

Signature:

unsubscribeFromTopic(registrationTokenOrTokens:string|string[],topic:string):Promise<MessagingTopicManagementResponse>;

Parameters

ParameterTypeDescription
registrationTokenOrTokensstring | string[]
topicstringThe topic from which to unsubscribe.

Returns:

Promise<MessagingTopicManagementResponse>

A promise fulfilled with the server's response after the device has been unsubscribed from the topic.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-11-12 UTC.