DEV Community

Douglas Makey Mendez Molero
Douglas Makey Mendez Molero

Posted on

6 2

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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up