Skip to content

Latest commit

 

History

History
107 lines (77 loc) · 2.33 KB

README.md

File metadata and controls

107 lines (77 loc) · 2.33 KB

FCM Client Library

This library provides a simple interface to interact with Firebase Cloud Messaging (FCM v1) for sending notifications to devices.

Features

📲 Send messages to devices using FCM.
📢 Send messages to topics.
🔑 Set service account credentials.
🔧 Customize HTTP client for requests.

Installation

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

Usage

Creating a New Client

To start sending messages, you need to create a new FCM client instance:

client := fcm.NewClient()

Setting Service Account Credentials

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)

Sending a Message

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")

Sending a Message to a Topic

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")

Customizing HTTP Client

You can customize the HTTP client used for making requests:

customClient := &http.Client{Timeout: time.Second * 10}
client = client.SetHTTPClient(customClient)

Contributing

Contributions to this library are welcome. Please ensure to follow the coding standards and write tests for new features.

License

This library is licensed under the MIT License. See the LICENSE file for details.