Skip to content

Commit

Permalink
feat: Send messages to Service Worker to save and delete subscription -
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrennand committed Feb 24, 2022
1 parent 36a09e6 commit 68acf5a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion frontend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ const subscribeToPushNotifications = async () => {
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(publicVapidKey),
});
// Send message to the Service Worker (sw.js) to save the subscription
console.log('Sending message to Service Worker.');
registration.active.postMessage({
action: 'SAVE_SUBSCRIPTION',
subscription: JSON.stringify(subscription)
});
// Create object containing selected server(s) and subscription data
const data = {
servers: servers,
Expand Down Expand Up @@ -107,8 +113,13 @@ const unsubscribeFromPushNotifications = async () => {
// Get current subscription
console.log('Getting subscription to unsubscribe from push notifications.');
const subscription = await registration.pushManager.getSubscription();
// Send message to the Service Worker (sw.js) to delete the subscription
console.log('Sending message to Service Worker.');
registration.active.postMessage({
action: 'DELETE_SUBSCRIPTION'
});
// Send backend API request to unsubscribe from push notifications
console.log('Removing subscription.')
console.log('Removing subscription.');
await fetch('/remove-subscription', {
credentials: 'same-origin',
method: 'DELETE',
Expand Down

0 comments on commit 68acf5a

Please sign in to comment.