我希望我的 chrome 扩展采用 notification.id
,并且:
- 更新现有通知(如果存在)。 或者
- 如果不存在则创建一个新通知。
调用 clear()
然后调用 create()
并不理想,因为 remove()
和 的动画在视觉上都是不协调的>create()
方法,我想在没有动画的情况下更新。另外,显然,在消失的通知上调用 update()
不会做任何事情。
有没有简单的方法来实现这个?
最佳答案
编辑:由于删除了 Chrome 的通知中心,此方法不再适用于除 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
});
}
});
}
关于javascript - chrome.notifications.update() 或 create(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350747/