Display Push Notifications natively (#482)

* use native push notifications

* prettier...
This commit is contained in:
ABevier 2023-04-21 00:07:38 -04:00 committed by GitHub
parent 732cb56860
commit 8f0b9f4c83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 15 deletions

View file

@ -2,7 +2,7 @@ import Pushy from "pushy-electron";
import { readSessionData } from "./auth";
import { post } from "./v1-client";
import * as log from "electron-log";
import { BrowserWindow } from "electron";
import { Notification, BrowserWindow } from "electron";
import { nameToSlug } from "./package";
import {
getInstalledPackages,
@ -24,6 +24,7 @@ export default function initialize(mainWindow: BrowserWindow) {
Pushy.register({ appId: "643647948f3b62fb34b29989" })
.then(async (push_token) => {
const { device_id } = await readSessionData();
log.info(`Registering device ${device_id} for push notifications with token: ${push_token}`);
if (device_id) await post(`/auth/device/${device_id}/register-push-token`, { push_token });
})
.catch((err) => {
@ -34,20 +35,27 @@ export default function initialize(mainWindow: BrowserWindow) {
// Listen for incoming notifications
Pushy.setNotificationListener(async (data: any) => {
// Display an alert with the "message" payload value
const isDup = await wasReceivedBefore(data);
try {
log.info("new notification received", data);
if (!isDup) {
Pushy.alert(mainWindow, data?.message as string);
log.info("new notification received", data.url, data.version);
const v = app.dock.getBadge();
if (!v) {
app.dock.setBadge("1");
const isDup = await wasReceivedBefore(data);
if (!isDup) {
new Notification({
title: "tea",
body: data?.message as string
}).show();
const v = app.dock.getBadge();
if (!v) {
app.dock.setBadge("1");
} else {
app.dock.setBadge((parseInt(v) + 1).toString());
}
} else {
app.dock.setBadge((parseInt(v) + 1).toString());
log.info("notification was already received before", data);
}
} else {
log.info("notification was already received before", data);
} catch (error) {
log.error("notification listener", error);
}
});
}
@ -139,6 +147,8 @@ async function wasReceivedBefore({
url: string;
version: string;
}): Promise<boolean> {
if (!url || !version) return false;
let received = false;
const pkg = url.replace("tea://packages/", "");
const searchString = `${pkg}:::${version}`;

View file

@ -24,9 +24,9 @@
divisor = 1024;
}
const N = n / divisor;
// going for 3 significant figures
const decimals = N < 10 ? 2 : N < 100 ? 1 : 0;
const N = n / divisor;
// going for 3 significant figures
const decimals = N < 10 ? 2 : N < 100 ? 1 : 0;
return `${N.toFixed(decimals)} ${unit}`;
};