Show a Pop-up Notification Using Web API
Introduction
This document describes how to display a simple pop-up notification in Studio Pro.
Prerequisites
This how-to uses the results of Get Started with the Web Extensibility API. Complete that how-to before starting this one.
Showing a Notification
With the notifications API, you can show a pop-up notification when your extension loads. The notification disappears after five seconds. To do this, follow these steps:
-
Create an
assetsfolder under yoursrcfolder. -
Find an icon you want to use in your notification and copy it into the
assetsfolder. This example uses the filecheck.png. -
Create an
Icons.tsfile inside that sameassetsfolder. -
Add the following code to the
Icons.tsfile, replacingcheck.pngwith the name of your icon and using an appropriate name in theimport,IIcons, andexportstatements.import Check from "./check.png"; interface IIcons { Check: string; } export default { Check } as IIcons; -
Create an
images.d.tsfile inside theassetsfolder. This is adeclarationfile, as indicated by thedfile extension. -
Add the line
declare module "*.png";to theimages.d.tsfile. This tells TypeScript that any import ending in .png should be treated as a module. This allows TypeScript to handle .png files correctly when you import them in your code and use images in your extensions. -
Replace your
src/main/index.tsfile with the following, using the appropriate icon name in place ofCheck:import { IComponent, getStudioProApi } from "@mendix/extensions-api"; import Icons from "../assets/Icons"; export const component: IComponent = { async loaded(componentContext) { const studioPro = getStudioProApi(componentContext); const notificationsApi = studioPro.ui.notifications; await notificationsApi.show({ title: "Extension Loaded", message: "The extension was successfully loaded", displayDurationInSeconds: 5, icon: { relativePath: Icons.Check, componentName: "extension/myextension" } }); } }This code does the following:
- It uses the
notificationsApifromstudioPro.ui.notificationsto access the notifications API. - It implements a
loadedmethod, which calls theshowmethod to display a pop-up notification for five seconds with the titleExtension Loaded, a message, and thecheck.pngicon you set up earlier. For more information, see the Full Reference for Show Method section.
- It uses the
Now, when the extension loads, your notification shows in the upper-right corner of Studio Pro:
Full Reference for Show Method
The show method has the following parameters:
title– the title of the notificationmessage– the text content of the notificationdisplayDurationInSeconds– an optional duration (in seconds) for the notification to remain visible; if no duration is provided, the pop-up remains indefinitely until the user removes iticon– an optional icon that displays inside the notification
Extensibility Feedback
If you would like to provide additional feedback, you can complete a short survey.
Any feedback is appreciated.