fix(desktop): direct SnoreToast -w to capture reply + WindowsToaster fallback
Root cause: node-notifier's toaster whitelist has no -w, so SnoreToast never waits and the reply is lost. Now drive SnoreToast directly with -w + our own pipe (correct args, no -application which had broken the toast). If the binary is missing or fails to show, fall back to node-notifier's WindowsToaster so a toast always appears. Logs code+raw for diagnosis. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+79
-39
@@ -35,20 +35,17 @@ ipcMain.on('get-install-info', (e) => {
|
|||||||
// on the next restart. No-op in dev (unpackaged).
|
// on the next restart. No-op in dev (unpackaged).
|
||||||
let autoUpdater = null;
|
let autoUpdater = null;
|
||||||
try { ({ autoUpdater } = require('electron-updater')); } catch (_) { /* not installed in dev */ }
|
try { ({ autoUpdater } = require('electron-updater')); } catch (_) { /* not installed in dev */ }
|
||||||
// Native Windows toast WITH a reply box, driven by SnoreToast directly (the binary node-notifier
|
// Native Windows toast WITH a reply box. node-notifier's WindowsToaster reliably SHOWS the
|
||||||
// bundles). We run it against our OWN named pipe so we fully control reading the typed reply.
|
// toast (avatar + reply box) but its option whitelist has no `-w`, so SnoreToast never waits
|
||||||
// Installed-app only (needs the AppUserModelID shortcut the NSIS installer registers). No pwsh.
|
// and the typed reply is lost. So we drive the bundled SnoreToast binary DIRECTLY with `-w` +
|
||||||
|
// our own named pipe to capture the reply — and fall back to WindowsToaster (display only) if
|
||||||
|
// the binary can't be found/spawned, so a toast always appears. Installed-app only (AppUserModelID).
|
||||||
const { spawn } = require('child_process');
|
const { spawn } = require('child_process');
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
|
let WindowsToaster = null;
|
||||||
function snoreToastPath() {
|
try { WindowsToaster = require('node-notifier').WindowsToaster; } catch (_) { /* optional */ }
|
||||||
try {
|
const APP_ID = 'com.bizgaze.connect.desktop';
|
||||||
const dir = path.dirname(require.resolve('node-notifier')); // .../node-notifier
|
const dbg = (o) => { try { fs.appendFileSync(path.join(app.getPath('userData'), 'toast-debug.log'), JSON.stringify(Object.assign({ t: Date.now() }, o)) + '\n'); } catch (_) {} };
|
||||||
const exe = os.arch() === 'ia32' ? 'snoretoast-x86.exe' : 'snoretoast-x64.exe';
|
|
||||||
const p = path.join(dir, 'vendor', 'snoreToast', exe);
|
|
||||||
return p.replace('app.asar' + path.sep, 'app.asar.unpacked' + path.sep); // execute from the unpacked copy
|
|
||||||
} catch (_) { return null; }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the avatar the renderer drew (a data: URL) to a temp PNG for SnoreToast's -p image.
|
// Write the avatar the renderer drew (a data: URL) to a temp PNG for SnoreToast's -p image.
|
||||||
function writeTempPng(dataUrl) {
|
function writeTempPng(dataUrl) {
|
||||||
@@ -59,40 +56,83 @@ function writeTempPng(dataUrl) {
|
|||||||
return p;
|
return p;
|
||||||
} catch (_) { return null; }
|
} catch (_) { return null; }
|
||||||
}
|
}
|
||||||
|
// Locate the SnoreToast binary node-notifier bundles (handles asar.unpacked in a packaged app).
|
||||||
// Resolves with {text} (replied), {open} (toast clicked), or null (dismissed/timeout).
|
function snoreExe() {
|
||||||
ipcMain.handle('reply-notification', (_e, payload = {}) => new Promise((resolve) => {
|
try {
|
||||||
const exe = snoreToastPath();
|
const dir = path.dirname(require.resolve('node-notifier'));
|
||||||
if (!exe || !fs.existsSync(exe)) return resolve(null);
|
const exe = os.arch() === 'ia32' ? 'snoretoast-x86.exe' : 'snoretoast-x64.exe';
|
||||||
const img = writeTempPng(payload.avatar);
|
const base = path.join(dir, 'vendor', 'snoreToast', exe);
|
||||||
const pipeName = '\\\\.\\pipe\\bizc-toast-' + crypto.randomBytes(6).toString('hex');
|
for (const c of [base, base.replace('app.asar' + path.sep, 'app.asar.unpacked' + path.sep)]) {
|
||||||
let raw = Buffer.alloc(0), done = false, server = null;
|
if (fs.existsSync(c)) return c;
|
||||||
const finish = (v) => { if (!done) { done = true; try { server && server.close(); } catch (_) {} if (img) { try { fs.unlinkSync(img); } catch (_) {} } resolve(v); } };
|
}
|
||||||
const parseAndFinish = (code) => {
|
} catch (_) {}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Direct SnoreToast with -w so it waits and writes the reply to our pipe. Rejects if the binary
|
||||||
|
// is missing or fails to show a toast (so the caller can fall back). Args match node-notifier's
|
||||||
|
// known-good set + -w (no -application, which broke the toast).
|
||||||
|
function directToast(payload, img) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const exe = snoreExe();
|
||||||
|
if (!exe) return reject(new Error('snoretoast not found'));
|
||||||
|
const pipe = '\\\\.\\pipe\\bizc-' + crypto.randomBytes(6).toString('hex');
|
||||||
|
let raw = Buffer.alloc(0), settled = false, server = null;
|
||||||
|
const settle = (fn, v) => { if (!settled) { settled = true; try { server && server.close(); } catch (_) {} fn(v); } };
|
||||||
|
server = net.createServer((sock) => { sock.on('data', (d) => { raw = Buffer.concat([raw, d]); }); });
|
||||||
|
server.on('error', (e) => settle(reject, e));
|
||||||
|
server.listen(pipe, () => {
|
||||||
|
const args = ['-appID', APP_ID, '-tb', '-w', '-pipeName', pipe];
|
||||||
|
if (img) args.push('-p', img);
|
||||||
|
args.push('-m', payload.body || ' ', '-t', payload.title || 'Biz Connect');
|
||||||
|
const child = spawn(exe, args, { windowsHide: true });
|
||||||
|
child.on('error', (e) => settle(reject, e));
|
||||||
|
child.on('exit', (code) => {
|
||||||
const s = raw.toString('utf16le').replace(/[\u0000\r\n]+/g, '').trim();
|
const s = raw.toString('utf16le').replace(/[\u0000\r\n]+/g, '').trim();
|
||||||
try { fs.appendFileSync(path.join(app.getPath('userData'), 'toast-debug.log'), JSON.stringify({ t: Date.now(), code, raw: s }) + '\n'); } catch (_) {}
|
dbg({ code, raw: s });
|
||||||
const r = {};
|
const r = {};
|
||||||
s.split(';').forEach((kv) => { const i = kv.indexOf('='); if (i > 0) r[kv.slice(0, i).trim().toLowerCase()] = kv.slice(i + 1); });
|
s.split(';').forEach((kv) => { const i = kv.indexOf('='); if (i > 0) r[kv.slice(0, i).trim().toLowerCase()] = kv.slice(i + 1); });
|
||||||
let text = String(r.text || r.value || r.userinput || r.textbox || r.reply || '').trim();
|
let text = String(r.text || r.value || r.userinput || r.textbox || r.reply || '').trim();
|
||||||
// SnoreToast exit 5 = TextEntered; if the reply isn't in a key=value field, take the raw value.
|
|
||||||
if (!text && code === 5 && s) { const parts = s.split('='); text = (parts.length > 1 ? parts.slice(1).join('=') : s).trim(); }
|
if (!text && code === 5 && s) { const parts = s.split('='); text = (parts.length > 1 ? parts.slice(1).join('=') : s).trim(); }
|
||||||
if (text) return finish({ kind: payload.kind, id: payload.id, text });
|
if (text) return settle(resolve, { kind: payload.kind, id: payload.id, text });
|
||||||
|
// 0=activated, 1=hidden, 2=dismissed, 3=timedout, 4=button, 5=text. Anything else = failed → fall back.
|
||||||
|
if (![0, 1, 2, 3, 4, 5].includes(code)) return settle(reject, new Error('snoretoast exit ' + code));
|
||||||
const act = String(r.action || '').toLowerCase();
|
const act = String(r.action || '').toLowerCase();
|
||||||
if (code === 0 || act.includes('activat') || act.includes('click')) return finish({ kind: payload.kind, id: payload.id, open: true });
|
if (code === 0 || act.includes('activat') || act.includes('click')) return settle(resolve, { kind: payload.kind, id: payload.id, open: true });
|
||||||
finish(null);
|
settle(resolve, null);
|
||||||
};
|
});
|
||||||
try {
|
});
|
||||||
server = net.createServer((sock) => { sock.on('data', (d) => { raw = Buffer.concat([raw, d]); }); });
|
});
|
||||||
server.on('error', () => finish(null));
|
}
|
||||||
server.listen(pipeName, () => {
|
// Fallback: node-notifier shows the toast (avatar + reply box) but can't capture the reply.
|
||||||
const args = ['-t', payload.title || 'Biz Connect', '-m', payload.body || ' ', '-appID', 'com.bizgaze.connect.desktop', '-pipeName', pipeName, '-application', process.execPath, '-tb', '-w'];
|
function fallbackToast(payload, img) {
|
||||||
if (img) args.push('-p', img);
|
return new Promise((resolve) => {
|
||||||
const child = spawn(exe, args, { windowsHide: true });
|
if (!WindowsToaster) return resolve(null);
|
||||||
child.on('error', () => finish(null));
|
try {
|
||||||
child.on('exit', (code) => setTimeout(() => parseAndFinish(code == null ? -1 : code), 250));
|
new WindowsToaster({ withFallback: false }).notify(
|
||||||
|
Object.assign({ title: payload.title || 'Biz Connect', message: payload.body || ' ', appID: APP_ID, tb: true }, img ? { icon: img } : {}),
|
||||||
|
(err, response, metadata) => {
|
||||||
|
const act = String(response || (metadata && metadata.action) || '').toLowerCase();
|
||||||
|
resolve(act.includes('activat') || act === 'click' ? { kind: payload.kind, id: payload.id, open: true } : null);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (_) { resolve(null); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Resolves with {text} (replied), {open} (toast clicked), or null (dismissed/timeout).
|
||||||
|
ipcMain.handle('reply-notification', async (_e, payload = {}) => {
|
||||||
|
const img = writeTempPng(payload.avatar);
|
||||||
|
const cleanup = () => { if (img) { try { fs.unlinkSync(img); } catch (_) {} } };
|
||||||
|
try {
|
||||||
|
const r = await directToast(payload, img);
|
||||||
|
cleanup();
|
||||||
|
return r;
|
||||||
|
} catch (e) {
|
||||||
|
dbg({ fallback: String(e && e.message) });
|
||||||
|
const r = await fallbackToast(payload, img);
|
||||||
|
cleanup();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (_) { finish(null); }
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Windows attributes notifications to the AppUserModelID. Without setting it, toasts read
|
// Windows attributes notifications to the AppUserModelID. Without setting it, toasts read
|
||||||
// "electron.app.<name>"; setting it to the installer's appId makes Windows resolve the
|
// "electron.app.<name>"; setting it to the installer's appId makes Windows resolve the
|
||||||
|
|||||||
Reference in New Issue
Block a user