Implemented pushsubscriptionchange
This commit is contained in:
parent
01dfb96363
commit
48a792a100
2
main.go
2
main.go
@ -28,6 +28,7 @@ import (
|
|||||||
"github.com/animenotifier/notify.moe/pages/listimport/listimportkitsu"
|
"github.com/animenotifier/notify.moe/pages/listimport/listimportkitsu"
|
||||||
"github.com/animenotifier/notify.moe/pages/listimport/listimportmyanimelist"
|
"github.com/animenotifier/notify.moe/pages/listimport/listimportmyanimelist"
|
||||||
"github.com/animenotifier/notify.moe/pages/login"
|
"github.com/animenotifier/notify.moe/pages/login"
|
||||||
|
"github.com/animenotifier/notify.moe/pages/me"
|
||||||
"github.com/animenotifier/notify.moe/pages/music"
|
"github.com/animenotifier/notify.moe/pages/music"
|
||||||
"github.com/animenotifier/notify.moe/pages/newsoundtrack"
|
"github.com/animenotifier/notify.moe/pages/newsoundtrack"
|
||||||
"github.com/animenotifier/notify.moe/pages/newthread"
|
"github.com/animenotifier/notify.moe/pages/newthread"
|
||||||
@ -129,6 +130,7 @@ func configure(app *aero.Application) *aero.Application {
|
|||||||
app.Ajax("/extension/embed", embed.Get)
|
app.Ajax("/extension/embed", embed.Get)
|
||||||
|
|
||||||
// API
|
// API
|
||||||
|
app.Get("/api/me", me.Get)
|
||||||
app.Get("/api/test/notification", notifications.Test)
|
app.Get("/api/test/notification", notifications.Test)
|
||||||
|
|
||||||
// PayPal
|
// PayPal
|
||||||
|
17
pages/me/me.go
Normal file
17
pages/me/me.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package me
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/animenotifier/notify.moe/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get ...
|
||||||
|
func Get(ctx *aero.Context) string {
|
||||||
|
user := utils.GetUser(ctx)
|
||||||
|
|
||||||
|
if user == nil {
|
||||||
|
return ctx.JSON(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(user)
|
||||||
|
}
|
@ -157,7 +157,38 @@ self.addEventListener("push", (evt: PushEvent) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener("pushsubscriptionchange", (evt: any) => {
|
self.addEventListener("pushsubscriptionchange", (evt: any) => {
|
||||||
console.log("pushsubscriptionchange", evt)
|
evt.waitUntil((self as any).registration.pushManager.subscribe(evt.oldSubscription.options)
|
||||||
|
.then(async subscription => {
|
||||||
|
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,
|
||||||
|
userAgent: navigator.userAgent,
|
||||||
|
screen: {
|
||||||
|
width: window.screen.width,
|
||||||
|
height: window.screen.height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let user = await fetch("/api/me").then(response => response.json())
|
||||||
|
|
||||||
|
return fetch("/api/pushsubscriptions/" + user.id + "/add", {
|
||||||
|
method: "POST",
|
||||||
|
credentials: "same-origin",
|
||||||
|
body: JSON.stringify(pushSubscription)
|
||||||
|
})
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener("notificationclick", (evt: NotificationEvent) => {
|
self.addEventListener("notificationclick", (evt: NotificationEvent) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user