Trent Larson
11 months ago
5 changed files with 88 additions and 68 deletions
@ -1,5 +1,69 @@ |
|||||
// many of these are also found in endorser-mobile utility.ts
|
// many of these are also found in endorser-mobile utility.ts
|
||||
|
|
||||
|
import axios, { AxiosResponse } from "axios"; |
||||
|
|
||||
|
import { DEFAULT_PUSH_SERVER } from "@/constants/app"; |
||||
|
import { db } from "@/db/index"; |
||||
|
import { MASTER_SETTINGS_KEY } from "@/db/tables/settings"; |
||||
|
|
||||
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
|
const Buffer = require("buffer/").Buffer; |
||||
|
|
||||
export const isGlobalUri = (uri: string) => { |
export const isGlobalUri = (uri: string) => { |
||||
return uri && uri.match(new RegExp(/^[A-Za-z][A-Za-z0-9+.-]+:/)); |
return uri && uri.match(new RegExp(/^[A-Za-z][A-Za-z0-9+.-]+:/)); |
||||
}; |
}; |
||||
|
|
||||
|
export const sendTestThroughPushServer = async ( |
||||
|
subscription: PushSubscription, |
||||
|
skipFilter: boolean, |
||||
|
): Promise<AxiosResponse> => { |
||||
|
await db.open(); |
||||
|
const settings = await db.settings.get(MASTER_SETTINGS_KEY); |
||||
|
let pushUrl: string = DEFAULT_PUSH_SERVER as string; |
||||
|
if (settings?.webPushServer) { |
||||
|
pushUrl = settings.webPushServer; |
||||
|
} |
||||
|
|
||||
|
// This is a special value that tells the service worker to send a direct notification to the device, skipping filters.
|
||||
|
// This is shared with the service worker and should be a constant. Look for the same name in additional-scripts.js
|
||||
|
// Use something other than "Daily Update" https://gitea.anomalistdesign.com/trent_larson/py-push-server/src/commit/3c0e196c11bc98060ec5934e99e7dbd591b5da4d/app.py#L213
|
||||
|
const DIRECT_PUSH_TITLE = "DIRECT_NOTIFICATION"; |
||||
|
|
||||
|
const auth = Buffer.from(subscription.getKey("auth")); |
||||
|
const authB64 = auth |
||||
|
.toString("base64") |
||||
|
.replace(/\+/g, "-") |
||||
|
.replace(/\//g, "_") |
||||
|
.replace(/=+$/, ""); |
||||
|
const p256dh = Buffer.from(subscription.getKey("p256dh")); |
||||
|
const p256dhB64 = p256dh |
||||
|
.toString("base64") |
||||
|
.replace(/\+/g, "-") |
||||
|
.replace(/\//g, "_") |
||||
|
.replace(/=+$/, ""); |
||||
|
const newPayload = { |
||||
|
endpoint: subscription.endpoint, |
||||
|
keys: { |
||||
|
auth: authB64, |
||||
|
p256dh: p256dhB64, |
||||
|
}, |
||||
|
message: `Test, where you will see this message ${ |
||||
|
skipFilter ? "un" : "" |
||||
|
}filtered.`,
|
||||
|
title: skipFilter ? DIRECT_PUSH_TITLE : "Your Web Push", |
||||
|
}; |
||||
|
console.log("Sending a test web push message:", newPayload); |
||||
|
const payloadStr = JSON.stringify(newPayload); |
||||
|
const response = await axios.post( |
||||
|
pushUrl + "/web-push/send-test", |
||||
|
payloadStr, |
||||
|
{ |
||||
|
headers: { |
||||
|
"Content-Type": "application/json", |
||||
|
}, |
||||
|
}, |
||||
|
); |
||||
|
|
||||
|
console.log("Got response from web push server:", response); |
||||
|
return response; |
||||
|
}; |
||||
|
Loading…
Reference in new issue