การแจ้งเตือนที่สมบูรณ์ด้วย webKit

ใช้การแจ้งเตือนที่สมบูรณ์บนเดสก์ท็อปเพื่อแจ้งให้ผู้ใช้ทราบว่า มีเหตุการณ์สำคัญเกิดขึ้น การแจ้งเตือนจะปรากฏขึ้นนอกหน้าต่างเบราว์เซอร์ ตามที่แสดงในสแนปชอตต่อไปนี้ รายละเอียดการแสดงการแจ้งเตือนและตำแหน่งที่แสดงจะขึ้นอยู่กับแพลตฟอร์ม

การแจ้งเตือนใน Microsoft Windows

การแจ้งเตือนบน Mac OS X

การแจ้งเตือนใน Ubuntu Linux

คุณสร้างหน้าต่างการแจ้งเตือนโดยใช้ JavaScript เล็กน้อย และอาจมีหน้า HTML อยู่ภายในส่วนขยายด้วย (ไม่บังคับ)

ตัวอย่าง

ก่อนอื่น ให้ประกาศสิทธิ์ notifications ในไฟล์ Manifest ดังนี้

{
  "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 ละเว้นโค้ดที่เกี่ยวข้องกับสิทธิ์ ซึ่งไม่จำเป็นหากคุณประกาศสิทธิ์ "การแจ้งเตือน"