Notification
建立作業系統桌面通知
程序:主程序
如果您想從渲染程序顯示通知,您應該使用 web Notifications API
類別:Notification
建立作業系統桌面通知
程序:主程序
Notification
是一個 EventEmitter。
它使用 options
設定的原生屬性建立新的 Notification
。
靜態方法
Notification
類別具有以下靜態方法
Notification.isSupported()
傳回 boolean
- 桌面通知在目前系統上是否受到支援
new Notification([options])
實例事件
使用 new Notification
建立的物件會發射以下事件
有些事件僅在特定作業系統上可用,並標記為如此。
事件:'show'
傳回
event
事件
當通知顯示給使用者時發射。請注意,此事件可能會多次觸發,因為通知可以透過 show()
方法多次顯示。
事件:'click'
傳回
event
事件
當使用者點擊通知時發射。
事件:'close'
傳回
event
事件
當通知被使用者手動介入關閉時發射。
此事件不保證在所有通知關閉的情況下都會發射。
在 Windows 上,close
事件可以透過以下三種方式之一發射:透過 notification.close()
以程式方式解除、由使用者關閉通知,或透過系統逾時。如果通知在初始 close
事件發射後仍在行動作業中心中,則呼叫 notification.close()
將從行動作業中心移除通知,但不會再次發射 close
事件。
事件:'reply' macOS
傳回
event
事件reply
字串 - 使用者在內嵌回覆欄位中輸入的字串。
當使用者點擊具有 hasReply: true
的通知上的「回覆」按鈕時發射。
事件:'action' macOS
傳回
event
事件index
數字 - 已啟動動作的索引。
事件:'failed' Windows
傳回
event
事件error
字串 - 在執行show()
方法期間遇到的錯誤。
當在建立和顯示原生通知時遇到錯誤時發射。
實例方法
使用 new Notification()
建構函式建立的物件具有以下實例方法
notification.show()
立即向使用者顯示通知。與 web notification API 不同,實例化 new Notification()
不會立即向使用者顯示通知。相反地,您需要先呼叫此方法,作業系統才會顯示它。
如果通知先前已顯示,此方法將解除先前顯示的通知,並使用相同的屬性建立一個新的通知。
notification.close()
解除通知。
在 Windows 上,當通知在螢幕上可見時呼叫 notification.close()
將解除通知並從行動作業中心移除它。如果在通知不再於螢幕上可見後呼叫 notification.close()
,則呼叫 notification.close()
將嘗試從行動作業中心移除它。
實例屬性
notification.title
代表通知標題的 string
屬性。
notification.subtitle
代表通知副標題的 string
屬性。
notification.body
代表通知內文的 string
屬性。
notification.replyPlaceholder
代表通知回覆預留位置的 string
屬性。
notification.sound
代表通知聲音的 string
屬性。
notification.closeButtonText
代表通知關閉按鈕文字的 string
屬性。
notification.silent
代表通知是否靜音的 boolean
屬性。
notification.hasReply
代表通知是否具有回覆動作的 boolean
屬性。
notification.urgency
Linux
代表通知緊急程度的 string
屬性。可以是 'normal'、'critical' 或 'low'。
預設值為 'low' - 請參閱 NotifyUrgency 以取得更多資訊。
notification.timeoutType
Linux Windows
代表通知逾時持續時間類型的 string
屬性。可以是 'default' 或 'never'。
如果 timeoutType
設定為 'never',則通知永遠不會過期。它會保持開啟狀態,直到被呼叫 API 或使用者關閉。
notification.actions
代表通知動作的 NotificationAction[] 屬性。
notification.toastXml
Windows
代表通知自訂 Toast XML 的 string
屬性。
播放聲音
在 macOS 上,您可以指定要在顯示通知時播放的聲音名稱。除了自訂聲音檔案外,可以使用任何預設聲音(在「系統偏好設定」>「聲音」下)。請確保聲音檔案已複製到應用程式套件 (例如,YourApp.app/Contents/Resources
) 或以下位置之一
~/資源庫/Sounds
/資源庫/Sounds
/網路/資源庫/Sounds
/系統/資源庫/Sounds
請參閱 NSSound
文件以取得更多資訊。