浏览器通知onclick事件未在Chrome中触发

时间:2018-11-20 11:36:45

标签: javascript notifications

我正在创建浏览器通知,它在Firefox和Edge中效果很好。但是由于某种原因,它无法在Chrome中使用。我可以看到通知,但是当我单击通知时,它不会被解雇。这是我的代码:

function notifyMe(text) {
    if (Notification.permission !== "granted")
        Notification.requestPermission();
    else {
        var notification = new Notification('Notification title', {               
            body: text,
            requireInteraction: true     
        });
        notification.onclick = function (event) {
            alert("clicked");
            event.preventDefault(); 
            console.log('Notification clicked.');
        } 

    }      
}

所以我的问题是,为什么notificaion.onclick不会在chrome中触发?

1 个答案:

答案 0 :(得分:0)

您可能正在单击关闭按钮,它不会触发单击事件,请尝试单击除关闭按钮以外的任何内容,并尝试使用public class Controller { @FXML private ContextMenu contextMenu; @FXML private BorderPane borderPane; @FXML private void initialize() { borderPane.setOnContextMenuRequested(event -> { contextMenu.show(borderPane, event.getScreenX(), event.getScreenY()); }); } } 事件

onclose

或者尝试使用function notifyMe(text) { if (Notification.permission !== "granted") Notification.requestPermission(); else { var notification = new Notification('Notification title', { body: text, requireInteraction: true }); notification.onclick = function (event) { alert("onClick!"); event.preventDefault(); console.log('Notification clicked.'); } notification.onclose = function (event) { alert("onClose!"); event.preventDefault(); console.log('Notification clicked.'); } } }

EventListener