Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc/client #3

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# 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:
```go
import "path/to/fcm"
```

Ensure you have the necessary dependencies installed:

```bash
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:

```go
client := fcm.NewClient()
```

### Setting Service Account Credentials

Before sending messages, set your service account credentials:

```go
client = client.WithCredentialFile("path/to/serviceAccountKey.json")
```

### Sending a Message

To send a message, create a `MessagePayload` and use the `Send` method:

```go
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:

```go
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:

```go
customClient := &http.Client{Timeout: time.Second * 10}
client = client.WithHTTPClient(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](/LICENCE) file for details.
7 changes: 7 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module example

go 1.21.0

require github.com/patrickkabwe/go-fcm v0.2.0

require github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
4 changes: 4 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/patrickkabwe/go-fcm v0.2.0 h1:dqRyYSJwC6Dy3ABQMHdIK2jXqN4Nf2eXaHZ7JCYdYmI=
github.com/patrickkabwe/go-fcm v0.2.0/go.mod h1:F//w2lQRwN7q6lXblqvrIrbp0S4ZL1MwxnO0ldTZPQs=
33 changes: 33 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"
"log"

"github.com/patrickkabwe/go-fcm"
)

func main() {
// Create a new FCM client.
client := fcm.NewClient().
WithCredentialFile("path/to/your/credential.json")

// Create a new message payload.
msg := &fcm.MessagePayload{
Message: fcm.Message{
Notification: fcm.Notification{
Title: "Hello",
Body: "World",
},
Topic: "news",
},
}

// Send the message to the FCM server.
if err := client.SendToTopic(msg); err != nil {
log.Fatalf("failed to send message: %v", err)
}

fmt.Println("message sent successfully")

}
Loading