Notifications

Notifications are useful for delivering messages from dapplets to users using the extension's sidebar and storing them in the extension's overlay section.


Syntax#

Core.notify():#

  • Parameters
    • title – a notification title
    • teaser – a short message for the popup
    • message – (optional) a message to a user
  • Return value
    • Promise<void>

Core.alert():#

  • Parameters
    • message – a message to a user
  • Return value
    • Promise<void>

Core.confirm():#

  • Parameters
    • message – a message to a user
  • Return value
    • Promise<boolean>– the result of the user decision

The initial code for this example is in master.

  1. Open src/index.ts. Implement methods onNotification(), onConfirm(), and onAlert(). Use notify(), confirm() and alert() functions from Core. The functions are asynchronous.
1export default class Notification {
2 //...
3 onNotification = () =>
4 Core.notify({
5 title: "Notification's title",
6 message: 'This is a full notification message',
7 teaser: 'This is a teaser message',
8 })
9
10 onAlert = () => Core.alert('This is an alert')
11
12 onConfirm = async () => {
13 const isConfirmed = await Core.confirm('This is a confirm')
14 if (isConfirmed) {
15 Core.notify('Confirmed!')
16 } else {
17 Core.notify('Rejected!')
18 }
19 }
20}
  1. Add onNotification(), onConfirm(), and onAlert() functions to exec field in buttons.
1//...
2async activate() {
3 const { button } = this.adapter.exports
4 this.adapter.attachConfig({
5 PROFILE: () => [
6 button({
7 DEFAULT: {
8 label: 'Notify',
9 img: NOTIFICATION_IMG,
10 exec: this.onNotification,
11 },
12 }),
13 button({
14 DEFAULT: {
15 label: 'Alert',
16 exec: this.onAlert,
17 },
18 }),
19 button({
20 DEFAULT: {
21 label: 'Confirm',
22 exec: this.onConfirm,
23 },
24 }),
25 ],
26 })
27}
28//...

Here is the result code of the example: ex19-notifications.

Run the dapplet:

npm i
npm start

video

Contents