אפשר להשתמש בהתראות עשירות במחשב כדי ליידע את המשתמשים על אירוע חשוב. ההתראות מופיעות מחוץ לחלון הדפדפן. בתמונות המצב הבאות אפשר לראות איך ההתראות נראות ואיפה הן מוצגות, בהתאם לפלטפורמה.
ניתן ליצור את חלון ההתראה באמצעות קצת JavaScript, ואם רוצים, באמצעות דף HTML שארוז בתוך התוסף.
דוגמה
קודם כול, צריך להצהיר על ההרשאה notifications
במניפסט:
{
"name": "My extension",
"manifest_version": 2,
...
"permissions": [
"notifications"
],
...
// Note: Because of bug 134315, you must declare any images you
// want to use with createNotification() as a web accessible resource.
"web_accessible_resources": [
"48.png"
],
}
לאחר מכן, יש להשתמש באובייקט webkitNotifications
כדי ליצור התראות:
// Note: There's no need to call webkitNotifications.checkPermission().
// Extensions that declare the notifications permission are always
// allowed create notifications.
// Create a simple text notification:
var notification = webkitNotifications.createNotification(
'48.png', // icon url - can be relative
'Hello!', // notification title
'Lorem ipsum...' // notification body text
);
// Or create an HTML notification:
var notification = webkitNotifications.createHTMLNotification(
'notification.html' // html url - can be relative
);
// Then show the notification.
notification.show();
הפניית API
מומלץ לעיין במפרט הטיוטה של התראות במחשב.
תקשורת עם תצוגות אחרות
ניתן לתקשר בין התראה לבין תצוגות אחרות בתוסף באמצעות extension.getBackgroundPage ו-extension.getViews. לדוגמה:
chrome.extension.getBackgroundPage().doThing();
chrome.extension.getViews({type:"notification"}).forEach(function(win) {
win.doOtherThing();
});
דוגמאות נוספות
דוגמה פשוטה לשימוש בהתראות מופיעה בספרייה examples/api/notifications. בקטע דוגמאות אפשר לראות דוגמאות נוספות ולקבל עזרה בהצגת קוד המקור.
עיין גם במדריך ההתראות של html5rocks.com. מתעלמים מהקוד שקשור להרשאות. לא צריך להצהיר על ההרשאה "התראות".