From 537267741b8b8cdd4ecedeb3f2da82e1145566d8 Mon Sep 17 00:00:00 2001 From: Marco Colli Date: Tue, 30 Jan 2024 14:40:58 +0100 Subject: [PATCH] Add alternative method to pass the VAPID public key to JavaScript (closes #11) Co-authored-by: rossta --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 77e6bcb..e98cf5a 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,16 @@ navigator.serviceWorker.register('/service-worker.js') ### Subscribing to push notifications -The VAPID public key you generated earlier is made available to the client as a `UInt8Array`. To do this, one way would be to expose the urlsafe-decoded bytes from Ruby to JavaScript when rendering the HTML template. +The VAPID public key that you generated earlier needs to be made available to JavaScript, which can be done in many ways, for example with a fetch request or when rendering the HTML template in Ruby: -```javascript -window.vapidPublicKey = new Uint8Array(<%= Base64.urlsafe_decode64(ENV['VAPID_PUBLIC_KEY']).bytes %>); +```erb + ``` Your JavaScript code uses the `pushManager` interface to subscribe to push notifications, passing the VAPID public key to the subscription settings.