Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README for subscribing #11

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,23 @@ 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 encoded VAPID public key you generated earlier needs to be made available to JavaScript, which can be done when rendering the HTML template in Ruby:

```javascript
window.vapidPublicKey = new Uint8Array(<%= Base64.urlsafe_decode64(ENV['VAPID_PUBLIC_KEY']).bytes %>);
```html
<script type="application/javascript">
window.vapidPublicKey = <%= ENV['VAPID_PUBLIC_KEY'].delete('=') %>);
</script>
```

*Alternative* The VAPID public key may also made available to the client as a `UInt8Array` from the urlsafe-decoded bytes.

```html
<script type="application/javascript">
window.vapidPublicKey = new Uint8Array(<%= Base64.urlsafe_deco
de64(ENV['VAPID_PUBLIC_KEY']).bytes %>);
</script>
```

Your JavaScript code uses the `pushManager` interface to subscribe to push notifications, passing the VAPID public key to the subscription settings.

```javascript
Expand Down
Loading