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.
You can install firemsg using npm, yarn, pnpm.
<not published yet><not published yet><not published yet>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);
}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);
});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.
-
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.
-
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.
Promise<string>: The message ID of the sent notification.
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.