# push-receiver

> A module to subscribe to GCM/FCM and receive notifications within a node process.

Latest version **2.1.1** (published 2020-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install push-receiver
pnpm add push-receiver
yarn add push-receiver
bun add push-receiver
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2020-09-08 |
| First published | 2017-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 54.2 KB |
| Known vulnerabilities | 0 (+13 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 219 |
| Author | MatthieuLemoine |
| Maintainers | matthieulemoine |
| Keywords | push, service, fcm, gcm, notifications, node, electron, receiver |

## Links

- npm: https://www.npmjs.com/package/push-receiver
- Repository: https://github.com/MatthieuLemoine/push-receiver
- Homepage: https://github.com/MatthieuLemoine/push-receiver#readme
- Issues: https://github.com/MatthieuLemoine/push-receiver/issues
- npm.io page: https://npm.io/package/push-receiver

## Dependencies (6)

- [long](https://npm.io/package/long.md) ^3.2.0
- [uuid](https://npm.io/package/uuid.md) ^3.1.0
- [request](https://npm.io/package/request.md) ^2.81.0
- [http_ece](https://npm.io/package/http_ece.md) ^1.0.5
- [protobufjs](https://npm.io/package/protobufjs.md) ^6.8.0
- [request-promise](https://npm.io/package/request-promise.md) ^4.2.1

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 2.1.1 (latest) — 2020-09-08
- 2.1.0 — 2019-02-21
- 2.0.2 — 2018-10-02
- 2.0.1 — 2018-08-27
- 2.0.0 — 2018-03-29
- 1.1.5 — 2017-11-20
- 1.1.4 — 2017-11-16
- 1.1.3 — 2017-11-15
- 1.1.2 — 2017-11-14
- 1.1.1 — 2017-11-10
- 1.1.0 — 2017-11-08
- 1.0.1 — 2017-10-26
- 1.0.0 — 2017-10-25

## README

# push-receiver

A library to subscribe to GCM/FCM and receive notifications within a node process.

For [Electron](https://github.com/electron/electron), you can use [electron-push-receiver](https://github.com/MatthieuLemoine/electron-push-receiver) instead which provides a convenient wrapper.

See [this blog post](https://medium.com/@MatthieuLemoine/my-journey-to-bring-web-push-support-to-node-and-electron-ce70eea1c0b0) for more details.

## When should I use `push-receiver` ?

- I want to **receive** push notifications sent using Firebase Cloud Messaging in an [electron](https://github.com/electron/electron) desktop application.
- I want to communicate with a node process/server using Firebase Cloud Messaging infrastructure.

## When should I not use `push-receiver` ?

- I want to **send** push notifications (use the firebase SDK instead)
- My application is running on a FCM supported platform (Android, iOS, Web).

## Install

`
npm i -S push-receiver
`

## Requirements

- Node v8 (async/await support)
- Firebase sender id to receive notification
- Firebase serverKey to send notification (optional)

## Usage

### Electron

You can use [electron-push-receiver](https://github.com/MatthieuLemoine/electron-push-receiver) instead which provides a convenient wrapper.

### Node

```javascript
const { register, listen } = require('push-receiver');

// First time
// Register to GCM and FCM
const credentials = await register(senderId); // You should call register only once and then store the credentials somewhere
storeCredentials(credentials) // Store credentials to use it later
const fcmToken = credentials.fcm.token; // Token to use to send notifications
sendTokenToBackendOrWhatever(fcmToken);


// Next times
const credentials = getSavedCredentials() // get your saved credentials from somewhere (file, db, etc...)
// persistentIds is the list of notification ids received to avoid receiving all already received notifications on start.
const persistentIds = getPersistentIds() || [] // get all previous persistentIds from somewhere (file, db, etc...)
await listen({ ...credentials, persistentIds}, onNotification);

// Called on new notification
function onNotification({ notification, persistentId }) {
  // Update list of persistentId in file/db/...
  updatePersistentIds([...persistentIds, persistentId]);
  // Do someting with the notification
  display(notification)
}
```

### Test notification

To test, you can use the [send script](scripts/send/index.js) provided in this repo, you need to pass your serverKey and the FCM token as arguments :

```
node scripts/send --serverKey="<FIREBASE_SERVER_KEY>" --token="<FIREBASE_TOKEN>"
```

---
_Source: https://npm.io/package/push-receiver · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
