use*_*162 18 google-chrome google-chrome-extension
我每天要做1-2次通知,重要的是用户不要错过它.有没有办法删除自动关闭,只允许用户手动关闭通知?
我在通知选项中看不到任何选项:
http://developer.chrome.com/extensions/notifications.html#type-NotificationOptions
Mat*_*llo 16
通知现在(自Chrome 50起)一个强制通知留在屏幕上的requireInteraction属性:
var notification = new Notification("TITLE", {
icon: 'assets/res/icon.png',
body: "MESSAGE",
requireInteraction: true
});
Run Code Online (Sandbox Code Playgroud)
在onclick你必须关闭通知:
notification.onclick = function()
{
this.close();
}
Run Code Online (Sandbox Code Playgroud)
gka*_*pak 12
更新(2016-05-24):
有趣的事实:不再需要所有这些神秘的黑客行为; 看到新的
requireInteraction旗帜
它可用于Chrome 50. 更多信息.
感谢root的评论,修改了这个答案,以解释onClosed在几秒钟后通知消失(进入notigications区域)时事件未被触发的事实.这仍然是一种hacky解决方案.
您可以利用通知的生命周期以下列事件之一结束的事实:
onClosed:当用户点击右上角的小'x'时.onClicked:当用户点击邮件正文时(不是'x',而不是某些按钮).onButtonClicked:当用户单击其中一个按钮(如果有)时.建议的解决方案包括以下步骤:
写它很简单,编码需要更多的努力:)这是我用来实现上述内容的示例代码:
在manifest.json中:
{
"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"]
}
Run Code Online (Sandbox Code Playgroud)
在background.js中:
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));
}
});
Run Code Online (Sandbox Code Playgroud)
最后的说明:
localStorage,chrome.storageAPI等),上延伸部恢复待处理通知/浏览器的启动等.
Timeout用chrome.alarmsAPI替换's 然后将后台页面转换为非持久性(也就是事件页面),这将是使它更加资源友好.| 归档时间: |
|
| 查看次数: |
10869 次 |
| 最近记录: |