112 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-04-02 05:34:16 +00:00
export default class PushManager {
2017-07-14 21:50:34 +00:00
pushSupported: boolean
2018-04-02 05:34:16 +00:00
2017-07-14 21:50:34 +00:00
constructor() {
this.pushSupported = ("serviceWorker" in navigator) && ("PushManager" in window)
}
2019-04-19 13:12:33 +00:00
async subscription() {
2017-07-14 23:32:06 +00:00
if(!this.pushSupported) {
return Promise.resolve(null)
}
2019-11-17 09:25:14 +00:00
const registration = await navigator.serviceWorker.ready
const subscription = await registration.pushManager.getSubscription()
2017-07-14 23:32:06 +00:00
2019-04-19 13:12:33 +00:00
return Promise.resolve(subscription)
2017-07-14 23:32:06 +00:00
}
2017-07-14 21:50:34 +00:00
async subscribe(userId: string) {
if(!this.pushSupported) {
return
}
2019-11-17 09:25:14 +00:00
const registration = await navigator.serviceWorker.ready
2017-07-14 21:50:34 +00:00
let subscription = await registration.pushManager.getSubscription()
if(!subscription) {
subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array("BAwPKVCWQ2_nc7SIGltYfWZhMpW54BSkbwelpa8eYMbqSitmCAGm3xRBdRiq1Wt-hUsE7x59GCcaJxqQtF2hZPw")
})
this.subscribeOnServer(subscription, userId)
} else {
console.log("Using existing subscription", subscription)
}
}
async unsubscribe(userId: string) {
if(!this.pushSupported) {
return
}
2019-11-17 09:25:14 +00:00
const registration = await navigator.serviceWorker.ready
const subscription = await registration.pushManager.getSubscription()
2017-07-14 21:50:34 +00:00
if(!subscription) {
console.error("Subscription does not exist")
return
}
2018-04-02 05:34:16 +00:00
2017-07-14 21:50:34 +00:00
await subscription.unsubscribe()
this.unsubscribeOnServer(subscription, userId)
}
subscribeOnServer(subscription: PushSubscription, userId: string) {
console.log("Send subscription to server...")
2019-11-17 09:25:14 +00:00
const rawKey = subscription.getKey("p256dh")
const key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : ""
2017-07-14 21:50:34 +00:00
2019-11-17 09:25:14 +00:00
const rawSecret = subscription.getKey("auth")
const secret = rawSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawSecret))) : ""
2017-07-14 21:50:34 +00:00
2019-11-17 09:25:14 +00:00
const endpoint = subscription.endpoint
2017-07-14 21:50:34 +00:00
2019-11-17 09:25:14 +00:00
const pushSubscription = {
2017-07-14 21:50:34 +00:00
endpoint,
p256dh: key,
auth: secret,
platform: navigator.platform,
2017-07-14 23:32:06 +00:00
userAgent: navigator.userAgent,
2017-07-14 21:50:34 +00:00
screen: {
width: window.screen.width,
height: window.screen.height
}
}
2019-11-20 07:47:00 +00:00
return fetch(`/api/pushsubscriptions/${userId}/add`, {
2017-07-14 21:50:34 +00:00
method: "POST",
credentials: "same-origin",
body: JSON.stringify(pushSubscription)
})
}
unsubscribeOnServer(subscription: PushSubscription, userId: string) {
console.log("Send unsubscription to server...")
console.log(subscription)
2019-11-17 09:25:14 +00:00
const pushSubscription = {
2017-07-14 21:50:34 +00:00
endpoint: subscription.endpoint
}
return fetch("/api/pushsubscriptions/" + userId + "/remove", {
method: "POST",
credentials: "same-origin",
body: JSON.stringify(pushSubscription)
})
}
}
2019-05-26 02:56:41 +00:00
function urlBase64ToUint8Array(base64String: string) {
2017-07-14 21:50:34 +00:00
const padding = "=".repeat((4 - base64String.length % 4) % 4)
const base64 = (base64String + padding)
.replace(/\-/g, "+")
.replace(/_/g, "/")
const rawData = window.atob(base64)
2019-05-26 02:56:41 +00:00
return Uint8Array.from([...rawData].map(char => char.charCodeAt(0)))
2019-11-17 09:44:30 +00:00
}