mirror of
https://github.com/ivabus/gui
synced 2025-04-23 14:07:14 +03:00
Display Push Notifications natively (#482)
* use native push notifications * prettier...
This commit is contained in:
parent
732cb56860
commit
8f0b9f4c83
2 changed files with 25 additions and 15 deletions
|
@ -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}`;
|
||||
|
|
|
@ -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}`;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue