fcm

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2024 License: MIT Imports: 8 Imported by: 0

README

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")
}

Documentation

Overview

Package fcm provides message notification integration for Firebase Cloud Messaging (FCM).

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")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v1.0.0

type Option func(*Service) error

Option is a function that configures a Service.

func WithCredentialsFile added in v1.0.0

func WithCredentialsFile(filename string) Option

WithCredentialsFile returns an Option to configure the FCM client with a credentials file.

func WithHTTPClient added in v1.0.0

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient returns an Option to configure the FCM client with a custom HTTP client.

func WithProjectID added in v1.0.0

func WithProjectID(projectID string) Option

WithProjectID returns an Option to configure the FCM client with a project ID.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service encapsulates the FCM client along with internal state for storing device tokens.

func New

func New(ctx context.Context, opts ...Option) (*Service, error)

New returns a new instance of a FCM notification service.

func (*Service) AddReceivers

func (s *Service) AddReceivers(deviceTokens ...string)

AddReceivers takes FCM device tokens and appends them to the internal device tokens slice.

func (*Service) Send

func (s *Service) Send(ctx context.Context, subject, message string) error

Send takes a message subject and a message body and sends them to all previously set devices.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL