111 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)
}
let registration = await navigator.serviceWorker.ready
let subscription = await registration.pushManager.getSubscription()
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
}
let registration = await navigator.serviceWorker.ready
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
}
let registration = await navigator.serviceWorker.ready
let subscription = await registration.pushManager.getSubscription()
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...")
let rawKey = subscription.getKey("p256dh")
let key = rawKey ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : ""
let rawSecret = subscription.getKey("auth")
let secret = rawSecret ? btoa(String.fromCharCode.apply(null, new Uint8Array(rawSecret))) : ""
let endpoint = subscription.endpoint
let pushSubscription = {
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
}
}
return fetch("/api/pushsubscriptions/" + userId + "/add", {
method: "POST",
credentials: "same-origin",
body: JSON.stringify(pushSubscription)
})
}
unsubscribeOnServer(subscription: PushSubscription, userId: string) {
console.log("Send unsubscription to server...")
console.log(subscription)
let pushSubscription = {
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)))
2017-07-14 21:50:34 +00:00
}