Add fcm push notifications
This commit is contained in:
+23
-24
@@ -1,24 +1,12 @@
|
||||
import * as nip19 from 'nostr-tools/nip19'
|
||||
import {parse, renderAsText} from '@welshman/content'
|
||||
import {MESSAGE, THREAD, COMMENT, EVENT_TIME} from '@welshman/util'
|
||||
/* global clients */
|
||||
|
||||
const renderOptions = {
|
||||
createElement: tag => ({
|
||||
_text: "",
|
||||
set innerText(text) {
|
||||
this._text = text
|
||||
},
|
||||
get innerHTML() {
|
||||
return this._text
|
||||
},
|
||||
})
|
||||
}
|
||||
import * as nip19 from "nostr-tools/nip19"
|
||||
|
||||
self.addEventListener('install', event => {
|
||||
self.addEventListener("install", event => {
|
||||
self.skipWaiting()
|
||||
})
|
||||
|
||||
self.addEventListener('activate', event => {
|
||||
self.addEventListener("activate", event => {
|
||||
event.waitUntil(self.clients.claim())
|
||||
})
|
||||
|
||||
@@ -26,26 +14,37 @@ self.addEventListener("push", e => {
|
||||
console.log("Service Worker: Push event received", e)
|
||||
|
||||
let url = "/"
|
||||
let title = "New activity"
|
||||
let body = "You have a new message"
|
||||
|
||||
try {
|
||||
const {event, relays = []} = e.data?.json() || {}
|
||||
const data = e.data?.json()
|
||||
|
||||
if (event) {
|
||||
url += nip19.neventEncode({id: event.id, relays})
|
||||
body = renderAsText(parse(event), renderOptions).toString()
|
||||
if (data?.event) {
|
||||
url += nip19.neventEncode({
|
||||
id: data.event.id,
|
||||
relays: data.relays || []
|
||||
})
|
||||
}
|
||||
|
||||
if (data?.title) {
|
||||
title = data.title
|
||||
}
|
||||
|
||||
if (data?.body) {
|
||||
body = data.body
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("Service Worker: Failed to parse push data", e)
|
||||
}
|
||||
|
||||
e.waitUntil(
|
||||
self.registration.showNotification("New message", {
|
||||
self.registration.showNotification(title, {
|
||||
body,
|
||||
data: {url},
|
||||
icon: "/pwa-192x192.png",
|
||||
badge: "/pwa-64x64.png",
|
||||
tag: 'flotilla-notification',
|
||||
tag: "flotilla-notification",
|
||||
requireInteraction: false,
|
||||
}),
|
||||
)
|
||||
@@ -74,8 +73,8 @@ self.addEventListener("notificationclick", e => {
|
||||
for (const client of clientList) {
|
||||
if (client.url.includes(location.origin)) {
|
||||
client.postMessage({
|
||||
type: 'NAVIGATE',
|
||||
url: url
|
||||
type: "NAVIGATE",
|
||||
url: url,
|
||||
})
|
||||
|
||||
return client.focus()
|
||||
|
||||
Reference in New Issue
Block a user