Cheatography
https://cheatography.com
Web-Server
Setup: |
|
Web Server Extension for Chrome: |
|
Initialize State
Set the initial subscription value: |
swRegistration.pushManager.getSubscription() .then(function(subscription) |
Handle a Push Event
Triggering a push message: |
self.addEventListener('push', function(event) |
Unsubscribe User
Unsubscribe Function: |
swRegistration.pushManager.getSubscription() .then(function(subscription) { if (subscription) { return subscription.unsubscribe(); } |
|
|
Register a service worker
Register a service worker: |
navigator.serviceWorker.register('sw.js').then(function(swReg) |
Handle Permission Denied
Handling permission denied: |
if (Notification.permission === 'denied') |
Notification Click
Add click event to notification: |
self.addEventListener('notificationclick', function(event) |
Application Server key
Set application server key: |
const applicationServerPublicKey = '<Your Public Key>'; |
Subscribe User
Handle user subscription: |
pushButton.addEventListener('click', function() { pushButton.disabled = true; if (isSubscribed) { // TODO: Unsubscribe user } else { subscribeUser(); } |
Update Subscription: |
updateSubscriptionOnServer(subscription); |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by Dominik Mihalic