npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-fcm-notification

v1.1.2

Published

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

Downloads

45

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.