Browse Source

Added messaging to service worker. Restarting integration procss

pull/78/head
Matthew Raymer 1 year ago
parent
commit
805c7b4810
  1. 11
      src/App.vue
  2. 48
      sw_scripts/additional-scripts.js
  3. 5175
      sw_scripts/safari-notifications.js

11
src/App.vue

@ -279,6 +279,17 @@ export default class App extends Vue {
} }
private askPermission(): Promise<NotificationPermission> { private askPermission(): Promise<NotificationPermission> {
if ("serviceWorker" in navigator && navigator.serviceWorker.controller) {
const secret = localStorage.getItem("secret");
if (secret) {
navigator.serviceWorker.controller.postMessage({
type: "SEND_LOCAL_DATA",
data: secret,
});
}
}
// Check if Notifications are supported // Check if Notifications are supported
if (!("Notification" in window)) { if (!("Notification" in window)) {
alert("This browser does not support notifications."); alert("This browser does not support notifications.");

48
sw_scripts/additional-scripts.js

@ -1,33 +1,31 @@
const notifications = require("./safari-notifications.js"); importScripts("./safari-notifications.js");
self.addEventListener("push", function (event) { self.addEventListener("push", function (event) {
let payload; event.waitUntil((async () => {
if (event.data) { try {
payload = JSON.parse(event.data.text()); let payload;
} if (event.data) {
payload = JSON.parse(event.data.text());
const title = payload ? payload.title : "Custom Title"; }
const options = { let value = await self.getNotificationCount();
body: payload ? payload.body : "Custom body text", const title = payload ? payload.title : "Custom Title";
icon: payload ? payload.icon : "icon.png", const options = {
badge: payload ? payload.badge : "badge.png", body: payload ? value : "SAMPLE",
}; icon: payload ? payload.icon : "icon.png",
badge: payload ? payload.badge : "badge.png",
event.waitUntil(self.registration.showNotification(title, options)); };
await self.registration.showNotification(title, options);
} catch (error) {
console.error('Error in processing the push event:', error);
}
})());
}); });
self.addEventListener("message", function (event) { self.addEventListener('message', event => {
const data = event.data; if (event.data && event.data.type === 'SEND_LOCAL_DATA') {
self.secret = event.data.data;
const result = notifications.getNotificationCount() console.log('Data stored in service worker:', self.secret);
switch (data.command) {
case "account":
break;
default:
console.log("Unknown command:", data.command);
} }
}); });

5175
sw_scripts/safari-notifications.js

File diff suppressed because it is too large
Loading…
Cancel
Save