admin管理员组文章数量:1429777
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?
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?
Share Improve this question edited Oct 14, 2014 at 9:49 Marco Bonelli 69.8k21 gold badges127 silver badges146 bronze badges asked Oct 14, 2014 at 0:03 Keven WangKeven Wang 1,2781 gold badge19 silver badges30 bronze badges2 Answers
Reset to default 7Edit: 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
});
}
});
}
Xan's answer no longer works on Windows, MacOS, or Linux. At this point the only way to make sure your notification displays, no matter what, is to create a new notification.
If you want to prevent multiple notifications from being on screen, you'll have to clear the old notification and replace it with a new one. This is demonstrated below.
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);
});
}
Of course, this results in an animation of existing notification getting dismissed and a new notification immediately taking its place, but unfortunately this is unavoidable.
本文标签: javascriptchromenotificationsupdate() or create()Stack Overflow
版权声明:本文标题:javascript - chrome.notifications.update() or create() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745428520a2658222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论