30 lines
868 B
JavaScript
30 lines
868 B
JavaScript
|
|
const registerServiceWorker = async () => {
|
||
|
|
const swRegistration = await navigator.serviceWorker.register("/sw.js");
|
||
|
|
return swRegistration;
|
||
|
|
};
|
||
|
|
|
||
|
|
const requestNotificationPermission = async () => {
|
||
|
|
// return value: 'granted', 'default', 'denied'
|
||
|
|
return await window.Notification.requestPermission();
|
||
|
|
};
|
||
|
|
|
||
|
|
export async function requestNotificationAndServiceWorker() {
|
||
|
|
if (!("serviceWorker" in navigator)) {
|
||
|
|
throw new Error("No Service Worker support!");
|
||
|
|
}
|
||
|
|
if (!("PushManager" in window)) {
|
||
|
|
throw new Error("No Push API Support!");
|
||
|
|
}
|
||
|
|
const permission = await requestNotificationPermission();
|
||
|
|
if(permission==='granted') await registerServiceWorker();
|
||
|
|
return permission
|
||
|
|
}
|
||
|
|
|
||
|
|
export function windowNotificationPermission() {
|
||
|
|
return window.Notification.permission
|
||
|
|
}
|
||
|
|
|
||
|
|
export function notificationCount() {
|
||
|
|
return this.$matrix.notificationCount
|
||
|
|
}
|