admin管理员组文章数量:1026088
I use notification API to show popup window on Chrome 73:
new Notification('', {
icon: "images/transparent.png",
image: res,
requireInteraction: true
});
notification.onclose = function () {
alert('close')
};
notification.onclick= function () {
alert('click')
};
notification.onerror= function () {
alert('error');
};
notification.onnotificationclose = function () {
alert("close")
};
I see this popup:
But the problem is that if user clicks icon with arrow, then onclose
is fired, but if user clicks 'Close' aka 'Закрыть' button, no handler is invoked.
How can I handle it? Is it a bug in chrome?
I use notification API to show popup window on Chrome 73:
new Notification('', {
icon: "images/transparent.png",
image: res,
requireInteraction: true
});
notification.onclose = function () {
alert('close')
};
notification.onclick= function () {
alert('click')
};
notification.onerror= function () {
alert('error');
};
notification.onnotificationclose = function () {
alert("close")
};
I see this popup:
But the problem is that if user clicks icon with arrow, then onclose
is fired, but if user clicks 'Close' aka 'Закрыть' button, no handler is invoked.
How can I handle it? Is it a bug in chrome?
Share Improve this question edited Apr 8, 2019 at 13:18 user1773603 asked Apr 2, 2019 at 23:53 Stepan YakovenkoStepan Yakovenko 9,26431 gold badges124 silver badges212 bronze badges 6-
Can you put a little more info? Like what version of chrome are you using and what are your Notification actions? Are you using a global service worker ? If so, did you try the
'notificationonclose'
event? – Daniel Piñeiro Commented Apr 5, 2019 at 10:37 - onnotificationclose doesn't help – Stepan Yakovenko Commented Apr 5, 2019 at 11:57
-
Does
notification.addEventListener('close', function () => { ... });
solve the issue? – pschild Commented Apr 8, 2019 at 13:29 - no it doesn't, you can test yourself – Stepan Yakovenko Commented Apr 8, 2019 at 14:10
- I seem this notification like a native notification of Windows 10? – hoangdv Commented Apr 8, 2019 at 14:20
1 Answer
Reset to default 5 +50As far as I can tell, when you use the Notification API like in your code snippet, you simply cannot handle events that are triggered by clicking the button in a custom way. It seems that the button being visible at all is a Chrome-specific thing, and it's only caused by setting requireInteraction
to true
. At least in Firefox and Edge, the button won't show up at all.
As an alternative and assuming you're making use of a Service Worker, you can also trigger the notification using the Service Worker's registration. By that you can also use additional attributes in the notification's options, like actions
where you can define a list of buttons that should show up. You can define an action
for each button and act accordingly in the Service Worker.
The following code works, tested with Chrome 73. Mind the browser patibility.
I hope that helps.
index.html
<button onclick="notifyMe()">Notify me!</button>
<script src="main.js"></script>
main.js
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js');
}
function notifyMe() {
if (Notification.permission === 'granted') {
navigator.serviceWorker.getRegistration().then((reg) => {
var options = {
body: '<Your Notification Body>',
icon: '<Your Notification Icon>',
actions: [
{ action: 'close', title: 'Close' }
],
requireInteraction: true
};
reg.showNotification('<Your Notification Title>', options);
});
} else {
Notification.requestPermission();
}
}
sw.js
self.addEventListener('notificationclick', (event) => {
if (event.action === 'close') {
console.log('handle close with button');
event.notification.close();
} else {
console.log('handle notification click');
}
}, false);
self.addEventListener('notificationclose', (event) => {
console.log('handle close with arrow');
}, false);
I use notification API to show popup window on Chrome 73:
new Notification('', {
icon: "images/transparent.png",
image: res,
requireInteraction: true
});
notification.onclose = function () {
alert('close')
};
notification.onclick= function () {
alert('click')
};
notification.onerror= function () {
alert('error');
};
notification.onnotificationclose = function () {
alert("close")
};
I see this popup:
But the problem is that if user clicks icon with arrow, then onclose
is fired, but if user clicks 'Close' aka 'Закрыть' button, no handler is invoked.
How can I handle it? Is it a bug in chrome?
I use notification API to show popup window on Chrome 73:
new Notification('', {
icon: "images/transparent.png",
image: res,
requireInteraction: true
});
notification.onclose = function () {
alert('close')
};
notification.onclick= function () {
alert('click')
};
notification.onerror= function () {
alert('error');
};
notification.onnotificationclose = function () {
alert("close")
};
I see this popup:
But the problem is that if user clicks icon with arrow, then onclose
is fired, but if user clicks 'Close' aka 'Закрыть' button, no handler is invoked.
How can I handle it? Is it a bug in chrome?
Share Improve this question edited Apr 8, 2019 at 13:18 user1773603 asked Apr 2, 2019 at 23:53 Stepan YakovenkoStepan Yakovenko 9,26431 gold badges124 silver badges212 bronze badges 6-
Can you put a little more info? Like what version of chrome are you using and what are your Notification actions? Are you using a global service worker ? If so, did you try the
'notificationonclose'
event? – Daniel Piñeiro Commented Apr 5, 2019 at 10:37 - onnotificationclose doesn't help – Stepan Yakovenko Commented Apr 5, 2019 at 11:57
-
Does
notification.addEventListener('close', function () => { ... });
solve the issue? – pschild Commented Apr 8, 2019 at 13:29 - no it doesn't, you can test yourself – Stepan Yakovenko Commented Apr 8, 2019 at 14:10
- I seem this notification like a native notification of Windows 10? – hoangdv Commented Apr 8, 2019 at 14:20
1 Answer
Reset to default 5 +50As far as I can tell, when you use the Notification API like in your code snippet, you simply cannot handle events that are triggered by clicking the button in a custom way. It seems that the button being visible at all is a Chrome-specific thing, and it's only caused by setting requireInteraction
to true
. At least in Firefox and Edge, the button won't show up at all.
As an alternative and assuming you're making use of a Service Worker, you can also trigger the notification using the Service Worker's registration. By that you can also use additional attributes in the notification's options, like actions
where you can define a list of buttons that should show up. You can define an action
for each button and act accordingly in the Service Worker.
The following code works, tested with Chrome 73. Mind the browser patibility.
I hope that helps.
index.html
<button onclick="notifyMe()">Notify me!</button>
<script src="main.js"></script>
main.js
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js');
}
function notifyMe() {
if (Notification.permission === 'granted') {
navigator.serviceWorker.getRegistration().then((reg) => {
var options = {
body: '<Your Notification Body>',
icon: '<Your Notification Icon>',
actions: [
{ action: 'close', title: 'Close' }
],
requireInteraction: true
};
reg.showNotification('<Your Notification Title>', options);
});
} else {
Notification.requestPermission();
}
}
sw.js
self.addEventListener('notificationclick', (event) => {
if (event.action === 'close') {
console.log('handle close with button');
event.notification.close();
} else {
console.log('handle notification click');
}
}, false);
self.addEventListener('notificationclose', (event) => {
console.log('handle close with arrow');
}, false);
本文标签: javascriptChrome notification click on Close buttonStack Overflow
版权声明:本文标题:javascript - Chrome notification click on Close button - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745623808a2159736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论