Firebase Cloud Messaging (FCM) is an essential tool for sending notifications to users’ devices or broadcasting messages to topics. If you’re a developer familiar with Go (Golang), you’ll be pleased to discover the go-fcm library that simplifies the integration with Firebase Cloud Messaging. In this article, we’ll dive into how to use this library effectively.
Features of go-fcm Library
- Send messages to a topic
- Send messages to a specific device list
- Messages can be notifications or data payloads
- Supports the condition attribute (FCM only)
- Instance ID features such as:
- Get information about app instance
- Subscribe app instance to a topic
- Batch subscribe/unsubscribe to/from a topic
- Create registration tokens for APNs tokens
Setting Up the Library
Getting started with the go-fcm library is straightforward. Here’s how to install it:
go get github.com/NaySoftware/go-fcm
Understanding Firebase Cloud Messaging Tokens
When working with Firebase, you’ll notice that FCM tokens have replaced server keys for sending messages. It’s important to adapt to this change as server key support will eventually be deprecated. You can find your Firebase Cloud Messaging token in:
- Firebase project settings
- Cloud Messaging
- Copy the Firebase Cloud Messaging token
Implementing the Go FCM Library
Let’s break down how to use the library to send messages.
Sending Messages to a Topic
Think of sending messages to a topic as sending a letter addressed to a group of people living in the same neighborhood. Instead of delivering individual letters to each person, you drop a single letter in a communal mailbox that everyone can access.
package main
import (
"fmt"
"github.com/NaySoftware/go-fcm"
)
const (
serverKey = "YOUR-KEY"
topic = "topicssomeTopic"
)
func main() {
data := map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
}
c := fcm.NewFcmClient(serverKey)
c.NewFcmMsgTo(topic, data)
status, err := c.Send()
if err == nil {
status.PrintResults()
} else {
fmt.Println(err)
}
Sending to a List of Devices
Sending messages to a list of devices is like sending a personalized invitation to everyone on your guest list for a party. Each device has its unique token, and you need to ensure that each invitation reaches the correct recipient.
package main
import (
"fmt"
"github.com/NaySoftware/go-fcm"
)
const (
serverKey = "YOUR-KEY"
)
func main() {
data := map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
}
ids := []string{
"token1",
}
xds := []string{
"token5",
"token6",
"token7",
}
c := fcm.NewFcmClient(serverKey)
c.NewFcmRegIdsMsg(ids, data)
c.AppendDevices(xds)
status, err := c.Send()
if err == nil {
status.PrintResults()
} else {
fmt.Println(err)
}
Troubleshooting
While using the go-fcm library, you may encounter some common issues. Here are a few troubleshooting tips:
- Ensure you have replaced “YOUR-KEY” with your actual Firebase Cloud Messaging token.
- Check that the devices you are attempting to send messages to are registered and have valid tokens.
- If there’s a failure in sending messages, look for a
RetryAfterheader in the response; implementing a backoff strategy may be helpful.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

