Send Messages to Topics

You can send messages to devices that are subscribed to a particular topic usingthe Admin SDK or the FCM v1 HTTP API.

Prerequisites

Sending to a Topic

You can send messages to a topic using either theFirebase Admin SDK or the FCMHTTP v1 API.

Important: Once anFCM fanout request is received, it's fullycommitted and cannot be canceled through theFCM APIs, console, orFirebase console support.

Using the Admin SDK

You can use the Admin SDK to send topic messages from your server. To learn moreabout setting up the Admin SDK, seeSend Messages using the AdminSDK.

Node.js

// The topic name can be optionally prefixed with "/topics/".consttopic='highScores';constmessage={data:{score:'850',time:'2:45'},topic:topic};// Send a message to devices subscribed to the provided topic.getMessaging().send(message).then((response)=>{// Response is a message ID string.console.log('Successfully sent message:',response);}).catch((error)=>{console.log('Error sending message:',error);});

Java

// The topic name can be optionally prefixed with "/topics/".Stringtopic="highScores";// See documentation on defining a message payload.Messagemessage=Message.builder().putData("score","850").putData("time","2:45").setTopic(topic).build();// Send a message to the devices subscribed to the provided topic.Stringresponse=FirebaseMessaging.getInstance().send(message);// Response is a message ID string.System.out.println("Successfully sent message: "+response);

Python

# The topic name can be optionally prefixed with "/topics/".topic='highScores'# See documentation on defining a message payload.message=messaging.Message(data={'score':'850','time':'2:45',},topic=topic,)# Send a message to the devices subscribed to the provided topic.response=messaging.send(message)# Response is a message ID string.print('Successfully sent message:',response)

Go

// The topic name can be optionally prefixed with "/topics/".topic:="highScores"// See documentation on defining a message payload.message:=&messaging.Message{Data:map[string]string{"score":"850","time":"2:45",},Topic:topic,}// Send a message to the devices subscribed to the provided topic.response,err:=client.Send(ctx,message)iferr!=nil{log.Fatalln(err)}// Response is a message ID string.fmt.Println("Successfully sent message:",response)

C#

// The topic name can be optionally prefixed with "/topics/".vartopic="highScores";// See documentation on defining a message payload.varmessage=newMessage(){Data=newDictionary<string,string>(){{"score","850"},{"time","2:45"},},Topic=topic,};// Send a message to the devices subscribed to the provided topic.stringresponse=awaitFirebaseMessaging.DefaultInstance.SendAsync(message);// Response is a message ID string.Console.WriteLine("Successfully sent message: "+response);

Using the HTTP v1 API

To send topic messages using the HTTP v1 API, construct a JSON POST request. Tolearn more about using the HTTP v1 API, seeSend a Message via FCM v1API.

REST

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1Content-Type: application/jsonAuthorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA{  "message":{    "topic" : "foo-bar",    "notification" : {      "body" : "This is a Firebase Cloud Messaging Topic Message!",      "title" : "FCM Message"      }   }}

cURL command:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{  "message": {    "topic" : "foo-bar",    "notification": {      "body": "This is a Firebase Cloud Messaging Topic Message!",      "title": "FCM Message"    }  }}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Sending to Topic Conditions

To send a message to acombination of topics, specify acondition, which isa boolean expression that specifies the target topics. For example, thefollowing condition will send messages to devices that are subscribed toTopicA and eitherTopicB orTopicC:

"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

FCM first evaluates any conditions in parentheses, and then evaluatesthe expression from left to right. You can include up to five topics in yourconditional expression.

Tip: If you often send messages to a specific combination of topics, considercreating a new topic for that user group. This is more efficient than usingtopic conditions. For example, instead of sending messages to subscribers ofTopicA andTopicB using the condition"'TopicA' in topics && 'TopicB' in topics", create a new topicTopicAandB,subscribe the relevant users to it, and send messages directly toTopicAandB.

Using the Admin SDK

Node.js

// Define a condition which will send to devices which are subscribed// to either the Google stock or the tech industry topics.constcondition='\'stock-GOOG\' in topics || \'industry-tech\' in topics';// See documentation on defining a message payload.constmessage={notification:{title:'$FooCorp up 1.43% on the day',body:'$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'},condition:condition};// Send a message to devices subscribed to the combination of topics// specified by the provided condition.getMessaging().send(message).then((response)=>{// Response is a message ID string.console.log('Successfully sent message:',response);}).catch((error)=>{console.log('Error sending message:',error);});

Java

// Define a condition which will send to devices which are subscribed// to either the Google stock or the tech industry topics.Stringcondition="'stock-GOOG' in topics || 'industry-tech' in topics";// See documentation on defining a message payload.Messagemessage=Message.builder().setNotification(Notification.builder().setTitle("$GOOG up 1.43% on the day").setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.").build()).setCondition(condition).build();// Send a message to devices subscribed to the combination of topics// specified by the provided condition.Stringresponse=FirebaseMessaging.getInstance().send(message);// Response is a message ID string.System.out.println("Successfully sent message: "+response);

Python

# Define a condition which will send to devices which are subscribed# to either the Google stock or the tech industry topics.condition="'stock-GOOG' in topics || 'industry-tech' in topics"# See documentation on defining a message payload.message=messaging.Message(notification=messaging.Notification(title='$GOOG up 1.43% on the day',body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',),condition=condition,)# Send a message to devices subscribed to the combination of topics# specified by the provided condition.response=messaging.send(message)# Response is a message ID string.print('Successfully sent message:',response)

Go

// Define a condition which will send to devices which are subscribed// to either the Google stock or the tech industry topics.condition:="'stock-GOOG' in topics || 'industry-tech' in topics"// See documentation on defining a message payload.message:=&messaging.Message{Data:map[string]string{"score":"850","time":"2:45",},Condition:condition,}// Send a message to devices subscribed to the combination of topics// specified by the provided condition.response,err:=client.Send(ctx,message)iferr!=nil{log.Fatalln(err)}// Response is a message ID string.fmt.Println("Successfully sent message:",response)

C#

// Define a condition which will send to devices which are subscribed// to either the Google stock or the tech industry topics.varcondition="'stock-GOOG' in topics || 'industry-tech' in topics";// See documentation on defining a message payload.varmessage=newMessage(){Notification=newNotification(){Title="$GOOG up 1.43% on the day",Body="$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",},Condition=condition,};// Send a message to devices subscribed to the combination of topics// specified by the provided condition.stringresponse=awaitFirebaseMessaging.DefaultInstance.SendAsync(message);// Response is a message ID string.Console.WriteLine("Successfully sent message: "+response);

Using the HTTP v1 API

REST

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1Content-Type: application/jsonAuthorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA{   "message":{    "condition": "'dogs' in topics || 'cats' in topics",    "notification" : {      "body" : "This is a Firebase Cloud Messaging Topic Message!",      "title" : "FCM Message",    }  }}

cURL command:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{  "notification": {    "title": "FCM Message",    "body": "This is a Firebase Cloud Messaging Topic Message!",  },  "condition": "'dogs' in topics || 'cats' in topics"}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Handling Topic Messages in the App

Once you send a message to a topic,FCM delivers it to all subscribedclient app instances.

To learn how to receive and process messages in your Android, iOS, or Web app,seeReceiveMessages.

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 2025-12-17 UTC.