push-receiver-kzs
v1.0.1
Published
A module to subscribe to GCM/FCM and receive notifications within a node process.
Maintainers
Readme
push-receiver-kzs
A library to subscribe to GCM/FCM and receive notifications within a node process.
(由上游 push-receiver / @eneris/push-receiver 思路延续,以 npm 包名 push-receiver-kzs 发布。)
When should I use push-receiver-kzs ?
- 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-kzs ?
- 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-kzsRequirements
- Node >=16(含 Electron 22 内置 Node 16.x);HTTP 使用
node-fetch以兼容无原生fetch的环境 - Firebase credentials from
Step 1- https://firebase.google.com/docs/web/setup
Acknowledgements
- https://github.com/MatthieuLemoine — initial module this work iterated on
- https://github.com/eneris/push-receiver — upstream maintenance and ideas
Usage
ClientConfig
interface ClientConfig {
credentials?: Credentials // Will be generated if missing - save this after first use!
persistentIds?: PersistentId[] // Default - []
bundleId?: string // Default - 'receiver.push.com'
chromeId?: string // Default - 'org.chromium.linux'
chromeVersion?: string // Default - '94.0.4606.51'
debug?: boolean // Enables debug console logs
heartbeatIntervalMs?: number // Default - 5 * 60 * 1000
firebase: FirebaseConfig // Full client firebase credentials are now needed
}Node example
import { PushReceiver, PushSender } from 'push-receiver-kzs'
;(async () => {
const instance = new PushReceiver({
debug: true,
persistentIds: [], // Recover stored ids of all previous notifications
firebase: {
// ... Firebase web client config (projectId, appId, apiKey, messagingSenderId, …)
},
// Omit `credentials` on first run; persist `newCredentials` from onCredentialsChanged, then pass them back here
})
const stopListeningToCredentials = instance.onCredentialsChanged(({ oldCredentials, newCredentials }) => {
console.log('Client generated new credentials.', newCredentials)
// Persist newCredentials and reload them on the next process start
})
const stopListeningToNotifications = instance.onNotification((notification) => {
console.log('Notification received', notification)
})
await instance.connect()
console.log('connected')
const sender = new PushSender({
/* Firebase service account JSON: type, project_id, private_key, client_email, … */
})
await sender.testMessage(instance.fcmToken)
console.log('message sent')
stopListeningToCredentials()
stopListeningToNotifications()
instance.destroy()
})()