1

Let's say I want to show the same notification each time something happens. That's what I currently use:

chrome.notifications.create(id, {   
                type:"basic",
                title:"Title",
                message:"My message",
                iconUrl: "icon.png",
            }, notificationResult);

But sometimes the notification doesn't appear.

Is that an id thing ? Do I need to reuse an already created notification ? Can I not create a new notification with the same id ?

I tried to do a var notification = chrome.notifications.create(id .... ) and do a notification.show() in case I already created one with the same id but that also didn't solve it.

So - do I need to recreate an existing notification each time I want to show the same one (which currently doesn't work for me), or is there a different way? How to make sure it pops every time?

Omri Aharon
  • 16,959
  • 5
  • 40
  • 58

2 Answers2

4

The id in the create function is specifically for reusing. IDs must be unique. If you use create with an ID of an existing notification, it basically behaves like an update.

If a notification exists, it may no longer be shown but only be visible in the Message Center. In this case, the notification IS updated - but not shown again.

The API docs specify that you can pass an empty string to the notification to get a unique new id. If you need it, it is passed to the callback.

But if you do want to reuse the ID (ensuring that the notification is unique), you can use priority trick to make it show again.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • I see. So what I did was to keep a variable that holds the last passed priority, and after each `create` I incremented it and on the next `create` it showed up again. Is there a simpler way? Thanks! – Omri Aharon Oct 25 '14 at 06:59
  • As far as I know, priorities range from -2 to 2, so you may get errors with this. – Xan Oct 25 '14 at 09:26
3

You can clear the notification if its not use and if you want to use the same id.

For example :

function Notify(){
var my_notif_id="some_id";

//This will clear your previous notifcation with the same ID 
chrome.notifications.clear(my_notif_id,function(){});

chrome.notifications.create(my_notif_id,options,function(){});

}

Now each time you call the notify function to display notification it will clear the old notification before displaying new notification and gets displayed.

UPDATED

As @Xan suggested, Its good to incorporate the create() method inside callback function of clear() So here is the complete example :

function Notify(id, options){
  //This will clear your previous notifcation with the same ID 
  chrome.notifications.clear(id, function() {
    //inside callback function
    chrome.notifications.create(id, options, function(){});
  });
}
Xan
  • 74,770
  • 16
  • 179
  • 206
Rafique Mohammed
  • 3,666
  • 2
  • 38
  • 43
  • I want to upvote you, but your code makes little sense (it clears the notification immediately). It would make more sense if you swapped the operations (clear before create). – Xan Feb 18 '15 at 11:32
  • 1
    While it probably works, it's best to keep to the async logic flow and put the `create` call inside the callback of `clear`. – Xan Feb 18 '15 at 13:45