Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/prantadas/firemsg

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.
https://github.com/prantadas/firemsg

firebase-admin-sdk nodejs npm-package typescript

Last synced: 1 day ago
JSON representation

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.

Awesome Lists containing this project

README

        

# 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
```sh

```

## pnpm
```sh

```

## yarn
```sh

```

# Example
## TypeScript
First, import the package and initialize the `firemsg` with the path to your Firebase Admin SDK JSON file:
```ts
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:

```js
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 }`

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`

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`: 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](https://github.com/PrantaDas/firemsg).

License
This project is licensed under the [MIT License](https://github.com/PrantaDas/firemsg/blob/main/LICENSE).