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])
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
Eventreply
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
Eventindex
number -The index of the action that was activated.已激活的操作的索引。
Event: 'failed' Windows
Returns:返回:
event
Eventerror
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 立即向用户显示通知,请注意,这意味着与HTML5通知实现不同,实例化一个new Notification
does not immediately show it to the user, you need to call this method before the OS will display it.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文档。