Skip to content

This is a simple wrapper service for sending notifications built on top of Firebase Cloud Messaging (FCM). It provides a function to send notifications with customizable options.

License

Notifications You must be signed in to change notification settings

PrantaDas/firemsg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FIRE MSG

Overview

This is a simple wrapper service for sending notifications built on top of Firebase Cloud Messaging (FCM). It provides a function to send notifications with customizable options.

Installation

You can install firemsg using npm, yarn, pnpm.

npm

<not published yet>

pnpm

<not published yet>

yarn

<not published yet>

Example

TypeScript

First, import the package and initialize the firemsg with the path to your Firebase Admin SDK JSON file:

import { FCM, NotificationOptions } from 'firemsg';

// optional, if the credentials file is present in the root driectory of the application
const credentialPath ="path/to/credentials/json"

// Initialize FCM service with Firebase Admin SDK JSON file path
const fcm = FCM(credentialPath);

// Define notification options
const options: NotificationOptions = {
    title: 'New Message',
    body: 'You have received a new message',
    topic: 'news',
    imageUrl: 'https://example.com/image.png',
    sound: 'default',
};

// Send a notification
try {
    // optional, if the credentials file is present in the root driectory of the application
    const credentialPath ="path/to/credentials/json"
    const fcm = FCM("app_name", credentialPath);
    const messageId = await fcm.send(options);
    console.log('Notification sent. Message ID:', messageId);
} catch (error) {
    console.error('Failed to send notification:', error);
}

CommonJS

First, require the package and initialize the Firebase Cloud Messaging service with the path to your Firebase Admin SDK JSON file:

const { FCM } = require('firemsg');


// optional, if the credentials file is present in the root driectory of the application
const credentialPath ="path/to/credentials/json"
// Initialize FCM service with Firebase Admin SDK JSON file path
const fcm = FCM("app_name", credentialPath);

// Define notification options
const options = {
    title: 'New Message',
    body: 'You have received a new message',
    topic: 'news',
    imageUrl: 'https://example.com/image.png',
    sound: 'default',
};

// Send a notification
fcm.send(options)
    .then((messageId) => {
        console.log('Notification sent. Message ID:', messageId);
    })
    .catch((error) => {
        console.error('Failed to send notification:', error);
    });

API

FCM(name: string, credentialPath?: string): { send: (options: NotificationOptions) => Promise<string> }

Initializes the Firebase Cloud Messaging service service with the provided Firebase Admin SDK JSON file path. Returns an object with a send method.

Parameters

  • name (string) : The name of the service

  • credentialPath (string,optional): The path to the Firebase Admin SDK JSON file.

send(options: NotificationOptions): Promise<string>

Sends a notification using Firebase Cloud Messaging with the provided options.

Parameters

  • options (NotificationOptions): The notification options.

    • title (string): The title of the notification.
    • body (string): The body of the notification.
    • topic (string): The topic to which the notification will be sent.
    • imageUrl (string, optional): The URL of the image to be included in the notification.
    • sound (string, optional): The sound to be played when the notification is received.
    • data (Data,optional): The addtional data to be sent with the notification.

Returns

  • Promise<string>: The message ID of the sent notification.

Note

This package is created as part of a learning process. Feedback and contributions are welcome! Feel free to open issues or pull requests on GitHub.

License This project is licensed under the MIT License.

About

This is a simple wrapper service for sending notifications built on top of Firebase Cloud Messaging (FCM). It provides a function to send notifications with customizable options.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published