chrome rich notifications change timeout to close

1.2k Views Asked by At

I am creating an google-chrome-extension which has rich notifications. I need to change timeout to close in this notification and I need some help with that. This is my code. I have already tried window.close(), but it does not seem to work in Chrome.

var options = {
  type: "basic",
  title: "test",
  message: "body here",
  iconUrl: "icon.png"
};
var msj = chrome.notifications.create(options);
setTimeout(function () {
  chrome.notifications.clear(msj); // how to close?
}, 1500); 
1

There are 1 best solutions below

1
On

Chrome APIs are mostly asynchronous (definitely take a look at that link).

chrome.notifications.create does not immediately create the notification, and will not return the ID. You need to use the callback for that:

chrome.notifications.create(options, function(msj) {
  setTimeout(function() {
    chrome.notifications.clear(msj);
  }, 1500); 
});