1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-11-24 08:22:18 +02:00
notify/service/fcm
2022-08-04 15:58:38 +02:00
..
doc.go style(fmt): make fmt 2022-08-04 15:58:38 +02:00
fcm_test.go feat(service): add fcm 2022-05-08 12:56:01 +03:00
fcm.go feat(service): add fcm 2022-05-08 12:56:01 +03:00
mock_fcmClient.go style(fmt): make fmt 2022-08-04 15:58:38 +02:00
README.md feat(service): add fcm 2022-05-08 12:56:01 +03:00

Firebase Cloud Messaging (FCM)

go.dev reference

Prerequisites

Navigate to Firebase console, login with your Google account and create a new project. You will find the Server Key in the project settings screen under Cloud Messaging tab. When the server is up and running you can add Firebase to your applications following the instructions in the Engage/Cloud Messaging section.

To test the integration with a device you can use FCM toolbox. You can also download the app to your mobile, create a device token and test the reachability of your device.

Usage

package main

import (
	"context"
	"log"

	"github.com/nikoksr/notify"
	"github.com/nikoksr/notify/service/fcm"
)

func main() {
	fcmSvc, err := fcm.New("server_api_key")
	if err != nil {
		log.Fatalf("fcm.New() failed: %s", err.Error())
	}

	fcmSvc.AddReceivers("deviceToken1")

	notifier := notify.New()
	notifier.UseServices(fcmSvc)

	// Use context.Background() if you want to send a simple notification message.
	ctx := context.Background()

	// Optionally, you can include additional data in the message payload by adding the corresponding value to the context.
	ctxWithData := context.WithValue(ctx, fcm.DataKey, map[string]interface{}{
		"some-key":  "some-value",
		"other-key": "other-value",
	})

	// Optionally, you can specify a total of retry attempts per each message by adding the corresponding value to the context.
	ctxWithDataAndRetries := context.WithValue(ctxWithData, fcm.RetriesKey, 3)

	err = notifier.Send(ctxWithDataAndRetries, "subject", "message")
	if err != nil {
		log.Fatalf("notifier.Send() failed: %s", err.Error())
	}

	log.Println("notification sent")
}