防止chrome.notifications API在几秒钟后隐藏我的通知 [英] Prevent chrome.notifications API from hiding my notification after a few seconds
问题描述
我每天做1-2个通知,重要的是用户不会错过它。有没有办法消除自动关闭,只允许用户手动关闭通知?
我没有看到通知选项中的任何选项:
http://developer.chrome .com / extensions / notifications.html#type-NotificationOptions
UPDATE(2016-05- 24):
有趣的事实:所有这些奥术黑客不再需要;查看新的requireInteraction
标志
自Chrome 50开始可用。更多信息。
< hr>
感谢root的评论,这个答案被修改以解释 您可以利用通知的生命周期以下列事件之一结束的事实: 建议的解决方案由以下步骤组成: 写起来很简单,编码需要更多的努力:) 在 manifest.json 中: > 在 background.js 中: 最后说明:如果你的通知是非常重要的,你应该实现一个恢复机制,例如浏览器操作系统崩溃或者突然终止。例如。依赖更持久的存储( I'm doing around 1-2 notifications a day and it's important the user doesn't miss it. Is there a way of removing the auto close and only allowing the user to manually close the notification? I don's see any option for this in the Notification Options: http://developer.chrome.com/extensions/notifications.html#type-NotificationOptions UPDATE (2016-05-24): Fun fact: all this arcane hacking is no longer needed; see the new It is availalbe since Chrome 50. More info. Thanks to root's comment, this answer is revised to account for the fact that the You can take advantage of the fact that a notification's life-cycle ends with one of the following events: The proposed solution consists of the following steps: Writing about it is simple, coding takes some more effort :)
Here is the sample code I used to achieve what is described above: In manifest.json: In background.js:
Final notes: 这篇关于防止chrome.notifications API在几秒钟后隐藏我的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
onClosed
:当用户点击右上角的小'x'时。 onClicked
:When用户点击邮件正文(不是'x',而不是某个按钮)。
onButtonClicked
:当用户点击其中一个按钮)。
解决方案n
以下是我用来实现上述内容的示例代码:
$ b
$ b
$ b {
manifest_version:2,
name:Test Extension,
版本:0.0,
背景:{
//我们需要这个时间out - 请参阅下面的注释
persistent:true,
scripts:[background.js]
},
browser_action:{
default_title:Test Extension
default_icon:{
19:img / icon19.png,
38:img / icon38。 png
,
},
permissions:[notifications]
}
var pendingNotifications = {};
/ *出于演示目的,创建通知的
*附加到浏览器操作的onClicked事件。
*根据您的需求进行更改。 * /
chrome.browserAction.onClicked.addListener(function(){
var dateStr = new Date()。toUTCString();
var details = {
type:basic ,
iconUrl:/img/notifIcon.png,
title:REMINDER,
message:dateStr +\\\
\\\
+There
+现在处理它?,
contextMessage:非常重要的东西......,
按钮:[
{title:Yes},
{title:No}
]
};
var listeners = {
onButtonClicked:function(btnIdx) {
if(btnIdx === 0){
console.log(dateStr +' - Clicked:yes');
} else if(btnIdx === 1){
console.log(dateStr +' - 点击:no');
}
},
onClicked:function(){
console.log(dat eStr +' - 点击:message-body');
},
onClosed:function(byUser){
console.log(dateStr +' - Closed:'
+(byUser?'by user':'automagically(!? )'));
}
};
/ *创建通知* /
createNotification(details,listeners);
});
$ b $ *创建通知并存储其重新生成计时器和事件监听器的引用
* * /
函数createNotification(details,listeners,notifId){
(notifId!== undefined)|| (notifId =);
chrome.notifications.create(notifId,details,function(id){
console.log('Created notification''+ id +'!');
if(pendingNotifications [id ]!$ undefined){
clearTimeout(pendingNotifications [id] .timer);
}
pendingNotifications [id] = {
监听者:
timer:setTimeout(function(){
console.log('Re-spawning notification''+ id +'...');
destroyNotification(id,function(wasCleared){
if(wasCleared){
createNotification(details,listeners,id);
}
});
},10000)
};
});
}
* *完全删除通知,取消其重新生成计时器(如果有的话)
*可选地,提供回调以在成功移除后执行* /
函数destroyNotification(notifId,callback){
/ *取消重新生成计时器(如果有)* /
if(pendingNotifications [notifId]!== undefined){
clearTimeout(pendingNotifications [notifId] .timer);
delete(pendingNotifications [notifId]);
$ b $ *删除通知本身* /
chrome.notifications.clear(notifId,function(wasCleared){
console.log('Destroyed notification '+ notifId +'!');
/ *执行回调(如果有)* /
回调&& callback(wasCleared);
});
$ b $ * / *响应用户点击其中一个按钮* /
chrome.notifications.onButtonClicked.addListener(function(notifId,btnIdx){
if (pendingNotifications [notifId]!== undefined){
var handler = pendingNotifications [notifId] .listeners.onButtonClicked;
destroyNotification(notifId,handler(btnIdx));
}
});
$ b $ *响应用户点击通知消息body * /
chrome.notifications.onClicked.addListener(function(notifId){
if(pendingNotifications [notifId] !==未定义){
var handler = pendingNotifications [notifId] .listeners.onClicked;
destroyNotification(notifId,handler());
}
});
$ b $ * / *响应用户点击右上角的小'x'* /
chrome.notifications.onClosed.addListener(函数(notifId,byUser){
if(pendingNotifications [notifId]!== undefined){
var handler = pendingNotifications [notifId] .listeners.onClosed;
destroyNotification(notifId,handler(byUser));
}
});
localStorage
, chrome.storage
API 等),在扩展/浏览器启动等时恢复待处理通知。
< sub>
Timeout
.com / extensions / alarms.htmlrel =nofollow noreferrer> chrome.alarms
API ,然后将背景页面转换为非-persistent(又名 活动页面 ),这将使它更加资源友好。
requireInteraction
flag
onClosed
event is not fired when the notification disappears (into the notigications area) after a few seconds. This is still kind of a hacky solution.
Background
onClosed
: When the user clicks on the small 'x' in the top-right corner.onClicked
: When the user clicks on the message body (not the 'x', not some button).onButtonClicked
: When the user clicks on one of the buttons (if any).The solution
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"background": {
// We need this for the `Timeout` - see notes below
"persistent": true,
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Test Extension"
"default_icon": {
"19": "img/icon19.png",
"38": "img/icon38.png"
},
},
"permissions": ["notifications"]
}
var pendingNotifications = {};
/* For demonstration purposes, the notification creation
* is attached to the browser-action's `onClicked` event.
* Change according to your needs. */
chrome.browserAction.onClicked.addListener(function() {
var dateStr = new Date().toUTCString();
var details = {
type: "basic",
iconUrl: "/img/notifIcon.png",
title: "REMINDER",
message: dateStr + "\n\n"
+ "There is one very important matter to attend to !\n"
+ "Deal with it now ?",
contextMessage: "Very important stuff...",
buttons: [
{ title: "Yes" },
{ title: "No" }
]
};
var listeners = {
onButtonClicked: function(btnIdx) {
if (btnIdx === 0) {
console.log(dateStr + ' - Clicked: "yes"');
} else if (btnIdx === 1) {
console.log(dateStr + ' - Clicked: "no"');
}
},
onClicked: function() {
console.log(dateStr + ' - Clicked: "message-body"');
},
onClosed: function(byUser) {
console.log(dateStr + ' - Closed: '
+ (byUser ? 'by user' : 'automagically (!?)'));
}
};
/* Create the notification */
createNotification(details, listeners);
});
/* Create a notification and store references
* of its "re-spawn" timer and event-listeners */
function createNotification(details, listeners, notifId) {
(notifId !== undefined) || (notifId = "");
chrome.notifications.create(notifId, details, function(id) {
console.log('Created notification "' + id + '" !');
if (pendingNotifications[id] !== undefined) {
clearTimeout(pendingNotifications[id].timer);
}
pendingNotifications[id] = {
listeners: listeners,
timer: setTimeout(function() {
console.log('Re-spawning notification "' + id + '"...');
destroyNotification(id, function(wasCleared) {
if (wasCleared) {
createNotification(details, listeners, id);
}
});
}, 10000)
};
});
}
/* Completely remove a notification, cancelling its "re-spawn" timer (if any)
* Optionally, supply it with a callback to execute upon successful removal */
function destroyNotification(notifId, callback) {
/* Cancel the "re-spawn" timer (if any) */
if (pendingNotifications[notifId] !== undefined) {
clearTimeout(pendingNotifications[notifId].timer);
delete(pendingNotifications[notifId]);
}
/* Remove the notification itself */
chrome.notifications.clear(notifId, function(wasCleared) {
console.log('Destroyed notification "' + notifId + '" !');
/* Execute the callback (if any) */
callback && callback(wasCleared);
});
}
/* Respond to the user's clicking one of the buttons */
chrome.notifications.onButtonClicked.addListener(function(notifId, btnIdx) {
if (pendingNotifications[notifId] !== undefined) {
var handler = pendingNotifications[notifId].listeners.onButtonClicked;
destroyNotification(notifId, handler(btnIdx));
}
});
/* Respond to the user's clicking on the notification message-body */
chrome.notifications.onClicked.addListener(function(notifId) {
if (pendingNotifications[notifId] !== undefined) {
var handler = pendingNotifications[notifId].listeners.onClicked;
destroyNotification(notifId, handler());
}
});
/* Respond to the user's clicking on the small 'x' in the top right corner */
chrome.notifications.onClosed.addListener(function(notifId, byUser) {
if (pendingNotifications[notifId] !== undefined) {
var handler = pendingNotifications[notifId].listeners.onClosed;
destroyNotification(notifId, handler(byUser));
}
});
localStorage
, chrome.storage
API etc), resuming pending notifications on extension/browser start-up etc.
Timeout
's with the chrome.alarms
API and then convert the background-page to non-persistent (a.k.a. event-page) which will make it more resource-friendly.