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.
push-receiver
?
When should I use - 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.
push-receiver
?
When should I not use - 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 = ; // First time// Register to GCM and FCMconst credentials = await ; // You should call register only once and then store the credentials somewhere // Store credentials to use it laterconst fcmToken = credentialsfcmtoken; // Token to use to send notifications; // Next timesconst credentials = // 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 = || // get all previous persistentIds from somewhere (file, db, etc...)await ; // Called on new notification { // Update list of persistentId in file/db/... ; // Do someting with the 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>"