什麼是 PWA 通知行為與事件處理?
PWA 通知行為 (Notification Behavior) 是指 Progressive Web App 在發送推播通知時,針對通知的堆疊方式、提醒頻率與互動反應所進行的邏輯設定。透過 Service Worker 監聽 push 與 notificationclick 事件,開發者可以實作 Tag (通知分組) 以避免訊息洗版、Actions (動作按鈕) 提供直接操作選項,以及 Focus Tab (視窗導向) 確保使用者點擊後能精準導回已開啟的頁面。這套機制能精細控制通知從出現到消失的整個生命週期,是打造具備高品質使用者體驗(UX)的 PWA 通知系統的核心關鍵。
PWA 推播通知行為與設計建議
推播通知的品質直接影響了使用者的留存。小編認為,一套優秀的通知系統應包含精緻的視覺與流暢的後續事件處理。在前幾天的文章中,我們已經開箱了:
- [通知選項視覺配置][pwa-notification]
- [後端推播伺服器實作][pwa-push-notificatoin-server]
- [處理用戶端推播事件][pwa-push-notificatoin-client]
這篇文章小編將更進一步解說 通知行為實作 中常用的參數設定,以及收到通知後的自動化行為流程。
1 2 3 4 5 6 7 8 9 10 11
| { "//": "行為相關參數", "tag": "<String>", "renotify": "<Boolean>", "data": "<Anything>", "requireInteraction": "<boolean>", "silent": "<Boolean>",
"//": "視覺與行為參數", "actions": "<Array of Objects>" }
|
如果擔心覺得太抽象,歡迎先用以下連結的通知產生器先試玩:
https://tests.peter.sh/notification-generator/
這次的 Demo 連結如下也歡迎各位大大試玩看看:
https://linyencheng-push-notification.herokuapp.com/
通知行為參數解析:讓推播更具互動性
除了單純 UI 顯示相關的參數以外,接下來介紹一些會影響流程的相關參數:
1. 通知分組 (Tag)
Tag 這個設定是方便讓訊息不要一直疊加,只要有同個 Tag 的新訊息就會關掉舊的並用新的取代,要注意的是後面取代的通知預設都不會再次觸發裝置的聲音或是震動。
1 2 3 4 5 6
| const title = "Notification 1 of 3"; const options = { body: "With 'tag' of 'message-group-1'", tag: "message-group-1", }; registration.showNotification(title, options);
|
2. 重複通知 (Renotify)
因為單純使用 Tag 不會觸發聲音,但在聊天軟體等情境下需要再次「叮咚」提醒,所以在 tag 的基礎上增加了 renotify: true。
1 2 3 4 5 6
| const title = "Notification 2 of 2"; const options = { tag: "renotify", renotify: true, }; registration.showNotification(title, options);
|
3. 靜音通知 (Silent)
如果你只想發送靜態訊息而不打擾使用者,可以停用震動與鈴聲。
1 2 3
| const title = "Silent Notification"; const options = { silent: true }; registration.showNotification(title, options);
|
4. 互動設定 (Require Interaction)
需要等使用者手動點擊或關閉後才會消失。Android 通常沒問題,但某些桌面版系統(如 Windows 7)通知會自動消失,Windows 10 則會收納在通知列。
1 2 3 4 5 6
| const title = "Require Interaction Notification"; const options = { body: "With requireInteraction: true", requireInteraction: true, }; registration.showNotification(title, options);
|
5. 通知動作 (Actions)
透過 actions 讓通知帶有按鈕,點擊後觸發後續事件。
1 2 3 4 5 6 7 8 9 10
| self.registration.showNotification(data.title, { image: "https://linyencheng.github.io/img/404-bg.jpg", icon: "https://linyencheng.github.io/img/icon_wechat.png", vibrate: [200, 100, 200, 100, 400], body: "嗨,我是彥成,喜歡爬山的前端工程師,有個部落格叫前端三分鐘 :)", actions: [ { action: "know-more", title: "了解更多" }, { action: "fans", title: "按讚粉專" }, ], });
|

