Implemented pushsubscriptionchange

This commit is contained in:
Eduard Urbach 2017-07-19 03:22:50 +02:00
parent 01dfb96363
commit 48a792a100
3 changed files with 51 additions and 1 deletions

View File

@ -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
View 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)
}

View File

@ -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) => {