Youcan send messages with a notification payload made upof predefined fields, a data payload of your own user-defined fields, or amessage containing both types of payload.SeeMessage types for more information.
Examples in this page show how to send notification messages using theFirebase Admin SDK (which has support for Node, Java, Python, C#, and Go) and the v1 HTTP protocol. There is also guidance for sending messages via the deprecated legacy HTTP and XMPP protocols.
On success, each send method returns a message ID. The Firebase Admin SDK returnsthe ID string in the format projects/project_id/messages/message_id.The HTTP protocol response is a single JSON key:
After you have created a topic, either by subscribing client app instances tothe topic on the client side or via theserver API, you can send messages to thetopic. If this is your first time building send requests for FCM,see the guide toyour server environment and FCM forimportant background and setup information.
To send a message to a combination of topics,specify a condition, which is a boolean expression that specifies thetarget topics. For example, the following condition will send messages todevices that are subscribed to TopicA and either TopicB or TopicC:
FCM first evaluates any conditions in parentheses, and then evaluatesthe expression from left to right. In the above expression, a user subscribed toany single topic does not receive the message. Likewise, a user who does notsubscribe to TopicA does not receive the message. These combinations doreceive it:
To send messages to device groups, use the HTTP v1 API. If you are currentlysending to device groups using the deprecated legacy send APIs forHTTP or XMPP, or any of the older versions of theFirebase Admin SDK for Node.jsbased on the legacy protocols, we strongly recommend that youmigrate to the HTTP v1 APIat the earliest opportunity. The legacy send APIswill be disabled and removed in June 2024.
The Admin SDKs support sending messages in batches.You can group up to 500messages into a single batch and send them all in a single API call, withsignificant performance improvement over sending separate HTTP requests foreach message.
This feature can be used to build a customized set of messages and send themto different recipients, including topics or specific device registration tokens.Use this feature when, for example, you need tosimultaneously send messages to different audiences with slightly differentdetails in the message body.
You can send messages to devices in direct boot mode using theHTTP v1 or legacy HTTP APIs. Before sending todevices in direct boot mode, make sure you have completed the steps toenable client devices to receive FCM messages in direct boot mode.
Platform-specific blocks give you flexibility to customize messages fordifferent platforms to ensure that they are handled correctly when received. TheFCM backend will take all specified parameters into account and customize themessage for each platform.
Whenever you want to send values only to particular platforms, don't usecommon fields; use platform-specific fields. For example, to send a notificationonly to Apple platforms and web but not to Android, you must use two separate sets offields, one for Apple and one for web.
For Android, the request sets a special icon and color to display on Android devices. As noted in the reference for AndroidNotification,the color is specified in #rrggbb format, and the image must be a drawableicon resource local to the Android app.
The following example send request sends a common notification title to allplatforms, but it also sends an action for the app to perform in response touser interacting with the notification. Here's an approximation of the visual effect on a user's device:
The following table lists the FCM v1 REST API error codes and their descriptions. Error Code Description and Resolution Steps UNSPECIFIED_ERROR No more information is available about this error. None. INVALID_ARGUMENT (HTTP error code = 400) Request parameters were invalid. An extension of type google.rpc.BadRequest is returned to specify which field was invalid. Potential causes include invalid registration, invalid package name, message too big, invalid data key, invalid TTL, or other invalid parameters.
Invalid registration: Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with FCM. Do not truncate the token or add additional characters.
Invalid package name: Make sure the message was addressed to a registration token whose package name matches the value passed in the request.
Message too big: Check that the total size of the payload data included in a message does not exceed FCM limits: 4096 bytes for most messages, or 2048 bytes in the case of messages to topics. This includes both the keys and the values.
Invalid data key: Check that the payload data does not contain a key (such as from, or gcm, or any value prefixed by google) that is used internally by FCM. Note that some words (such as collapse_key) are also used by FCM but are allowed in the payload, in which case the payload value will be overridden by the FCM value.
Invalid TTL: Check that the value used in ttl is an integer representing a duration in seconds between 0 and 2,419,200 (4 weeks).
Invalid parameters: Check that the provided parameters have the right name and type. UNREGISTERED (HTTP error code = 404) App instance was unregistered from FCM. This usually means that the token used is no longer valid and a new one must be used. This error can be caused by missing registration tokens, or unregistered tokens.
Missing Registration: If the message's target is a token value, check that the request contains a registration token.
Not registered: An existing registration token may cease to be valid in a number of scenarios, including:
- If the client app unregisters with FCM.
- If the client app is automatically unregistered, which can happen if the user uninstalls the application. For example, on iOS, if the APNs Feedback Service reported the APNs token as invalid.
- If the registration token expires (for example, Google might decide to refresh registration tokens, or the APNs token has expired for iOS devices).
- If the client app is updated but the new version is not configured to receive messages.
For all these cases, remove this registration token from the app server and stop using it to send messages. SENDER_ID_MISMATCH (HTTP error code = 403) The authenticated sender ID is different from the sender ID for the registration token. A registration token is tied to a certain group of senders. When a client app registers for FCM, it must specify which senders are allowed to send messages. You should use one of those sender IDs when sending messages to the client app. If you switch to a different sender, the existing registration tokens won't work. QUOTA_EXCEEDED (HTTP error code = 429) Sending limit exceeded for the message target. An extension of type google.rpc.QuotaFailure is returned to specify which quota was exceeded. This error can be caused by exceeded message rate quota, exceeded device message rate quota, or exceeded topic message rate quota.
Message rate exceeded: The sending rate of messages is too high. You must reduce the overall rate at which you send messages. Use exponential backoff with a minimum initial delay of 1 minute to retry rejected messages.
Device message rate exceeded: The rate of messages to a particular device is too high. See message rate limit to a single device. Reduce the number of messages sent to this device and use exponential backoff to retry sending.
Topic message rate exceeded: The rate of messages to subscribers to a particular topic is too high. Reduce the number of messages sent for this topic and use exponential backoff with a minimum initial delay of 1 minute to retry sending. UNAVAILABLE (HTTP error code = 503) The server is overloaded. The server couldn't process the request in time. Retry the same request, but you must:
- Honor the Retry-After header if it is included in the response from the FCM Connection Server.
- Implement exponential back-off in your retry mechanism. (e.g. if you waited one second before the first retry, wait at least two seconds before the next one, then 4 seconds and so on). If you're sending multiple messages, consider applying jittering. For more information, see Handling retries. Senders that cause problems risk being denylisted. INTERNAL (HTTP error code = 500) An unknown internal error occurred. The server encountered an error while trying to process the request. You could retry the same request following suggestions in Handling retries. If the error persists, please contact Firebase support. THIRD_PARTY_AUTH_ERROR (HTTP error code = 401) APNs certificate or web push auth key was invalid or missing. A message targeted to an iOS device or a web push registration could not be sent. Check the validity of your development and production credentials.
If you are currently using the legacy protocols, build message requests as shownin this section. Keep in mind that, if you are sending to multiple platforms viaHTTP, the v1 protocol can greatly simplify your message requests.
To send to combinations of multiple topics, the app server must set the condition key (instead of the to key) to a boolean condition that specifies the target topics. For example, to send messages to devices that subscribedto TopicA and either TopicB orTopicC:
3a8082e126