Electron下Notification的坑

1.electron

观察发现崩溃场景和提示有关,issue上有遇到同样问题的用户:
App crashes on quit when calling ‘Notification#show’

崩溃场景:

1
2
3
4
5
6
7
8
9
app.on('ready', () => {
setTimeout(() => {
let myNotification = new Notification('Title', {
body: 'Lorem Ipsum Dolor Sit Amet'
});
myNotification.show();
myNotification.show();
}, 3000)
});

可以确定elcetron在遇到多个Notification同时触发时会有崩溃的bug.

2.拼多多客服平台代码

拼多多在第一次弹出消息时将window.openMsg置为1,并有逻辑如下,openMsg为1时不断触发弹出消息的代码。

1
2
3
window.openMsg ? void y(e) : void setTimeout(function() {
window.inBrowser || y(e)
}, 500)

弹出消息的相关代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Notification.requestPermission(function(n) {
var i = "有" + e + "位买家正在等待您的回复";
if ("granted" == n) {
if (window.notification && (notification.onclose = function() {}
,
notification.close()),
0 == e)
return;
window.notification = new Notification(i,{
dir: "auto",
lang: "hi",
tag: "newTag",
icon: "http://imsproduction.oss-cn-shanghai.aliyuncs.com/b22f71c705923308a8581cacfd136fc3.png",
body: "点击立即回复",
requireInteraction: "true"
}),
window.openMsg = !0,
notification.onclose = function() {
window.openMsg = !1
}
,
notification.onclick = function() {
parent.focus(),
this.close()
}
}
})

在多用户的场景下多个webview同时触发不断弹出Notification的逻辑,electron就挂了。

3.electron 对 Notification支持度

  • On Windows 10, notifications “just work”.
    (部分属性不支持,如requestPermission,electron不会请求是否通过的通知,会直接通过,just work)
  • On Windows 8.1 and Windows 8, a shortcut to your app, with a Application User Model ID, must be installed to the Start screen. Note, however, that it does not need to be pinned to the Start screen.
    (win8将快捷方式安装到Start screen的方式导致Note出现有问题)

  • On Windows 7, notifications are not supported. You can however send “balloon notifications” using the Tray API.
    (win7 不支持electron中使用Notification)

引用:
Using Notification API for Electron App