PWA 點擊事件處理:優化使用者歷程
點擊通知 (Notification Click)
透過 Service Worker 監聽 notificationclick 事件來開啟視窗或 Focus 到已開啟的 Tab。
Focus Tab 實作
如果 URL 已經在瀏覽器中開啟,就直接聚焦到該頁面,避免開啟重複的視窗。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| self.addEventListener("notificationclick", function (event) { const urlToOpen = new URL("/hello.html", self.location.origin).href;
const promiseChain = clients.matchAll({ type: "window", includeUncontrolled: true, }).then((windowClients) => { let matchingClient = null; for (let i = 0; i < windowClients.length; i++) { const windowClient = windowClients[i]; if (windowClient.url === urlToOpen) { matchingClient = windowClient; break; } } return matchingClient ? matchingClient.focus() : clients.openWindow(urlToOpen); });
event.notification.close(); event.waitUntil(promiseChain); });
|
合併通知與關閉事件
當使用者關閉通知時,可以監聽 notificationclose 做統計。此外,透過 registration.getNotifications() 可以在發送新通知前先關閉舊的,達成手動合併的效果。
完整實作範例
這次小編實作的 Service Worker 邏輯如下:
- 監聽
push 並根據標題內容顯示不同樣式的通知。 - 針對不同的
action(了解更多、按讚粉專)引導至不同連結。 - 若該連結已開啟,則使用
focus()。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| self.addEventListener("push", (event) => { const data = event.data.json(); if (data.title === "前端三分鐘") { self.registration.showNotification(data.title, { body: "嗨,我是彥成,點擊了解更多...", actions: [ { action: "know-more", title: "了解更多" }, { action: "fans", title: "按讚粉專" } ], }); } });
self.addEventListener("notificationclick", function (event) { const action = event.action; let url = "https://linyencheng.github.io/"; if (action === "fans") url = "https://www.facebook.com/linyencheng.tw";
event.notification.close(); event.waitUntil( clients.matchAll({ type: "window" }).then((windowClients) => { for (let client of windowClients) { if (client.url === url && "focus" in client) return client.focus(); } if (clients.openWindow) return clients.openWindow(url); }) ); });
|
FAQ:PWA 通知行為常見問題
Q1:通知中的 Actions 按鈕數量有限制嗎?
A:是的。雖然 API 支援多個按鈕,但大多數作業系統(如 Android 或 Windows)通常只會顯示前 2 到 3 個 按鈕。建議最重要的動作放在第一個。
Q2:為什麼我設定了 renotify: true 卻噴出錯誤?
A:renotify 屬性必須與 tag 屬性「同時存在」才有效。如果您只設定了 renotify 而沒有給予標籤,瀏覽器會因為不知道要重新提醒哪一組通知而報錯。
Q3:如何判斷使用者是點擊了通知主體還是點擊了 Actions 按鈕?
A:在 notificationclick 事件處理器中,您可以檢查 event.action。如果值為空字串,代表使用者點擊的是通知主體;如果包含特定字串,則代表點擊了對應的 Action 按鈕。
掌握了這套 通知行為實作 技巧,您將能更有彈性地與使用者互動。持續優化點擊後的導向邏輯,讓您的 PWA 更像是一個貼心的原生 App!
更多相關文章
想提升 Web App 的留存率嗎?本篇 PWA 教學深入解析「新增至主畫面 (A2HS)」實作細節。我們將解析 Manifest 配置、Service Worker 註冊到 beforeinstallprompt 程式實戰。前 150 字直接回答 A2HS 定義,助您掌握 PWA 安裝提示的設計技巧與最佳實務。
想讓您的 Web App 擁有原生 App 般的流暢體驗嗎?本篇 PWA 架構教學深入探討 App Shell Model。我們將分享如何透過靜態資源快取與動態資料分離,提升感知效能與可靠性。前 150 字直接回答 App Shell 定義,助您打造秒開且離線可用的高品質 Web App。
想讓您的 Web App 像原生 App 一樣具備快速啟動功能嗎?本篇 PWA 捷徑教學深入解析 App Shortcuts 實作技巧。我們將分享如何透過 Manifest 配置快速啟動選單、應用場景與各平台支援限制。前 150 字直接回答 App Shortcuts 定義,助您打造更高效率的高品質 Web App。
想提升 PWA 品質嗎?本篇 PWA 稽核教學深入解析 Lighthouse 使用建議。我們將解析 FMP、TTI 等效能指標,並提供包含 Webpack 優化、離線瀏覽實作與跨瀏覽器相容性的 10+ 個檢查清單。前 150 字直接回答稽核核心價值,助您打造高品質 Web App。
想提升 Web App 在離線環境下的體驗嗎?本篇 PWA 背景同步教學深入掌握 Periodic Background Sync API 實作技巧。我們將解析註冊機制、權限檢查到監聽處理,助您實現定期自動更新。前 150 字直接回答背景同步定義,讓您的 Progressive Web App 在網路不穩時依然強大。
喜歡這篇文章,請幫忙拍拍手喔 🤣