A simple implement of web push notifications.
A simple implement of web push notifications.
dom.moduleScripts.enabled
flag)
npm install
npm run dev
http://localhost:3000/server
and create a key pair.http://localhost:3000/client
.Read these first: How Push Works, Google’s codelab
application server keys
.application server public key
from the server.application server public key
.PushSubscription
from the push service.PushSubscription
to the server.PushSubscription
to the DB.PushSubscription
and application server keys
are needed.This implement uses localStorage
to share data between server and client. That’s a major - and maybe the only - difference from Google’s codelab.
// client-side
const updateSubscriptionOnServer = (subscription) => {
// TODO: Send subscription to application server
window.localStorage.setItem('subscription', JSON.stringify(subscription))
}
// server-side
const subscription = JSON.parse(window.localStorage.getItem('subscription'))
// ...
const result = await webPush.sendNotification(subscription, data, options)
localStorage
with a database: We have to use databases for real world applications.pushsubscriptionchange
event: I’m still confused with this. Please help me if you can.