Chrome桌面通知点击后如何聚焦到内容

15

我正在开发一款Chrome扩展程序,需要将桌面通知功能集成到其中。我的需求是,当用户单击通知窗口时,需要将其带到引起通知的标签页。我可以使用chrome.tabs API实现这一点,但我无法弄清楚如何在单击通知时将Chrome置于前台。

我知道chrome中禁用了window.focus(),但这绝对是可行的,因为Gmail桌面通知就是这种行为。

3个回答

27
notification = webkitNotifications.createNotification(...)
notification.onclick = function(){
    window.focus();
    this.cancel();
};
notification.show()

...正常工作,无需任何额外权限。


1
这是错误的,这是针对HTML5 API的,他正在询问Chrome扩展API。 - RVera
这对我也不起作用。点击通知只会关闭通知,而不会将焦点设置在窗口上。 - Marko
我在这里发布了更新的答案:https://dev59.com/qm445IYBdhLWcg3wWY8U#40964355 - jazzcat

6

我想确保选项卡被设置为活动状态并且窗口获得焦点。您是否知道同时执行这两个操作是否会有害,或者我应该先设置选项卡为活动状态,然后在回调中设置窗口焦点? - Cody Allan Taylor

4
function msg(){
    var notification = new Notification("Title", {body: "Yore message", icon: "img.jpg" });
    notification.onshow = function() { setTimeout(notification.close(), 15000); };
    notification.onclick = function(){
        window.focus();
        this.cancel();
    };
}
msg();


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接