Chrome extension development: auto close the notification box
Asked Answered
A

5

7

After doing something I run this code:

var notification = webkitNotifications.createNotification(
   'icon.png',  // icon url - can be relative
  'Done!',  // notification title
  'Just updated your list!'  // notification body text
   );
  notification.show();

which of course pops up a notification into the users screen.

It there anyway to time this notification so that it auto-closes in X amount of seconds?

Thanks! R

Allusive answered 20/4, 2011 at 14:30 Comment(1)
Please see this answer for detailed description: https://mcmap.net/q/1177075/-google-chrome-extension-to-close-notification-after-user-clickMadly
P
16

You can use notification.cancel();

Porterhouse answered 13/5, 2011 at 14:53 Comment(3)
I like the notification.cancel(); because it can be called from the same function that creates the notification.Czarist
Thanks! Works exactly like I wanted it to!Allusive
There might be a update for the API, you should use notification.close() now.Perak
C
9
var notification = webkitNotifications.createNotification('images/icon-48x48.png',"This is       Title","Biswarup Adhikari Notification");
notification.show();
setTimeout(function(){
notification.cancel();
},2000);

Chrome notification will close automatically after 2000 milli sec or 2 sec.

Coates answered 19/8, 2012 at 20:1 Comment(0)
I
3

You'll be able to call window.close() from inside the notification's HTML page. That will close the notification.

To close at a certain time, calling something like setTimeout( function () { window.close(); }, timeInMicroseconds); should be effective.

Infestation answered 20/4, 2011 at 14:40 Comment(1)
Thanks you! That hits the spot!Allusive
B
0
function show(title, message, icon) {
try {
    icon = icon || 'src/img/icons/icon48.png';
    var self = this;
    var isClosed = false;
    var notificationId = "posting_" + Math.random();

    chrome.notifications.create(notificationId, {
        type: "basic",
        title: title + "!",
        message: message,
        iconUrl: icon
    }, function (nId) {
    });

    setTimeout(function () {
        if (!isClosed)
            chrome.notifications.clear(notificationId, function (wasCleared) {
            });
    }, 3000);
} catch (e) {
    alert(e.message);
}

}

ok, when i created notification remeber the id notificationId and settimeout clear this id

Bamby answered 10/12, 2015 at 7:23 Comment(1)
please also explain what you changed and whyTuranian
O
-1
//Use requireInternaction and set it to true for notification to not to auto-hide.

function showNotification() {
    var options = {
        body: 'The Subtitles will Go Here',
        requireInteraction: true
    };

    if (window.Notification && Notification.permission !== "denied") {
       Notification.requestPermission(function (status) {  // status is "granted", if accepted by user

var n = new Notification('Title', options);
        });
     }

   }
Overestimate answered 11/2, 2017 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.