我正在使用node-notifier包进行通知,但我有一个问题。在notifier.on操作中心中未激发Windows单击事件
notifier.notify(
{
appId: "com.electron.demo", // Absolute path (doesn't work on balloons),
title: "Alert Message",
message: "Click to view",
icon: path.join(__dirname, 'msicon.png'),
wait: true,
timeout: false,
id: "demo app"
},
function(err, response) {
// Response is response from notification
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
notifier.on("click", function(notifierObject, options, event) {
// Triggers if `wait: true` and user clicks notification
var request = new sql.Request();
let username = userName;
request.query("update alerts.dbo.groupmessages set isRead = 1\
where pk = '"+ recordset.recordset[i].pk + "'\
and userFK = (select pk from alerts.dbo.users where username = '"+ username + "')\
and groupFK = (select groupFK from alerts.dbo.users where username = '"+ username + "')", function (err, recordset) {
if (err) throw err
console.log(err, recordset, 'User shit');
});
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 400,
height: 400,
frame: false,
webPreferences: {
nodeIntegration: true
}
});
// and load the index.html of the app.
ejse.data('message', recordset.recordset[i].message);
win.loadFile('index.ejs')
}
createWindow();
});
});
});notifier.on单击事件并不只在Windows Action Center中触发。请告诉我原因和解决方案。谢谢。
发布于 2020-06-07 05:15:02
Action Center需要在本机代码中单独实现,这是node-notifier所没有的。
您可以尝试使用node-powertoast,并使用onActivated回调:npm i node-powertoast
const toast = require('powertoast');
toast({
title: "Hello",
message: "world",
callback: {
timeout: 5000, //keep-a-live in ms
onActivated: ()=>{ console.log("activated") },
onDismissed: (reason)=>{ console.log(reason) }
})
.then(()=> console.log("Notified")
.catch(err => console.error(err));至于原因:
你可以看到,对于其他项目来说,这是一项重要的任务,例如,下面的项目似乎让行动中心的支持停滞了两年:试图自己实现它的https://github.com/mohabouje/WinToast/issues/35人员似乎被卡住了,并且很难正确地实现它。
这是很难做到的。
发布于 2022-01-29 12:45:47
通知没有获得点击,因为并非所有操作中心的要求都得到了满足。
一般来说,Windows通知有三种类型的激活:foreground、background和protocol。
在任何情况下,您都需要具备以下各项:
AppUserModelId属性的快捷方式。请咨询您的安装程序如何做到这一点,或使用shell.readShortcutLink和shell.writeShortcutLink电子API。具体地说,WiX允许在选项中指定appUserModelId和toastActivatorClsid。AppUserModelId。app.setAppUserModelId('AppUserModelId');
app.setAsDefaultProtocolClient('example');
要使用foreground或background,您需要:
ToastActivatorCLSID属性的快捷方式。有关如何操作的信息,请参阅上面的内容。ToastActivatorCLSID和您选择的自定义协议注册COM服务器,并调用:registerActivator();;registerComServer()
我不会深入讨论如何响应自定义协议的细节,但在搜索example:时,您需要解析命令行参数,如果应用程序已经启动,还需要在second-instance钩子中解析它。
要使用protocol类型,不需要额外的操作。
在那之后,即使是标准的电子通知也会从行动中心获得点击。
但是如何使用这些协议并获得功能丰富的通知呢?电子允许你直接指定toastXml!
假设这是您的通知。即使从操作中心单击,它也会打开https://google.com。
const notification = new Notification({
toastXml: `
<toast launch="https://google.com" activationType="protocol">
<visual>
<binding template="ToastGeneric">
<text>Wonderman meets Superwoman</text>
<text>In the eve of the new millennium, Wonderman challenges Superwoman to a bliniking contest.</text>
<text placement="attribution">Mars Press</text>
</binding>
</visual>
</toast>
`,
});
notification.show();这条通知只是打开了Google。你可以在Notification Visualizer App上获得更多的想法
https://stackoverflow.com/questions/61207372
复制相似问题