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 answar: after Chrome 50, please add new attribute: [requireInteraction: true]!
don't use
chrome.notifications.create
try to use
var notification= new Notification("New mail from John Doe", { tag: 'msg1', requireInteraction: true});
will not to close.
if want to close=>
notification.close();
ref: http://www.w3.org/TR/notifications/
https://developer.mozilla.org/en-US/docs/Web/API/notification/requireInteraction
webkit notifications seem to be more persistent on the screen, althouth they have the opposite problem - how to hide them to sys tray.
http://developer.chrome.com/extensions/desktop_notifications.html
UPDATE (2016-05-24):
Xan commented:
It is availalbe since Chrome 50. More info.
Thanks to root's comment, this answer is revised to account for the fact that the
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
You can take advantage of the fact that a notification's life-cycle ends with one of the following events:
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
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:
localStorage
,chrome.storage
API etc), resuming pending notifications on extension/browser start-up etc.Timeout
's with thechrome.alarms
API and then convert the background-page to non-persistent (a.k.a. event-page) which will make it more resource-friendly.This answer is obsolete; see this answer for an up to date solution involving
requireInteraction
flag (Chrome 50+).There is a slightly better (but again, hacky) solution.
When you call
update
on a notification that changes its priority, and the priority is 0 or above, the notification will be re-shown and the timer for hiding it reset.So, you can show a notification with a high priority, (say, 2) and then repeat this on an interval shorter than time to hide the notification:
Notification now has (since Chrome 50) a
requireInteraction
property to force the notification to stay on screen:In
onclick
you have to close the notification: