Giter VIP home page Giter VIP logo

push-receiver's Introduction

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 @eneris/push-receiver

Requirements

  • Node v16 (async/await/randomUUID support)
  • Firebase sender id to receive notification
  • Firebase serverKey to send notification (optional)

Usage

ClientConfig

interface ClientConfig {
    credentials?: Credentials // Will be generated if missing - save this after first use!
    persistentIds?: PersistentId[] // Default - []
    senderId: string // Required
    bundleId?: string // Default - 'receiver.push.com'
    chromeId?: string // Default - 'org.chromium.linux'
    chromeVersion?: string // Default - '94.0.4606.51'
    skipFcmRegistration?: boolean // Default - false
    logLevel?: keyof typeof LogLevels // 'NONE'|'DEBUG'|'VERBOSE' - default: 'NONE'
    vapidKey?: string // Default - default firebase VAPID key
}

Node example

import PushReceiver from '@eneris/push-receiver'
import { argv as parsedArgs } from 'yargs'

if (!parsedArgs.senderId) {
    console.error('Missing senderId')
    return
}

(async () => {
    const instance = new PushReceiver({
        logLevel: parsedArgs.logLevel || 'DEBUG',
        senderId: parsedArgs.senderId,
        persistentIds: [], // Recover stored ids of all previous notifications
    })

    const stopListeningToCredentials = instance.onCredentialsChanged(({ oldCredentials, newCredentials }) => {
        console.log('Client generated new credentials.', newCredentials)
        // Save them somewhere! And decide if thing are needed to re-subscribe
    })

    const stopListeningToNotifications = instance.onNotification(({ notification }) => {
        // Do someting with the notification
        console.log('Notification received', notification)
    })

    await instance.connect()

    if (parsedArgs.serverId) {
        await instance.testMessage(parsedArgs.serverId)
    }

    stopListeningToCredentials()
    stopListeningToNotifications()
    instance.destroy()
})()

push-receiver's People

Contributors

eneris avatar matthieulemoine avatar dependabot[bot] avatar koush avatar patrick-remy avatar pedrokantar avatar selfisekai avatar benoist avatar ibash avatar julianhille avatar uzitech avatar realjax avatar mderrier avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.