Notification Class

A notification shown to the user (https://notifications.spec.whatwg.org/#notification), by BrowsingContextOptions.ShowNotification once the notifications permission is granted. The host calls Notification.Click when the user activates the notification and Notification.Dismiss when the user closes it.

public sealed class Notification : EventTarget

Remarks

A non-persistent notification is created by the constructor: the error event is fired when the permission is not granted, and its show, click and close events are fired at the object. A persistent notification is created by ServiceWorkerRegistration.ShowNotification: it belongs to the service worker registration, whose active worker receives its notificationclick and notificationclose events (NotificationEvent), and ServiceWorkerRegistration.GetNotifications returns new objects that represent it. Only the persistent notifications have actions (at most Notification.MaxActions).

A notification whose tag is not empty replaces the notification of the same origin with the same tag (the close event of a replaced non-persistent notification is fired). The resources of the notifications (the icon, the image, the badge) are not fetched: the host fetches them if it displays them.

See also

Inheritance
EventTarget Notification

Constructors

NameDescription
Notification(string, NotificationOptions?)Creates a notification and shows it (https://notifications.spec.whatwg.org/#dom-notification-notification): the show event is fired once it is shown, or the error event when the permission is not granted.

Notification(string, NotificationOptions?)

Notification.cs

Creates a notification and shows it (https://notifications.spec.whatwg.org/#dom-notification-notification): the show event is fired once it is shown, or the error event when the permission is not granted.

public Notification(string title, NotificationOptions? options = null)

Parameters

title string
The title of the notification.
options NotificationOptions?
The options of the notification.

Exceptions

TypeErrorException
The constructor is called in a service worker, the options have actions, or renotify is set without tag.

See also

Properties

NameDescription
ActionsGets the actions of the notification (https://notifications.spec.whatwg.org/#dom-notification-actions): always empty for a non-persistent notification.
BadgeGets the absolute URL of the badge, or the empty string.
BodyGets the body of the notification.
DataGets a deserialization of the data of the notification (the same value each time for an object).
DirGets the direction of the text.
IconGets the absolute URL of the icon, or the empty string.
ImageGets the absolute URL of the image, or the empty string.
IsPersistentGets whether the notification is a persistent notification: it was shown by ServiceWorkerRegistration.ShowNotification.
IsShownGets whether the notification is shown: it is in the list of notifications of the browsing contexts.
LangGets the language of the text.
MaxActionsGets the maximum number of actions of a notification (https://notifications.spec.whatwg.org/#dom-notification-maxactions): 2, as in Chromium. The actions after the first two are ignored.
NavigateGets the absolute URL to navigate to when the notification is activated, or the empty string.
OnClickGets or sets the event handler of the click event.
OnCloseGets or sets the event handler of the close event.
OnErrorGets or sets the event handler of the error event.
OnShowGets or sets the event handler of the show event.
PermissionGets whether the user allows the origin of the current global object to show notifications (https://notifications.spec.whatwg.org/#dom-notification-permission), read from the permission store (BrowsingContextOptions.Permissions).
RenotifyGets whether the user is alerted again when the notification replaces another one.
RequireInteractionGets whether the notification stays until the user activates or closes it.
SilentGets whether the notification is silent, or null for the default of the device.
TagGets the tag of the notification.
TimestampGets the time of the notification, in milliseconds since the Unix epoch.
TitleGets the title of the notification.

Gets the actions of the notification (https://notifications.spec.whatwg.org/#dom-notification-actions): always empty for a non-persistent notification.

public IReadOnlyList<NotificationAction> Actions { get; }

Gets the absolute URL of the badge, or the empty string.

public string Badge { get; }

Gets the body of the notification.

public string Body { get; }

Gets a deserialization of the data of the notification (the same value each time for an object).

public object? Data { get; }

Gets the direction of the text.

public NotificationDirection Dir { get; }

Gets the absolute URL of the icon, or the empty string.

public string Icon { get; }

Gets the absolute URL of the image, or the empty string.

public string Image { get; }

IsPersistent

Notification.cs

Gets whether the notification is a persistent notification: it was shown by ServiceWorkerRegistration.ShowNotification.

public bool IsPersistent { get; }

Property value

bool

Gets whether the notification is shown: it is in the list of notifications of the browsing contexts.

public bool IsShown { get; }

Property value

bool

Gets the language of the text.

public string Lang { get; }

MaxActions

Notification.cs

Gets the maximum number of actions of a notification (https://notifications.spec.whatwg.org/#dom-notification-maxactions): 2, as in Chromium. The actions after the first two are ignored.

public static uint MaxActions { get; }

Gets or sets the event handler of the click event.

public Action<Event>? OnClick { get; set; }

Gets or sets the event handler of the close event.

public Action<Event>? OnClose { get; set; }

Gets or sets the event handler of the error event.

public Action<Event>? OnError { get; set; }

Gets or sets the event handler of the show event.

public Action<Event>? OnShow { get; set; }

Permission

Notification.cs

Gets whether the user allows the origin of the current global object to show notifications (https://notifications.spec.whatwg.org/#dom-notification-permission), read from the permission store (BrowsingContextOptions.Permissions).

public static NotificationPermission Permission { get; }

Gets whether the user is alerted again when the notification replaces another one.

public bool Renotify { get; }

RequireInteraction

Notification.cs

Gets whether the notification stays until the user activates or closes it.

public bool RequireInteraction { get; }

Gets whether the notification is silent, or null for the default of the device.

public bool? Silent { get; }

Gets the tag of the notification.

public string Tag { get; }

Gets the time of the notification, in milliseconds since the Unix epoch.

public ulong Timestamp { get; }

Gets the title of the notification.

public string Title { get; }

Methods

NameDescription
Click()Activates the notification, as when the user clicks it (https://notifications.spec.whatwg.org/#activating-a-notification): fires the click event of a non-persistent notification, and focuses its window unless the event is canceled. It must be called on the thread of the event loop of the browsing context. For a persistent notification, fires the notificationclick event at the active worker of its registration (from any thread).
Click(string)Activates an action of a persistent notification, as when the user clicks its button (https://notifications.spec.whatwg.org/#activating-a-notification): fires the notificationclick event, whose action is the name of the action, at the active worker of its registration.
Close()Closes the notification (https://notifications.spec.whatwg.org/#dom-notification-close): the close event of a non-persistent notification is fired. Closing a persistent notification fires no event.
Dismiss()Closes the notification, as when the user dismisses it (https://notifications.spec.whatwg.org/#closing-a-notification): fires the close event of a non-persistent notification (it must be called on the thread of the event loop of the browsing context), or the notificationclose event at the active worker of the registration of a persistent notification. BrowsingContextOptions.CloseNotification is not called.
RequestPermission(NotificationPermissionCallback?)Asks the user to allow the origin to show notifications (https://notifications.spec.whatwg.org/#dom-notification-requestpermission), with BrowsingContextOptions.RequestPermission, unless the permission is already granted or denied.

Click overloads

Activates the notification, as when the user clicks it (https://notifications.spec.whatwg.org/#activating-a-notification): fires the click event of a non-persistent notification, and focuses its window unless the event is canceled. It must be called on the thread of the event loop of the browsing context. For a persistent notification, fires the notificationclick event at the active worker of its registration (from any thread).

public void Click()

Click(string)

Notification.cs

Activates an action of a persistent notification, as when the user clicks its button (https://notifications.spec.whatwg.org/#activating-a-notification): fires the notificationclick event, whose action is the name of the action, at the active worker of its registration.

public void Click(string action)
Parameters
action string
The name of the action (NotificationAction.Action).
Exceptions
InvalidOperationException
The notification is not persistent.
ArgumentException
The notification has no action with this name.

Closes the notification (https://notifications.spec.whatwg.org/#dom-notification-close): the close event of a non-persistent notification is fired. Closing a persistent notification fires no event.

public void Close()

Closes the notification, as when the user dismisses it (https://notifications.spec.whatwg.org/#closing-a-notification): fires the close event of a non-persistent notification (it must be called on the thread of the event loop of the browsing context), or the notificationclose event at the active worker of the registration of a persistent notification. BrowsingContextOptions.CloseNotification is not called.

public void Dismiss()

RequestPermission(NotificationPermissionCallback?)

Notification.cs

Asks the user to allow the origin to show notifications (https://notifications.spec.whatwg.org/#dom-notification-requestpermission), with BrowsingContextOptions.RequestPermission, unless the permission is already granted or denied.

public static Task<NotificationPermission> RequestPermission(NotificationPermissionCallback? deprecatedCallback = null)

Parameters

deprecatedCallback NotificationPermissionCallback?
A callback invoked with the permission, before the task completes.

Returns

Task<NotificationPermission>

See also