迁移 Chrome 扩展 v3 通知

时间:2021-01-02 23:08:03

标签: google-chrome-extension notifications migration manifest

我正在尝试将我的 Google Chrome 扩展程序清单 v2 迁移到 v3。 但是当我尝试创建通知时出现错误。

Service Worker 中的错误:

extensions::imageUtil:11
Uncaught ReferenceError: Image is not defined
at loadImageData (extensions::imageUtil:11)
at Object.loadAllImages (extensions::imageUtil:74)
at replaceNotificationOptionURLs (extensions::notifications:89)
at extensions::notifications:115
at service_workers.js:2

我的 service_workers.js:

self.addEventListener('activate', (event) => {
    chrome.notifications.create({
        type:     'basic',
        iconUrl:  'assets/images/icon_128.png',
        title:    'MyTitle',
        message:  'MyMessage!'
    });
});

我的 manifest.json

{
 "name": "My Extension",
 "description": "...",
 "version": "0.1",
 "manifest_version": 3,
 "permissions": [
   "storage",
   "alarms",
   "notifications",
   "activeTab",
   "tabs"
 ],
 "host_permissions": [
   "http://localhost:3000/api/data"
 ],
 "background": {
   "service_worker": "service_workers.js"
 },
 "action": {
   "default_title": "ExtensionPopup",
   "default_popup": "popup/popup.html"
 },
 "icons": {
   "128": "assets/images/icon_128.png"
 },
 "content_security_policy": {
   "extension_pages": "script-src 'self'; object-src 'self' "
 }
}

我还尝试为 iconUrl 提供 chrome.runtime.getURL 或 base64 数据,但没有成功。 我正在 Chrome Canary V89 上进行测试。

我错过了什么吗? 提前谢谢你!

1 个答案:

答案 0 :(得分:3)

根据来自devlin@chromium.org for Manifest V3的文档。 Extension notifications 替换为 web notifications

我的通知是这样工作的,但我不知道这是否是正确的方法:

registration.showNotification(title, {
    body: message,
    data: UUID,
    icon: logoUrl,
    message,
    actions: [
      { action: 'Open', title: 'Open' },
      { action: 'Close', title: 'Close' }
    ]
  })

self.addEventListener('notificationclick', function (event) {
  if (event.action === 'Open') {
    // something(event.notification.data) // UUID
  }
  event.notification.close()
})