Categorygithub.com/Valodim/go-fcm
modulepackage
0.0.0-20240202145952-e2e51cc73eb7
Repository: https://github.com/valodim/go-fcm.git
Documentation: pkg.go.dev

# README

go-fcm for FCM HTTP v1 API

GoDoc Build Status Go Report Card

Golang client library for Firebase Cloud Messaging v1 API.

The Legacy FCM HTTP Protocol has no construct to make a distinction between Android, iOS and Web notifications. The new HTTP v1 API does.

With this library, you:

Getting Started

CLI App

Usage

go install github.com/tevjef/go-fcm/cmd/fcm-send

NAME:
   go-fcm - Send messages to devices through Firebase Cloud Messaging.

USAGE:
   go-fcm [global options]

VERSION:
   1.0.0

COMMANDS:
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --topic value, -t value       The name of the topic to send a message to.
   --token value, -k value       The device topic or registration id to send a message to.
   --condition value, -c value   The condition to send a message to, e.g. 'foo' in topics && 'bar' in topics
   --title value                 The notification title.
   --body value                  The notification body.
   --validate-only               Validate the message, but don't send it.
   --credentials-location value  Location of the Firebase Admin SDK JSON credentials. [$CREDENTIALS_LOCATION]
   --project-id value            The id of your Firebase project. [$PROJECT_ID]
   --help, -h                    show help
   --version, -v                 print the version

As package

Usage

go get github.com/tevjef/go-fcm

Example

import (
	"encoding/json"
	"log"

	"github.com/tevjef/go-fcm"
)

func main() {
	// Create the message to be sent.
	msg := &fcm.SendRequest{
		ValidateOnly: true,
		Message: &fcm.Message{
			Token: "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
			Notification: &fcm.Notification{
				Title: "FCM Message",
				Body:  "This is a Firebase Cloud Messaging Topic Message!",
			},
			Apns: &fcm.ApnsConfig{
				Payload: &fcm.ApnsPayload{
					Aps: &fcm.ApsDictionary{
						Alert: &fcm.ApnsAlert{
							LaunchImage: "UILaunchImageFileKey",
						},
						Badge:            1,
						Category:         "NEW_MESSAGE_CATEGORY",
						ContentAvailable: int(fcm.ApnsContentAvailable),
					},
				},
			},
			Android: &fcm.AndroidConfig{
				Priority: string(fcm.AndroidHighPriority),
				TTL:      "84000s",
				Notification: &fcm.AndroidNotification{
					Icon:        "ic_notification",
					Color:       "#rrggbb",
					ClickAction: "MainActivity",
				},
			},
		},
	}

	// Create a FCM client to send the message.
	client, err := fcm.NewClient("projectID", "sa.json")
	if err != nil {
		log.Fatalln(err)
	}

	// Send the message and receive the response without retries.
	response, err := client.Send(msg)
	if err != nil {
		log.Fatalln(err)
	}

	log.Printf("%#v\n", response)
}

Example JSON sent to FCM HTTP v1 API

{
   "validate_only": true,
   "message": {
     "token": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
     or
     "topic": "cats", 
     or
     "condition": "'dogs' in topics || 'cats' in topics", 
     "apns": {
       "headers": {
         "apns-expiration": "14567890",
         "apns-priority": "10",
         "apns-topic": "my-topic",
         "apns-collapse-id": "my-collapse-id"
       },
       "payload": {
         "acme1": "bar",
         "acme2": [
           "bang",
           "whiz"
         ],
         "aps": {
           "alert": {
             "action-loc-key": "PLAY",
             "body": "Acme message received from Johnny Appleseed",
             "launch-image": "UILaunchImageFileKey",
             "loc-args": [
               "Jenna",
               "Frank"
             ],
             "loc-key": "GAME_PLAY_REQUEST_FORMAT",
             "title": "Acme title",
             "title-loc-args": [
               "Jenna",
               "Frank"
             ],
             "title-loc-key": "GAME_PLAY_REQUEST_FORMAT"
           },
           "badge": 1,
           "category": "NEW_MESSAGE_CATEGORY",
           "content-available": 1,
           "sound": "chime.aiff",
           "thread-id": "my-thread-id"
         }
       }
     },
     "android": {
       "collapse_key": "my-collapse-key",
       "priority": "HIGH",
       "ttl": "84000s",
       "restricted_package_name": "com.github.go-fcm",
       "data": {
         "acme1": "bar",
         "acme2": [
           "bang",
           "whiz"
         ]
       },
       "notification": {
         "title": "FCM Message",
         "body": "This is a Firebase Cloud Messaging Topic Message!",
         "icon": "ic_notification",
         "sound": "res_raw_notification_sound.mp3",
         "tag": "my-notification-tag",
         "color": "#rrggbb",
         "click_action": "MainActivity",
         "body_loc_key": "notification_body",
         "body_loc_args": [
           "Jenna",
           "Frank"
         ],
         "title_loc_key": "notification_title",
         "title_loc_args": [
           "Jenna",
           "Frank"
         ]
       }
     },
     "notification": {
       "title": "FCM Message",
       "body": "This is a Firebase Cloud Messaging Topic Message!"
     }
   }
 }

# Packages

No description provided by the author
No description provided by the author

# Functions

NewClient creates new Firebase Cloud Messaging Client based on a json service account file credentials file.
WithEndpoint returns Option to configure FCM Endpoint.
WithHTTPClient returns Option to configure HTTP Client.

# Constants

ApnsContentAvailable flags the notification to be delivered to the user’s device in the background.
ApnsContentUnavailable flags the notification to be delievered directly to the user without first waking your app up.
ApnsMutableContent flags the notification to be delievered via the notification service app extension.
ApnsNonMutableContent flags the notification to be delievered directly to the user without waking up the notification service app extension.

# Variables

AndroidHighPriority is the default priority for notification messages.
AndroidNormalPriority is the default priority for data messages.
ApnsHighPriority sends the push message immediately.
ApnsNormalPriority sends the push message at a time that takes into account power considerations for the device.
ApnsTypeAlert, if this is set a notifcation body is required by the push message.
ApnsTypeBackground use in conjuction with content-available on iOS 13+.
ErrInvalidApnsPriority occurs if the priority is not 5 or 10.
ErrInvalidMessage occurs if push notitication message is nil.
ErrInvalidTarget occurs if message topic is empty.
ErrInvalidTimeToLive occurs if TimeToLive more then 2419200.

# Structs

AndroidConfig represents android specific options for messages sent through FCM connection server.
AndroidNotification represents a notification to send to android devices.
ApnsAlert represents a APNS alert.
ApnsConfig represents Apple Push Notification Service specific options.
ApnsHeaders represents a collection of APNS headers.
ApnsPayload defines an APNS notification.
ApsDictionary defines an APNS notification.
Client abstracts the interaction between the application server and the FCM server via HTTP protocol.
HttpError contains the dump of the request and response for debugging purposes.
Message represents list of targets, options, and payload for HTTP JSON messages.
MulticastMessage represents a message that can be sent to multiple devices via Firebase Cloud Messaging (FCM).
BatchResponse represents the response from the `SendAll()` and `SendMulticast()` APIs.
Notification specifies the basic notification template to use across all platforms.
SendRequest has a flag for testing and the actual message to send.
SendResponse represents the status of an individual message that was sent as part of a batch request.
WebpushConfig represents the Webpush protocol outlined in https://tools.ietf.org/html/rfc8030 https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#WebpushConfig.
WebpushNotification represents a web notification to send via webpush protocol.

# Type aliases

AndroidMessagePriority represents the priority of a message to send to Android devices.
ApnsContentAvailability represents the content-available key in a APNS notification which may wither be 1 or 0.
ApnsMutableContent represents the mutable-content key in a APNS notification which may either be 1 or 0.
ApnsMessagePriority represents the priority of the notification.
ApnsMessageType represents the message type of the push notification.
Option configurates Client with defined option.