Web Notifier
web-notifier
is a an easy to use library for providing push notifications on the web. We take care of all the hard stuff: scheduling, creating tokens, pushing to clients, and so on.
Getting Started
- Install the dependency:
npm i -S web-notifier
- Generate VAPID Keys, these should be stored somewhere where they can be reused between sessions (ex.
process.env.VAPID_PRIVATE_KEY
andprocess.env.VAPID_PUBLIC_KEY
).
console.log(WebNotifier.generateVAPIDKeys());
- Instantiate an instance on the server
const { WebNotifier, MongoAdapter } = require('web-notifier');
const notifier = new WebNotifier({
vapidKeys: {
publicKey: process.env.VAPID_PUBLIC_KEY,
privateKey: process.env.VAPID_PRIVATE_KEY,
email: process.env.VAPID_EMAIL
},
notificationDefaults: {
badge: '/notification-badge.png',
icon: '/android-chrome-192x192.png',
url: '/'
},
getUserPushSubscription,
removeUserPushSubscription,
adapter: new MongoAdapter(db.connection)
});
- Send a push notification
notifier.send(userId, {
title: "This will send immediately"
});
You can also defer the message till a later date by using
cont when = new Date();
notifier.schedule(when, userId, {
title: "This will send at variable 'when'."
});