DEV Community

Douglas Makey Mendez Molero
Douglas Makey Mendez Molero

Posted on

Firebase Cloud Messaging with Golang

I needed to create a microservice for manage communications in my app, emails, SMS, and notifications with FCM.

So I decided to create a lib in for FCM for use en my app.

Example:

package main
import (
    "log"
    "github.com/douglasmakey/go-fcm"
    )
func main() {
    // init client
    client := fcm.NewClient("ApiKey")

    // You can use your HTTPClient 
    //client.SetHTTPClient(client)

    data := map[string]interface{}{
        "message": "From Go-FCM",
        "details": map[string]string{
            "name": "Name",
            "user": "Admin",
            "thing": "none",
        },
    }

    // You can use PushMultiple or PushSingle
    client.PushMultiple([]string{"token 1", "token 2"}, data)
    //client.PushSingle("token 1", data)

    // registrationIds remove and return map of invalid tokens
    badRegistrations := client.CleanRegistrationIds()
    log.Println(badRegistrations) 

    status, err := client.Send()
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    log.Println(status.Results)
}
Enter fullscreen mode Exit fullscreen mode

the source

👋 Kindness is contagious

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Community matters.

Top comments (3)

Collapse
 
tranphuoctien profile image
Tran Tien

Omg! How to you can check each sent success?

Collapse
 
ridwankustanto profile image
Ridwan Kustanto

Yea, how can we know? Should I make android app first to able check it?

Collapse
 
tyagip966 profile image
tyagip966

I want to push notification over web (web app).
Then how can i check ?
Is there any log created inside firebase ?

Thank You

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay