JSPM

ts-fcm-notification

1.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q25575F
  • License ISC

A simple Firebase Cloud Messaging (FCM) notification library for Node.js

Package Exports

  • ts-fcm-notification
  • ts-fcm-notification/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-fcm-notification) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

FCM Notification TS

Overview

ts-fcm-notification is a simple library to send push notifications using Firebase Cloud Messaging (FCM) in Node.js. This library provides functionalities to send messages to individual tokens, multiple tokens, and topics.

Installation

To install the library, use npm or yarn:

npm install ts-fcm-notification

or

yarn add ts-fcm-notification

Usage

Initialization

First, initialize the library with your Firebase service account key:

import { FCM } from 'ts-fcm-notification';
import key from './path/to/your-service-account-file.json';

const fcm = FCM(JSON.stringify(key));

Sending Messages

Send a Message to a Single Token


const

 message = {
  data: {
    message: 'Hello FCM, You are working fine',
  },
  notification: {
    title: 'Test Notification',
    body: 'Hello FCM, You are working fine body',
  },
  token: 'your-device-token',
};

const response = await fcm.send(message);
console.log(response);

Send a Message to Multiple Tokens

const tokens = ['token1', 'token2', 'token3'];
const message = {
  data: {
    message: 'Hello FCM, You are working fine',
  },
  notification: {
    title: 'Test Notification',
    body: 'Hello FCM, You are working fine body',
  },
};

const response = await fcm.multipleTokens(0, tokens, message);
console.log(response);

Send a Message to a Topic

const message = {
  data: {
    message: 'Hello FCM, You are working fine',
  },
  notification: {
    title: 'Test Notification',
    body: 'Hello FCM, You are working fine body',
  },
  topic: 'your-topic',
};

const response = await fcm.send(message);
console.log(response);

Send a Message to Multiple Topics

const topics = ['topic1', 'topic2'];
const message = {
  data: {
    message: 'Hello FCM, You are working fine',
  },
  notification: {
    title: 'Test Notification',
    body: 'Hello FCM, You are working fine body',
  },
};

const response = await fcm.sendToMultipleTopics(0, topics, message);
console.log(response);

Subscribing and Unsubscribing from Topics

Subscribe to a Topic

const token = 'your-device-token';
const topic = 'your-topic';

const response = await fcm.subscribeToTopic(token, topic);
console.log(response);

Unsubscribe from a Topic

const token = 'your-device-token';
const topic = 'your-topic';

const response = await fcm.unsubscribeFromTopic(token, topic);
console.log(response);

API Reference

FCM(key: string)

Initializes the FCM library with the provided service account key.

send(payload: any)

Sends a message to a single token or topic.

multipleTokens(i: number, tokens: string[], payload: any)

Sends a message to multiple tokens.

sendToMultipleTopics(i: number, topics: string[], payload: any)

Sends a message to multiple topics.

subscribeToTopic(token: string, topic: string)

Subscribes a device token to a topic.

unsubscribeFromTopic(token: string, topic: string)

Unsubscribes a device token from a topic.

Example

An example usage of the library can be found in the examples/index.ts file.

import { FCM } from '../lib/index';
import key from './key.json';

const token = 'your-device-token';
const fcm = FCM(JSON.stringify(key));

const message = {
  data: {
    message: 'Hello FCM, You are working fine',
  },
  notification: {
    title: 'Test Notification',
    body: 'Hello FCM, You are working fine body',
  },
  topic: 'all',
};

const sendMessge = async () => {
  const response = await fcm.send(message);
  console.log(response);
};

sendMessge();

License

This project is licensed under the ISC License. See the LICENSE file for details.