Show a Pop-up Notification Using Web API
Introduction
This how-to describes how to show a simple pop-up notification in Studio Pro.
Prerequisites
This how-to uses the results of Get Started with the Web Extensibility API. Make sure to 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 will disappear after five seconds. To do this, follow the steps below:
-
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 enables TypeScript to handle .png files correctly when you import them in your code and allows you to 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 allow you to use the notifications API. - It implements a
loadedmethod, which calls theshowmethod to show 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 below.
- It uses the
Now, when the extension loads, your notification will show in the top-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 will remain indefinitely until the user removes it themselvesicon– an optional icon that is displayed inside the notification
Extensibility Feedback
If you would like to provide additional feedback, you can complete a small survey.
Any feedback is appreciated.