问题描述
我希望我的Chrome扩展采用 notification.id
和:
- 如果存在,更新现有通知。
或 - 创建一个新的通知(如果它不存在)。
调用 clear()
然后 create()
并不理想,因为动画对于 remove()
和 create()
方法,我想在没有动画的情况下进行更新。另外,显然,在消失的通知上调用 update()
不会执行任何操作。
有没有一种简单的方法来实现这一点?
编辑:由于删除了Chrome的通知中心,此方法不再适用于除ChromeOS之外的任何平台。
可能的解决方法包括使用 requireInteraction:true
通知上的标记可完全控制通知生命周期。
重新显示通知有一个肮脏的窍门。如果您将通知的优先级更改为更高的值,则会将其重新显示(如果存在)。
function createOrUpdate(id, (优先级:0),函数(存在){
if(存在){
var targetPriority = options.priority || 0;
options.priority = 1;
//以更高优先级更新
chrome.notifications.update(id,options ,函数(){
chrome.notifications.update(id,{priority:targetPriority},function(){
callback(true); //已更新
});
});
} else {
chrome.notifications.create(id,options,function(){
callback(false); //创建
});
}
});
}
I want my chrome extension to take a notification.id
, and:
- Update an existing notification if it does exist.OR
- Create a new notification if it doesn't exist.
Calling clear()
then create()
is not ideal, since the animation is visually jarring for both remove()
and create()
methods, where I want to update without animations. Plus, obviously, calling update()
on a disappeared notification doesn't do anything.
Is there an easy way to implement this?
Edit: This approach no longer works on any platform except ChromeOS due to the removal of Chrome's Notification Center.
Possible ideas to work around it include using requireInteraction: true
flag on notifications to fully control notification lifetime.
There is a dirty trick for re-showing a notification. If you change a notification's priority to a higher value, it will be re-shown if it exists.
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
});
}
});
}
这篇关于chrome.notifications.update()或create()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!