This library provides a simple interface to interact with Firebase Cloud Messaging (FCM v1) for sending notifications to devices.
📲 Send messages to devices using FCM.
📢 Send messages to topics.
🔑 Set service account credentials.
🔧 Customize HTTP client for requests.
To use this library, simply import it into your Go project:
import "path/to/fcm"
Ensure you have the necessary dependencies installed:
go get -u github.com/patrickkabwe/go-fcm
To start sending messages, you need to create a new FCM client instance:
client := fcm.NewClient()
Before sending messages, set your service account credentials:
client = client.SetCredentialFile("path/to/serviceAccountKey.json")
OR
credentials := &fcm.Credentials{
ProjectID: "your-project-id",
PrivateKeyID: "your-private-key-id",
PrivateKey: "yout-private-key",
ClientEmail: "your-client-email",
ClientID: "your-client-id",
AuthURI: "your-auth-uri",
TokenURI: "your-token-uri",
AuthProviderX509CertURL: "your-auth-provider-x509-cert-url",
ClientX509CertURL: "your-client-x509-cert-url",
}
client = client.SetCredential(credentials)
To send a message, create a MessagePayload
and use the Send
method:
msg := &MessagePayload{
// Populate your message payload
}
err := client.Send(msg)
if err != nil {
log.Fatalf("Failed to send message: %v", err)
}
log.Println("Message sent successfully")
To send a message to a specific topic:
msg := &MessagePayload{
// Populate your message payload
Topic: "news",
}
err := client.SendToTopic(msg)
if err != nil {
log.Fatalf("Failed to send message: %v", err)
}
log.Println("Message sent successfully")
You can customize the HTTP client used for making requests:
customClient := &http.Client{Timeout: time.Second * 10}
client = client.SetHTTPClient(customClient)
Contributions to this library are welcome. Please ensure to follow the coding standards and write tests for new features.
This library is licensed under the MIT License. See the LICENSE file for details.