addon.notifications

Available in userscripts
Available in persistent scripts ✔️
Available in popup scripts
Required manifest permissions notifications

Description

Allows addons to create browser notifications. This API slightly differs from the browser.notifications API extensions can use.

Example

General example

addon.notifications.create({
  type: "basic",
  title: "Hello from notification!",
  iconUrl: "/images/icon.png",
  message: "Hi!",
  buttons: [
    {
      title: "Button 1",
    },
    {
      title: "Button 2",
    },
  ],
});

addon.notifications.addEventListener("click", function(event) {
  console.log("User clicked notification with ID " + event.detail.id);
});
addon.notifications.addEventListener("closed", function(event) {
  console.log("User closed notification with ID " + event.detail.id);
});
addon.notifications.addEventListener("buttonclick", function(event) {
  console.log(`User clicked button ${event.detail.buttonIndex} of notification ${event.detail.id}`);
});

Properties

addon.notifications.muted

Type Boolean
Nullable No

Whether Scratch Addons is currently muted or not.

Methods

addon.notifications.create

Parameter Type Required Description
options NotificationOptions Yes Unlike the browser.notifications API, specifying buttons or requireInteraction does not throw in Firefox. Instead, the values are ignored.
Return value Promise<String | null>

Creates a notification.
The notification will not be created if Scratch Addons is muted.

addon.notifications.update

Parameter Type Required Description
notificationId String Yes The ID of the notification to update
options NotificationOptions Yes Unlike the browser.notifications API, specifying buttons or requireInteraction does not throw in Firefox. Instead, the values are ignored.
Return value Boolean

Updates an existing notification, given its ID.
The returned promise resolves to a boolean that indicates whether the update succeeded.

addon.notifications.clear

Parameter Type Required Description
notificationId String Yes The ID of the notification to clear
Return value Boolean

Clears an existing notification, given its ID.
The returned promise resolves to a boolean that indicates whether the clearing succeeded.

addon.notifications.getAll

Return value Object[]

Gets all existing notifications for the addon.
Based on browser.notifications.getAll().

Events

click

Event detail property Type Description
id String The ID of the notification that was clicked

Fires when any existing notification is clicked.

close

Event detail property Type Description
id String The ID of the notification that was closed

Fires when any existing notification is closed.

buttonclick

Event detail property Type Description
id String The ID of the notification where a button was clicked
buttonIndex Number Index of the button that was clicked (first button being index 0)

Fires when a notification button is clicked.
This will never fire in Firefox.


Last updated at 12 July 2021 by Hans5958 on commit d07183b. Improve this page.