Skip to content

coreybutler/go-fcm-receiver

 
 

Repository files navigation

go-fcm-receiver

A library to subscribe to a GCM/FCM (Firebase Cloud Messaging) sender using protobuf.

The library was inspired by push-receiver and by this blog post.

The difference between this library and other libraries

  • This library receives FCM notifications the same way an Android device does. This library is an FCM client.
  • Other libraries (such as go-fcm) sends notifications via fcm, and does not receive notifications. Those libraries are an FCM server-side.

Install

import "github.com/morhaviv/go-fcm-receiver"

Requirements

  • Firebase sender id to receive notification

Usage

Creating a new device and listening

package main

import (
	go_fcm_receiver "github.com/morhaviv/go-fcm-receiver"
)

func main() {
	newDevice := go_fcm_receiver.FCMClient{
		SenderId:          845754665638,
		OnDataMessage: func(message []byte) {
			fmt.Println("Received a message:", string(message))
		},
	}
	privateKey, authSecret, err := newDevice.CreateNewKeys()
	if err != nil {
		panic(err)
	}
	fcmToken, gcmToken, androidId, securityToken, err := newDevice.Register()
	if err != nil {
		panic(err)
	}
	SaveDeviceDetails(fcmToken, gcmToken, androidId, securityToken, privateKey, authSecret)
	err = newDevice.StartListening()
	if err != nil {
		panic(err)
	}

}

Listening an existing device

package main

import (
	go_fcm_receiver "github.com/morhaviv/go-fcm-receiver"
)

func main() {
	oldDevice := go_fcm_receiver.FCMClient{
		SenderId:          845754665638, // Firebase Project ID
		GcmToken:          "<GCM_TOKEN>",
		FcmToken:          "<FCM_TOKEN>",
		AndroidId:         5240887932061714513, // The androidId returned when the device was created
		SecurityToken:     69534515778185919, // The securityToken returned when the device was created
		OnDataMessage: func(message []byte) {
			fmt.Println("Received a message:", string(message))
		},
	}
	
	err := oldDevice.LoadKeys("<PRIVATE_KEY_BASE64>", "<AUTH_SECRET_BASE64>")
	if err != nil {
		panic(err)
	}
	
	err = oldDevice.StartListening()
	if err != nil {
		panic(err)
	}
}

About

A library to subscribe to a GCM/FCM (Firebase Cloud Messaging) sender using protobuf

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Go 100.0%