3

The Chrome extension works fine. My problem is that the notification closes in 7 seconds. I want for the user click to close the notification.

function engine(){
    var latestId;
    var ids;
    var messages = [];
    var newmessage = [];

    $.get('http://localhost/site/json.php',function (data){
        var json = $.parseJSON(data);
        messages = json;
        ids = json[0].id;
        if(latestId == ids){
            //no update
        }else if(latestId === undefined){
            var run = {
                type: "basic",
                title: "Title",
                message: "Some message",
                iconUrl : "icon.png",
            };

            chrome.notifications.create(run);
        }    
    });
}
Antti29
  • 2,953
  • 12
  • 34
  • 36
user3811744
  • 27
  • 1
  • 4
  • Unsure what you're asking. Do you want the notification to close whenever you click on it, or do you want to keep it from hiding after 7 seconds and close ONLY on user interaction? – Xan Mar 23 '16 at 12:16

4 Answers4

3

First create your notification and give it a notificationID parameter to close it later.

var notification = {
    type:"basic",
    title:"News From Us!",
    message:"Google Chrome Updated to v50!",
    iconUrl:"assets/images/icon.png"
};
chrome.notifications.create("notfyId",notification);

On notification click you can close notification using its id (which is "notfyId")

function forwardNotfy(){
    chrome.notifications.clear("notfyId");
    window.open(url); //optional
 }

 chrome.notifications.onClicked.addListener(forwardNotfy);

Now, when you click your notification it'll close.

Nezih
  • 381
  • 6
  • 19
1

You can use notification.close();:

setTimeout(function() {
    notification.close();
}, 2000);

Demo:

// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
  if (Notification.permission !== "granted")
    Notification.requestPermission();
});

function notifyMe() {
  if (!Notification) {
    alert('Desktop notifications not available in your browser. Try Chromium.'); 
    return;
  }

  if (Notification.permission !== "granted")
    Notification.requestPermission();
  else {
    var notification = new Notification('Notification title', {
      icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
      body: "Hey there! You've been notified!x",
    });

    notification.onclick = function () {
      window.open("http://stackoverflow.com/a/13328397/1269037");      
    };
    
    setTimeout(function() {
    notification.close();
}, 2000);
  }

}
<button onclick="notifyMe()">
  Notify me!
</button>

JSBin Demo

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 1
    notification.cancel is not a function write in consol ? Can you please example code write here? – user3811744 Mar 23 '16 at 12:20
  • @Arokiyanathan: ​​​​​​​​​​​​​​​Please [edit] your answer, and put the link into it. Also please try to make your answer detailed and clearly. See: [answer] – Remi Guan Mar 23 '16 at 14:15
1

This feature is currently only implemented in the beta channel, and not in the latest version of chrome. See here for details.

When it is implemented, you will be able to use requireInteraction : True like:

var run = {
    type: "basic",
    title: "Title",
    message: "Some message",
    iconUrl : "icon.png",
    requireInteraction : True,
}

to implement this.

James Paterson
  • 2,652
  • 3
  • 27
  • 40
0

notification.close() is used to close any notification.

For more information please see the below code-

To create the notification:

var notification = new Notification('OnlineOfferPrice', {
    icon: 'icon.png',
    body: "Test Message",
});

If you want to perform operation on notification click please use the below code. After your business logic it will automatically close-

notification.onclick = function () { 
    //write your business logic here.
    notification.close();
};

If notification is not clicked and you want to close it automatically after 6 seconds-

setTimeout(function() { notification.close(); }, 6000);
IRSHAD
  • 2,855
  • 30
  • 39