Skip to main content

Notification

Create OS desktop notifications创建操作系统桌面通知

Process:进程:Main

Using in the renderer process在渲染器过程中使用

If you want to show Notifications from a renderer process you should use the HTML5 Notification API如果你想显示来自渲染器进程的通知,你应该使用HTML5通知API

Class: Notification

Create OS desktop notifications创建操作系统桌面通知

Process:进程:Main

Notification is an EventEmitter.是一个EventEmitter

It creates a new Notification with native properties as set by the options.它使用options设置的本机属性创建一个新Notification

Static Methods静态方法

The Notification class has the following static methods:Notification类具有以下静态方法:

Notification.isSupported()

Returns返回boolean - Whether or not desktop notifications are supported on the current system当前系统是否支持桌面通知

new Notification([options])

  • options Object (optional)
    • title string (optional) - A title for the notification, which will be shown at the top of the notification window when it is shown.通知的标题,显示时将显示在通知窗口的顶部。
    • subtitle string (optional) macOS - A subtitle for the notification, which will be displayed below the title.通知的副标题,将显示在标题下方。
    • body string (optional) - The body text of the notification, which will be displayed below the title or subtitle.通知的正文,将显示在标题或副标题下方。
    • silent boolean (optional) - Whether or not to emit an OS notification noise when showing the notification.显示通知时是否发出操作系统通知噪音。
    • icon (string | NativeImage) (optional) - An icon to use in the notification.要在通知中使用的图标。
    • hasReply boolean (optional) macOS - Whether or not to add an inline reply option to the notification.是否将内联回复选项添加到通知中。
    • timeoutType string (optional) Linux Windows - The timeout duration of the notification. Can be 'default' or 'never'.通知的超时持续时间。可以是“默认”或“从不”。
    • replyPlaceholder string (optional) macOS - The placeholder to write in the inline reply input field.要在内联回复输入字段中写入的占位符。
    • sound string (optional) macOS - The name of the sound file to play when the notification is shown.显示通知时要播放的声音文件的名称。
    • urgency string (optional) Linux - The urgency level of the notification. Can be 'normal', 'critical', or 'low'.通知的紧急程度。可以是“正常”、“严重”或“低”。
    • actions NotificationAction[] (optional) macOS - Actions to add to the notification. 要添加到通知中的操作。Please read the available actions and limitations in the NotificationAction documentation.请阅读NotificationAction文档中的可用操作和限制。
    • closeButtonText string (optional) macOS - A custom title for the close button of an alert. 警报的关闭按钮的自定义标题。An empty string will cause the default localized text to be used.空字符串将导致使用默认的本地化文本。
    • toastXml string (optional) Windows - A custom description of the Notification on Windows superseding all properties above. Windows上通知的自定义描述将取代上述所有属性。Provides full customization of design and behavior of the notification.提供通知的设计和行为的完全自定义。

Instance Events实例事件

Objects created with new Notification emit the following events:使用new Notification创建的对象会发出以下事件:

Note: Some events are only available on specific operating systems and are labeled as such.某些事件仅在特定的操作系统上可用,并被标记为这样。

Event: 'show'

Returns:返回:

  • event Event

Emitted when the notification is shown to the user, note this could be fired multiple times as a notification can be shown multiple times through the show() method.当通知显示给用户时发出,请注意,这可能会被多次触发,因为通知可以通过show()方法显示多次。

Event: 'click'

Returns:返回:

  • event Event

Emitted when the notification is clicked by the user.当用户单击通知时发出。

Event: 'close'

Returns:返回:

  • event Event

Emitted when the notification is closed by manual intervention from the user.当用户手动干预关闭通知时发出。

This event is not guaranteed to be emitted in all cases where the notification is closed.不能保证在关闭通知的所有情况下都会发出此事件。

Event: 'reply' macOS

Returns:返回:

  • event Event
  • reply string - The string the user entered into the inline reply field.用户在内联回复字段中输入的字符串。

Emitted when the user clicks the "Reply" button on a notification with hasReply: true.当用户单击hasReply:true通知上的“回复”按钮时发出。

Event: 'action' macOS

Returns:返回:

  • event Event
  • index number - The index of the action that was activated.已激活的操作的索引。

Event: 'failed' Windows

Returns:返回:

  • event Event
  • error string - The error encountered during execution of the show() method.在执行show()方法期间遇到的错误。

Emitted when an error is encountered while creating and showing the native notification.在创建和显示本机通知时遇到错误时发出。

Instance Methods实例方法

Objects created with new Notification have the following instance methods:使用new Notification创建的对象具有以下实例方法:

notification.show()

Immediately shows the notification to the user, please note this means unlike the HTML5 Notification implementation, instantiating a new Notification does not immediately show it to the user, you need to call this method before the OS will display it.立即向用户显示通知,请注意,这意味着与HTML5通知实现不同,实例化一个new Notification不会立即显示给用户,您需要在操作系统显示它之前调用此方法。

If the notification has been shown before, this method will dismiss the previously shown notification and create a new one with identical properties.如果以前显示过通知,此方法将取消以前显示的通知,并创建一个具有相同属性的新通知。

notification.close()

Dismisses the notification.取消通知。

Instance Properties实例属性

notification.title

A string property representing the title of the notification.表示通知标题的string属性。

notification.subtitle

A string property representing the subtitle of the notification.一个string属性,表示通知的副标题。

notification.body

A string property representing the body of the notification.表示通知正文的string属性。

notification.replyPlaceholder

A string property representing the reply placeholder of the notification.一个string属性,表示通知的答复占位符。

notification.sound

A string property representing the sound of the notification.表示通知声音的string属性。

notification.closeButtonText

A string property representing the close button text of the notification.一个string属性,表示通知的关闭按钮文本。

notification.silent

A boolean property representing whether the notification is silent.表示通知是否为静默的boolean属性。

notification.hasReply

A boolean property representing whether the notification has a reply action.一个boolean属性,表示通知是否具有回复操作。

notification.urgency Linux

A string property representing the urgency level of the notification. 一个string属性,表示通知的紧急级别。Can be 'normal', 'critical', or 'low'.可以是“正常”、“严重”或“低”。

Default is 'low' - see NotifyUrgency for more information.默认值为“低”-有关详细信息,请参阅NotifyUrgency

notification.timeoutType Linux Windows

A string property representing the type of timeout duration for the notification. 一个string属性,表示通知的超时持续时间类型。Can be 'default' or 'never'.可以是“默认”或“从不”。

If timeoutType is set to 'never', the notification never expires. 如果timeoutType设置为“never”,则通知永远不会过期。It stays open until closed by the calling API or the user.它一直处于打开状态,直到被调用API或用户关闭为止。

notification.actions

A NotificationAction[] property representing the actions of the notification.NotificationAction[]属性,表示通知的操作。

notification.toastXml Windows

A string property representing the custom Toast XML of the notification.一个string属性,表示通知的自定义Toast XML。

Playing Sounds播放声音

On macOS, you can specify the name of the sound you'd like to play when the notification is shown. 在macOS上,您可以指定在显示通知时要播放的声音的名称。Any of the default sounds (under System Preferences > Sound) can be used, in addition to custom sound files. 除了自定义声音文件外,还可以使用任何默认声音(在“系统首选项”>“声音”下)。Be sure that the sound file is copied under the app bundle (e.g., YourApp.app/Contents/Resources), or one of the following locations:请确保将声音文件复制到应用程序捆绑包下(例如,YourApp.app/Contents/Resources),或以下位置之一:

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

See the NSSound docs for more information.有关更多信息,请参阅NSSound文档。