我希望我的铬分机能得到一个notification.id,并且:
调用clear()然后create()并不理想,因为动画对remove()和create()方法都有视觉上的干扰,在这里我想在没有动画的情况下进行更新。此外,显然,在消失的通知上调用update()没有任何作用。
有什么简单的方法来实现这一点吗?
发布于 2014-10-14 10:09:51
编辑:由于删除了,不再在任何平台上工作,只在ChromeOS上工作。
可能的解决方法包括在通知中使用requireInteraction: true标志来完全控制通知生存期。
重新显示通知有一个肮脏的伎俩。如果将通知的优先级更改为更高的值,则将重新显示它是否存在。
function createOrUpdate(id, options, callback) {
// Try to lower priority to minimal "shown" priority
chrome.notifications.update(id, {priority: 0}, function(existed) {
if(existed) {
var targetPriority = options.priority || 0;
options.priority = 1;
// Update with higher priority
chrome.notifications.update(id, options, function() {
chrome.notifications.update(id, {priority: targetPriority}, function() {
callback(true); // Updated
});
});
} else {
chrome.notifications.create(id, options, function() {
callback(false); // Created
});
}
});
}发布于 2017-07-13 11:21:01
Xan's answer不再在Windows、MacOS或Linux上工作。此时,确保通知显示的唯一方法是创建一个新的通知。
如果要防止多个通知出现在屏幕上,则必须清除旧通知,并将其替换为新通知。下文将说明这一点。
NOTIFICATION_ID = "some_random_string";
function showNotification ( ... , callback) {
chrome.notifications.clear(NOTIFICATION_ID, function(cleared) {
var options = {
// whatever
};
chrome.notifications.create(NOTIFICATION_ID, options, callback);
});
}当然,这会导致现有通知被驳回,并立即出现新的通知,但不幸的是,这是不可避免的。
https://stackoverflow.com/questions/26350747
复制相似问题