【问题标题】:Google Chrome extension to close notification after user click谷歌浏览器扩展在用户点击后关闭通知
【发布时间】:2016-07-10 16:38:29
【问题描述】:

Chrome 扩展程序运行良好。我的问题是通知会在 7 秒内关闭。我想让用户点击关闭通知。

function engine(){
    var latestId;
    var ids;
    var messages = [];
    var newmessage = [];

    $.get('http://localhost/site/json.php',function (data){
        var json = $.parseJSON(data);
        messages = json;
        ids = json[0].id;
        if(latestId == ids){
            //no update
        }else if(latestId === undefined){
            var run = {
                type: "basic",
                title: "Title",
                message: "Some message",
                iconUrl : "icon.png",
            };

            chrome.notifications.create(run);
        }    
    });
}

【问题讨论】:

  • 不确定你在问什么。您是希望在单击通知时关闭通知,还是希望在 7 秒后不隐藏通知并仅在用户交互时关闭?

标签: javascript google-chrome-extension


【解决方案1】:

notification.close() 用于关闭任何通知。

更多信息请看下面的代码-

创建通知:

var notification = new Notification('OnlineOfferPrice', {
    icon: 'icon.png',
    body: "Test Message",
});

如果您想对通知点击执行操作,请使用以下代码。在你的业务逻辑之后它会自动关闭-

notification.onclick = function () { 
    //write your business logic here.
    notification.close();
};

如果通知没有被点击,而你想在 6 秒后自动关闭它-

setTimeout(function() { notification.close(); }, 6000);

【讨论】:

    【解决方案2】:

    首先创建你的通知并给它一个 notificationID 参数以便稍后关闭它。

    var notification = {
        type:"basic",
        title:"News From Us!",
        message:"Google Chrome Updated to v50!",
        iconUrl:"assets/images/icon.png"
    };
    chrome.notifications.create("notfyId",notification);
    

    点击通知时,您可以使用其 ID(即“notfyId”)关闭通知

    function forwardNotfy(){
        chrome.notifications.clear("notfyId");
        window.open(url); //optional
     }
    
     chrome.notifications.onClicked.addListener(forwardNotfy);
    

    现在,当您单击通知时,它会关闭。

    【讨论】:

    • window.location 不工作,但是 window.open 可以
    • 感谢您的提示!
    【解决方案3】:

    你可以使用notification.close();:

    setTimeout(function() {
        notification.close();
    }, 2000);
    

    演示:

    // request permission on page load
    document.addEventListener('DOMContentLoaded', function () {
      if (Notification.permission !== "granted")
        Notification.requestPermission();
    });
    
    function notifyMe() {
      if (!Notification) {
        alert('Desktop notifications not available in your browser. Try Chromium.'); 
        return;
      }
    
      if (Notification.permission !== "granted")
        Notification.requestPermission();
      else {
        var notification = new Notification('Notification title', {
          icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
          body: "Hey there! You've been notified!x",
        });
    
        notification.onclick = function () {
          window.open("http://stackoverflow.com/a/13328397/1269037");      
        };
        
        setTimeout(function() {
        notification.close();
    }, 2000);
      }
    
    }
    <button onclick="notifyMe()">
      Notify me!
    </button>

    JSBin 演示

    【讨论】:

    • notification.cancel 不是写在 consol 中的函数吗?你能把示例代码写在这里吗?
    • @Arokiyanathan:​​​​​​​​​​​​​​请edit你的答案,并把链接放进去。另外,请尽量使您的答案详细而清晰。见:How to Answer
    【解决方案4】:

    此功能目前仅在 beta 通道中实现,并没有在最新版本的 chrome 中实现。详情请见here

    实现后,你就可以使用requireInteraction : Truelike:

    var run = {
        type: "basic",
        title: "Title",
        message: "Some message",
        iconUrl : "icon.png",
        requireInteraction : True,
    }
    

    实现这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 2011-06-04
      • 2015-04-17
      相关资源
      最近更新 更多