Notifications
Learn how to show custom notifications.
Extensions that render their own UI, such as editing extensions, can show toast notifications to users. You can use these notifications to communicate important information, such as when a long-running task has finished or when an error has occurred.
This tutorial explains how to show notifications to users.

Step 1: Choose a notification type

An extension can display two types of notifications:
  • info
  • error
This is an example of an info notification:
This is an example of an error notification:
The main differences are that error notifications are dismissible and remain on the screen for a longer period of time.

Step 2: Define the notification text

User-facing strings, including the text that appears in a notification, can't be embedded in the source code of the extension. This is because Canva must be able to:
  • Verify that user-facing strings meet the UX guidelines.
  • Localize user-facing strings for different regions.
To define user-facing strings, you must create a JSON file that contains all of the strings and upload this file to the Developer Portal, via the extension's Translations field.
The following snippet demonstrates the required structure of the JSON file:
1
{
2
"en": [
3
{
4
"key": "greeting",
5
"value": "Hello! I hope you're having a lovely day.",
6
"translatorNote": "Label for notification when user opens the app."
7
}
8
]
9
}
Copied!
To learn more about the structure of this JSON file, see Translations.

Step 3: Show a notification

To show a notification, call the showNotification method:
1
const canva = window.canva.init();
2
3
canva.onReady(async (opts) => {
4
canva.showNotification("greeting", "info");
5
});
Copied!
This method accepts two arguments:
  • The key of a string defined in the Translations file.
  • The notification type. (The default value is "info".)
You can call the showNotification method at any point in the lifecycle of the extension.

Example

index.js

1
const canva = window.canva.init();
2
3
canva.onReady(async () => {
4
renderControls();
5
});
6
7
canva.onControlsEvent(async (opts) => {
8
if (!opts.message.commit) {
9
return;
10
}
11
12
if (opts.message.controlId === "showInfo") {
13
canva.showNotification("infoExample", "info");
14
}
15
16
if (opts.message.controlId === "showError") {
17
canva.showNotification("errorExample", "error");
18
}
19
});
20
21
function renderControls() {
22
const controls = [
23
canva.create("button", {
24
id: "showInfo",
25
label: "Show info",
26
}),
27
canva.create("button", {
28
id: "showError",
29
label: "Show error",
30
}),
31
];
32
33
canva.updateControlPanel(controls);
34
}
Copied!

labels.json

1
{
2
"en": [
3
{
4
"key": "infoExample",
5
"value": "This is an example of an 'info' notification.",
6
"translatorNote": "Label to demonstrate an 'info' notification."
7
},
8
{
9
"key": "errorExample",
10
"value": "This is an example of an 'error' notification.",
11
"translatorNote": "Label to demonstrate an 'error' notification."
12
}
13
]
14
}
Copied!
Last modified 1mo ago