From 7de61719117c2896ec0320fbe6380d23300c15ff Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Thu, 24 Aug 2023 21:21:15 +0800 Subject: [PATCH] Notes on service worker for development of web push --- web-push.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/web-push.md b/web-push.md index fb7e31d..5b532ff 100644 --- a/web-push.md +++ b/web-push.md @@ -49,7 +49,38 @@ function askPermission(): Promise { If the user grants permission, the client application registers a service worker using the ServiceWorkerRegistration API. - + +Here's a version which can be used for testing locally. Note there can be +caching issues in your browser! Incognito is highly recommended. + +self.addEventListener('push', function(event: PushEvent) { + console.log('Received a push message', event); + + const title = 'Push message'; + const body = 'The message body'; + const icon = '/images/icon-192x192.png'; + const tag = 'simple-push-demo-notification-tag'; + + event.waitUntil( + self.registration.showNotification(title, { + body: body, + icon: icon, + tag: tag + }) + ); +}); + + +vue.config.js + +module.exports = { + pwa: { + workboxOptions: { + importScripts: ['sw-dev.ts'] + } + } +} + In the next step, BROWSER requests a data structure from SERVICE called a VAPID (Voluntary Application Server Identification) which is the public key from a key-pair.