2011-04-20 801 views
7

Después de hacer algo que ejecuta este código:Chrome desarrollo de extensiones: auto cerrar el cuadro de notificación

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

que por supuesto aparece una notificación en la pantalla de los usuarios.

¿Hay alguna manera de sincronizar esta notificación para que se cierre automáticamente en X cantidad de segundos?

Gracias! R

Respuesta

16

Puede utilizar notification.cancel();

+0

Me gusta el 'notification.cancel();' porque se puede invocar desde la misma función que crea la notificación. – smfoote

+0

¡Gracias! ¡Funciona exactamente como yo lo quería! – Ryan

+0

Puede haber una actualización para la API, debe usar notification.close() ahora. – William

3

Podrá llamar al window.close() desde el interior de la página HTML de la notificación. Eso cerrará la notificación.

Para cerrar en un momento determinado, llamar a algo como setTimeout(function() { window.close(); }, timeInMicroseconds); debe ser efectivo.

+0

Gracias usted! Eso golpea el lugar! – Ryan

9
var notification = webkitNotifications.createNotification('images/icon-48x48.png',"This is  Title","Biswarup Adhikari Notification"); 
notification.show(); 
setTimeout(function(){ 
notification.cancel(); 
},2000); 

notificación Chrome se cerrará automáticamente después de 2000 seg mili o 2 seg.

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); 
} 

}

+0

por favor también explique qué cambió y por qué – blckbird

-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); 
     }); 
    } 

    } 
Cuestiones relacionadas