首页 > 解决方案 > chrome.notifications.update() not change my notification

问题描述

I want my chrome extension can work as follow: once the icon is clicked, it send request to server and write some data into database; after these are done, it will popup a notification says that you track this page successfully.

However, I noticed that it would take some time before the notification pops up(because slow backend processing), so I want that once I click the icon, it firstly pops up a notification says you are tracking the page, after server request complete successfully, the notification updates to saying that you tracked this page successfully.

The problem is currently it only pops up first notification and disappears after sometime. The second notification never shows up. Can someone tell me why?

Here is some of my code related:

var xhr = new XMLHttpRequest();
var n_id;
xhr.open("POST", "http://34.204.12.200:5000/track_page", true);
if (xhr.readyState !== 4) {
    var opt1 = {
        type: "basic",
        title: "price tracker",
        message: "tracking this item",
        iconUrl: "sign check.png"
    }
    chrome.notifications.create(opt1, function(notificationId) {

        n_id = notificationId;
        console.log(n_id);
    });
}

xhr.onreadystatechange = function(e) {
    if (xhr.readyState === 4) {
        console.log('responseText1: ', xhr.responseText);
        if (xhr.responseText === 'success') {
            var opt2 = {
                type: "basic",
                title: "price tracker",
                message: "track this item successfully!",
                iconUrl: "sign check.png"
            }
            console.log('readyState:', xhr.readyState);
            console.log('status:', xhr.status);
            console.log(n_id);
            chrome.notifications.update(n_id, opt2);
            chrome.browserAction.setIcon({
                tabId: tab.id,
                path: {
                    "38": "icon2.png"
                }
            });
            res = 'yes';
        }
    }
}

and how to move code clock left in stackoverflow?

标签: javascriptgoogle-chrome-extension

解决方案


发生这种情况是因为默认情况下,这些通知会在几秒钟后消失,因此您无法更新消失的通知。为了保留通知,请requireInteraction: true在创建通知时在通知选项中添加属性。

另一件事是在更新方法中使用回调函数进行调试。


推荐阅读