push-receiver
A library to subscribe to GCM/FCM and receive notifications within a node process.
For Electron, you can use electron-push-receiver instead which provides a convenient wrapper.
See this blog post for more details.
When should I use push-receiver ?
- I want to receive push notifications sent using Firebase Cloud Messaging in an 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 instead which provides a convenient wrapper.
Node
const { register, listen } = require('push-receiver-v2');
const config = {
firebase: {
apiKey: "XXxxXxX0x0x-Xxxx0-X0Xxxxx_0xxXx_XX0xXxX",
appID: "1:000000000000:android:xxx0xxxx0000x000xxx000",
projectID: "the-app-name"
},
vapidKey: ''
};
const credentials = await register(config);
storeCredentials(credentials)
const fcmToken = credentials.fcm.token;
sendTokenToBackendOrWhatever(fcmToken);
const credentials = getSavedCredentials()
const persistentIds = getPersistentIds() || []
await listen({ ...credentials, persistentIds}, onNotification);
function onNotification({ notification, persistentId }) {
updatePersistentIds([...persistentIds, persistentId]);
display(notification)
}
Test notification
To test, you can use the send script 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>"
Note: The send endpoint is deprecated and removed as of June 20, 2024: https://firebase.google.com/support/faq#fcm-depr-features