跳转到主内容

通知

创建OS(操作系统)桌面通知

进程:主进程

渲染进程通知

如果您想要在渲染进程中显示通知,你应该使用Web Notifications API

类: Notification

创建OS(操作系统)桌面通知

进程:主进程

Notification 是一个 EventEmitter

通过 options 来设置的一个新的原生 Notification

静态方法

Notification 类有以下静态方法:

Notification.isSupported()

返回 boolean - 当前系统是否支持桌面通知

new Notification([options])

  • options Object (可选)
    • title string(可选)- 通知标题,在通知窗口顶部显示。
    • subtitlestring (可选) 通知的副标题, 显示在标题下面。 macOS
    • body string (可选) - 通知的正文文本,将显示在标题或副标题下面。
    • silent boolean(可选)- 是否在显示通知时禁止操作系统发出通知提示音。
    • icon(string | NativeImage ) (可选) 用于在该通知上显示的图标。
    • hasReplyboolean (可选) 是否在通知中添加一个答复选项。 macOS
    • timeoutType string (可选) Linux Windows - 通知的超时持续时间 可以是 'default' 或 'never'.
    • replyPlaceholderstring (可选) 答复输入框中的占位符。 macOS
    • sound string (可选) 显示通知时播放的声音文件的名称。 macOS
    • urgency string (可选) Linux - 通知的紧急级别。 可以是 'normal', 'critical', 或者 'low'
    • actions NotificationAction[] (可选) macOS - 要添加到通知中的操作 请阅读 NotificationAction文档来了解可用的操作和限制。
    • closeButtonText string (可选) macOS - 自定义关闭按钮提示内容。 空字符串将替换为默认的本地化文本。
    • toastXml string (可选) Windows - 自定义的窗口通知描述可取代上面所有属性。 提供完全自定义的设计和通知行为。

实例事件

new Notification 创建的对象触发以下事件:

info

Some events are only available on specific operating systems and are labeled as such.

事件: 'show'

返回:

  • event Event

Emitted when the notification is shown to the user. Note that this event can be fired multiple times as a notification can be shown multiple times through the show() method.

事件: 'click'

返回:

  • event Event

在用户单击通知时触发。

事件: 'close'

返回:

  • event Event

当用户手动关闭通知时触发

当通知关闭后,这个事件不能保证在所有情况下都会触发。

On Windows, the close event can be emitted in one of three ways: programmatic dismissal with notification.close(), by the user closing the notification, or via system timeout. If a notification is in the Action Center after the initial close event is emitted, a call to notification.close() will remove the notification from the action center but the close event will not be emitted again.

事件: 'reply' macOS

返回:

  • event Event
  • replystring-用户在内联答复字段中输入的字符串.

当用户单击 hasReply: true 的通知上的 "Reply" 按钮时触发。

事件: 'action' macOS

返回:

  • event Event
  • index number - 已激活的操作的索引.

Event: 'failed' Windows

返回:

  • event Event
  • error string - 执行 show() 方法时遇到的错误

在创建和显示本地通知时发生错误时触发异常。

实例方法

Objects created with the new Notification() constructor have the following instance methods:

notification.show()

Immediately shows the notification to the user. Unlike the web notification API, instantiating a new Notification() does not immediately show it to the user. Instead, you need to call this method before the OS will display it.

如果以前已显示通知, 则此方法将忽略以前显示的通知,并创建具有相同属性的新通知

notification.close()

忽略这条通知

On Windows, calling notification.close() while the notification is visible on screen will dismiss the notification and remove it from the Action Center. If notification.close() is called after the notification is no longer visible on screen, calling notification.close() will try remove it from the Action Center.

实例属性

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

string 通过windows的 toastXML 自定义通知

播放声音

在 macOS 上, 您可以指定在显示通知时要播放的声音的名称。 除了自定义声音文件之外, 还可以使用任何默认声音 (系统首选项 > 声音)。 请确保声音文件是在应用程序包(例如, YourApp.app/Contents/Resources) 内存在副本, 或者是下列位置之一:

  • ~/Library/Sounds
  • /Library/Sounds
  • /Network/Library/Sounds
  • /System/Library/Sounds

有关详细信息, 请参见 NSSound 文档。