ChatMVC/Chat/wwwroot/js/notif.js
2025-01-25 10:41:37 -03:00

41 lines
1.8 KiB
JavaScript

navigator.serviceWorker.ready.then(reg => {
reg.pushManager.getSubscription().then(function (sub) {
// check if subscription is not exist and the process is for initial generation
if (!sub && !onlyRefreshToken) {
// ask for permission
Notification.requestPermission()
//generate subscription object
reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
}).then(function (subscribe) {
// send subscription to server to be saved
// parse string version of the json to get the expected object structure
sendSubscriptionFunction(JSON.parse(JSON.stringify(subscribe)))
}).catch(e => {
console.log(e)
})
}
// check if subscription is not exist but the permission already granted
else if (!sub && onlyRefreshToken && isPermissionGranted) {
//re-generate subscription object
reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
}).then(function (subscribe) {
// send subscription to server to be saved
// parse string version of the json to get the expected object structure
sendSubscriptionFunction(JSON.parse(JSON.stringify(subscribe)))
}).catch(e => {
console.log(e)
})
}
// check if subscription is already exist
else if (!!sub) {
// send subscription to server to be saved
// parse string version of the json to get the expected object structure
sendSubscriptionFunction(JSON.parse(JSON.stringify(sub)))
}
})
})