mirror of
https://github.com/aljazceru/meshcore-web.git
synced 2025-12-18 00:24:21 +01:00
show notification when a new message is received
This commit is contained in:
@@ -2,6 +2,7 @@ import GlobalState from "./GlobalState.js";
|
|||||||
import {BleConnection, Constants, SerialConnection} from "@liamcottle/meshcore.js";
|
import {BleConnection, Constants, SerialConnection} from "@liamcottle/meshcore.js";
|
||||||
import Database from "./Database.js";
|
import Database from "./Database.js";
|
||||||
import Utils from "./Utils.js";
|
import Utils from "./Utils.js";
|
||||||
|
import NotificationUtils from "./NotificationUtils.js";
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
|
|
||||||
@@ -232,6 +233,7 @@ class Connection {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// save message to database
|
||||||
await Database.Message.insert({
|
await Database.Message.insert({
|
||||||
status: "received",
|
status: "received",
|
||||||
to: GlobalState.selfInfo.publicKey,
|
to: GlobalState.selfInfo.publicKey,
|
||||||
@@ -245,6 +247,9 @@ class Connection {
|
|||||||
error: null,
|
error: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// show notification
|
||||||
|
await NotificationUtils.showNotification(contact.advName, message.text);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/js/NotificationUtils.js
Normal file
22
src/js/NotificationUtils.js
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
class NotificationUtils {
|
||||||
|
|
||||||
|
static async showNotification(title, body) {
|
||||||
|
|
||||||
|
// request notification permission
|
||||||
|
const result = await Notification.requestPermission();
|
||||||
|
if(result !== "granted"){
|
||||||
|
console.log(result);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// show notification
|
||||||
|
new window.Notification(title, {
|
||||||
|
body: body,
|
||||||
|
icon: "/icon.png",
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default NotificationUtils;
|
||||||
Reference in New Issue
Block a user