javascript - 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?
解决方案
发生这种情况是因为默认情况下,这些通知会在几秒钟后消失,因此您无法更新消失的通知。为了保留通知,请requireInteraction: true
在创建通知时在通知选项中添加属性。
另一件事是在更新方法中使用回调函数进行调试。
推荐阅读
- mendix - 如何在 Mendix 中导入小部件包?
- javascript - 如何让黄瓜在 selenium nightwatch 框架中运行
- r - 如何将函数应用于许多 ML 模型并保存它们的输出?
- c - 如何将 Windows 消息从一个线程传递到另一个线程?
- python - Kivy 应用程序在运行时抛出 context_intructions.so 错误
- java - 如何在我的演示代码中访问以下方法“addPoints()”和“getScores()”
- laravel - Laravel 中实现的 SQL 连接和查询在哪里?
- java - Java比较对象列表以查找重复项并将它们放入新列表中
- ffmpeg - 是否可以对具有确切长度(到毫秒)的音频流进行编码?
- swift - 打开基于 NSDocument 的应用程序会加载两个窗口