8

I'm developing an extension with chrome.notifications would like to set a time for closing the notification, how can I do this?

guto
  • 117
  • 1
  • 1
  • 8

2 Answers2

11

Chrome automatically hides the notification. How long it stays on screen depends on the priority attribute. Note that everywhere except Chrome OS the hidden notification is effectively closed, as the Notification Center was removed.

It's possible to keep the notification shown, but you'll have to resort to dirty tricks. If you don't want to do it the hard way, consider using Web Notifications instead - they will not be hidden.

Update: Both chrome.notifications and Web Notifications APIs now implement a boolean flag requireInteraction that defines whether Chrome will fade the notification away automatically or not.

You can still manually close (i.e., remove completely) the notification by calling chrome.notifications.clear() with the notification ID.

To schedule something, you can either use DOM intervals, or chrome.alarms API.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
5

I'm using this in my project

var opt = {type, title, message, etc....}

chrome.notifications.create("", opt, function(id) {
    timer = setTimeout(function(){chrome.notifications.clear(id);}, 2000);
});

notification is closed after 2 sec (2000 ms)

Wolf War
  • 1,503
  • 1
  • 15
  • 28
  • Works well for short times, but needs additional magic to keep displayed for longer times. – Xan Apr 10 '15 at 10:51
  • 1
    yea, I presumed that he want's short(er) time just to flash notification and not to bother users with closing it (I need that behavior), but for longer than default, this won't work – Wolf War Apr 10 '15 at 13:45