Unofficial Firebase Admin SDK for Golang
Firebase Admin SDK for Golang
On Wednesday, May 17, 2017 Google announced at Google IO : Open sourcing the Firebase SDKs. But for now, there is no official Admin SDK for Golang, only Java, Node and Python Admin SDKs.
So welcome go-firebase-admin SDK :)
Note
If you decide to use this unofficial SDK still in development,
please use any package manager to fix version, there will be a lot of breaking changes.
Install the package with go:
go get github.com/acoshift/go-firebase-admin
This go-firebase-admin SDK supports the following functions :
Authentication
User Management API
Realtime Database API
Cloud Messaging API
You can find more details about go-firebase-admin on godoc.org.
You need a service_account.json file, if you don't have an admin SDK service_account.json, please check this guide
You need a Firebase API Key for FCM, whose value is available in the Cloud Messaging tab of the Firebase console Settings panel
Initialize Firebase Admin SDK
package main
import (
"io/ioutil"
"google.golang.org/api/option"
"github.com/acoshift/go-firebase-admin"
)
func main() {
// Init App with service_account
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
ProjectID: "YOUR_PROJECT_ID",
}, option.WithCredentialsFile("service_account.json"))
if err != nil {
panic(err)
}
}
package main
import (
"io/ioutil"
"google.golang.org/api/option"
"github.com/acoshift/go-firebase-admin"
)
func main() {
// Init App with service_account
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
ProjectID: "YOUR_PROJECT_ID",
}, option.WithCredentialsFile("service_account.json"))
if err != nil {
panic(err)
}
// Firebase AUth
firAuth := firApp.Auth()
// VerifyIDToken
claims, err := firAuth.VerifyIDToken("My token")
// CreateCustomToken
myClaims := make(map[string]string)
myClaims["name"] = "go-firebase-admin"
myClaims["ID"] = "go-go-go"
cutomToken, err := firAuth.CreateCustomToken(claims.UserID, myClaims)
}
package main
import (
"io/ioutil"
"google.golang.org/api/option"
"github.com/acoshift/go-firebase-admin"
)
func main() {
// Init App with service_account
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
ProjectID: "YOUR_PROJECT_ID",
DatabaseURL: "YOUR_DATABASE_URL",
}, option.WithCredentialsFile("service_account.json"))
if err != nil {
panic(err)
}
// Firebase Database
firDatabase := firApp.Database()
type dinosaurs struct {
Appeared int64 `json:"appeared"`
Height float32 `json:"height"`
Length float32 `json:"length"`
Order string `json:"order"`
Vanished int64 `json:"vanished"`
Weight int `json:"weight"`
}
r := firDatabase.Ref("test/path")
err = r.Child("bruhathkayosaurus").Set(&dinosaurs{-70000000, 25, 44, "saurischia", -70000000, 135000})
if err != nil {
panic(err)
}
// Remove
err = r.Remove()
if err != nil {
panic(err)
}
// Snapshot
snapshot, err := r.OrderByChild("height").EqualTo(0.6).OnceValue()
if err != nil {
panic(err)
}
}
package main
import (
"io/ioutil"
"google.golang.org/api/option"
"github.com/acoshift/go-firebase-admin"
)
func main() {
// Init App with service_account
firApp, err := firebase.InitializeApp(context.Background(), firebase.AppOptions{
ProjectID: "YOUR_PROJECT_ID",
DatabaseURL: "YOUR_DATABASE_URL",
APIKey: "YOUR_API_KEY",
}, option.WithCredentialsFile("service_account.json"))
if err != nil {
panic(err)
}
// FCM
firFCM := firApp.FCM()
// SendToDevice
resp, err := firFCM.SendToDevice(context.Background(), "mydevicetoken",
firebase.Message{Notification: firebase.Notification{
Title: "Hello go firebase admin",
Body: "My little Big Notification",
Color: "#ffcc33"},
})
if err != nil {
panic(err)
}
// SendToDevices
resp, err := firFCM.SendToDevices(context.Background(), []string{"mydevicetoken"},
firebase.Message{Notification: firebase.Notification{
Title: "Hello go firebase admin",
Body: "My little Big Notification",
Color: "#ffcc33"},
})
if err != nil {
panic(err)
}
// SubscribeDeviceToTopic
resp, err := firFCM.SubscribeDeviceToTopic(context.Background(), "mydevicetoken", "/topics/gofirebaseadmin")
// it's possible to ommit the "/topics/" prefix
resp, err := firFCM.SubscribeDeviceToTopic(context.Background(), "mydevicetoken", "gofirebaseadmin")
if err != nil {
panic(err)
}
// UnSubscribeDeviceFromTopic
resp, err := firFCM.UnSubscribeDeviceFromTopic(context.Background(), "mydevicetoken", "/topics/gofirebaseadmin")
// it's possible to ommit the "/topics/" prefix
resp, err := firFCM.UnSubscribeDeviceFromTopic(context.Background(), "mydevicetoken", "gofirebaseadmin")
if err2 != nil {
panic(err)
}
}
MIT License
Copyright (c) 2016 Thanatat Tamtan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.