Simon
Simon

Reputation: 504

How to close chrome notifications programmaticaly without clearing

I am currently using a user supplied variable to set the length of the time a notification popup is displayed to the user. However, I assume because I am using chome.notifications.clear(), I have the unwanted side effect of notifications not being stored in the system tray/bell icon menu.

Is there a way where I can progammaticaly set the amount of duration a notification popup is displayed, without removing it from the list of notifications in the system tray/bell icon menu?

Here is a snippet of my notification creation code:

var opt = {
  type: "basic",
  title: notification.app + ": " + notification.title,
  message: "",
  iconUrl: dataUri,
  contextMessage: time
}

var note = chrome.notifications.create("someid", opt, function(id){
        console.log("notification id: " + id);
        setTimeout(function(){
                chrome.notifications.clear(id,function(wasCleared){
                        //need to do anything?
                });
        }, get_value_from_local("note_time") * 1000 );
});

Upvotes: 1

Views: 168

Answers (1)

Xan
Xan

Reputation: 77521

There is a notification property, priority, that you can use to emulate the behavior you need.

The official documentation on notifications is, frankly, piss-poor, but more information can be extracted from linked articles. This one (emphasis mine):

Notifications can be assigned a priority between -2 to 2. Priorities < 0 are only shown in the center; priorities > 0 are shown for increasing duration and more high priority notifications can be displayed in the system tray.

In fact, you can update the priority value to show or hide your notification.

chrome.notifications.create(
  "test",
  {
    type: "basic",
    iconUrl: "ion.png",
    title: "Test",
    message: "Hide me!"
  },
  function(){
    setTimeout( function(){
      chrome.notifications.update("test", {priority: -1}, function(){});
    }, 1000);
  }
);

Likewise if you update the priority to 0 or above, an existing notification will show up again.

Upvotes: 3

Related Questions