Skip to content
Open
22 changes: 20 additions & 2 deletions resources/electron/electron-plugin/dist/server/api/notification.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
import express from 'express';
import { Notification } from 'electron';
import { notifyLaravel } from "../utils.js";
import playSoundLib from 'play-sound';
import fs from 'fs';
const isLocalFile = (sound) => {
if (typeof sound !== 'string')
return false;
if (/^https?:\/\//i.test(sound))
return false;
return sound.includes('/') || sound.includes('\\');
};
const router = express.Router();
router.post('/', (req, res) => {
const { title, body, subtitle, silent, icon, hasReply, timeoutType, replyPlaceholder, sound, urgency, actions, closeButtonText, toastXml, event: customEvent, reference, } = req.body;
const eventName = customEvent !== null && customEvent !== void 0 ? customEvent : '\\Native\\Desktop\\Events\\Notifications\\NotificationClicked';
const notificationReference = reference !== null && reference !== void 0 ? reference : (Date.now() + '.' + Math.random().toString(36).slice(2, 9));
const usingLocalFile = isLocalFile(sound);
const notification = new Notification({
title,
body,
subtitle,
silent,
silent: usingLocalFile ? true : silent,
icon,
hasReply,
timeoutType,
replyPlaceholder,
sound,
sound: usingLocalFile ? undefined : sound,
urgency,
actions,
closeButtonText,
toastXml
});
if (usingLocalFile && !silent) {
fs.access(sound, fs.constants.F_OK, (err) => {
if (err) {
return;
}
playSoundLib().play(sound, () => { });
});
}
notification.on("click", (event) => {
notifyLaravel('events', {
event: eventName || '\\Native\\Desktop\\Events\\Notifications\\NotificationClicked',
Expand Down
26 changes: 24 additions & 2 deletions resources/electron/electron-plugin/src/server/api/notification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import express from 'express';
import { Notification } from 'electron';
import {notifyLaravel} from "../utils.js";
declare const require: any;
import playSoundLib from 'play-sound';
import fs from 'fs';

const isLocalFile = (sound: unknown) => {
if (typeof sound !== 'string') return false;
if (/^https?:\/\//i.test(sound)) return false;
// Treat any string containing path separators as a local file
return sound.includes('/') || sound.includes('\\');
};
const router = express.Router();

router.post('/', (req, res) => {
Expand All @@ -26,22 +36,34 @@ router.post('/', (req, res) => {

const notificationReference = reference ?? (Date.now() + '.' + Math.random().toString(36).slice(2, 9));

const usingLocalFile = isLocalFile(sound);

const notification = new Notification({
title,
body,
subtitle,
silent,
silent: usingLocalFile ? true : silent,
icon,
hasReply,
timeoutType,
replyPlaceholder,
sound,
sound: usingLocalFile ? undefined : sound,
urgency,
actions,
closeButtonText,
toastXml
});

if (usingLocalFile && !silent) {
fs.access(sound, fs.constants.F_OK, (err) => {
if (err) {
return;
}

playSoundLib().play(sound, () => {});
});
}

notification.on("click", (event) => {
notifyLaravel('events', {
event: eventName || '\\Native\\Desktop\\Events\\Notifications\\NotificationClicked',
Expand Down
31 changes: 31 additions & 0 deletions resources/electron/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions resources/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"get-port": "^7.1.0",
"kill-sync": "^1.0.3",
"nodemon": "^3.1.9",
"play-sound": "^1.1.3",
"ps-node": "^0.1.6",
"tree-kill": "^1.2.2",
"yauzl": "^3.2.0"
Expand Down
Loading