Configuration
After installing the add-on in your application, create notification types, configure notification channels and assign roles to users.
The add-on provides two notification channels out-of-the-box: the in-app channel shows notifications in the application UI, the email channel sends emails. You can define your own channels by creating beans implementing the NotificationChannel
interface.
Notification Types
The add-on does not provide notification types by default. You should create a set of NotificationType
instances and register them via NotificationTypesRepository
when initializing the application. The following code can be added to any bean of the application, for example to the main application class annotated with @SpringBootApplication
:
@PostConstruct
public void postConstruct() {
notificationTypesRepository.registerTypes(
new NotificationType("info", "INFO_CIRCLE"), (1)
new NotificationType("warn", "WARNING")
);
}
1 | Creates a notification type with the unique name info and icon name INFO_CIRCLE . |
Localized messages for the notification types can be added with the following template:
io.jmix.notifications/NotificationType.<name>=<value>
In case of the example above:
io.jmix.notifications/NotificationType.info=Info
io.jmix.notifications/NotificationType.warn=Warn
In-App Channel
To show notifications sent to the in-app channel, add a notification indicator component to an application screen. Below is an example of adding the indicator to the main screen with side menu:
<window xmlns="http://jmix.io/schema/ui/window"
xmlns:ntf="http://jmix.io/schema/notifications/ui"> (1)
<layout>
<cssLayout id="horizontalWrap"
stylename="jmix-drawer-layout">
<drawer id="drawer" expandOnHover="true">
<!-- ... -->
<ntf:notificationsIndicator id="ntfIndicator"/> (2)
1 | Declares the notifications namespace in the root element. |
2 | Adds a visual component that displays a counter of the unread notifications. |
Email Channel
To send notifications by email, add the Email add-on to the application. After that, you will be able to select the email channel in the notification editor window.
The recipient email address is taken from a property of the user object. By default, the add-on assumes that email is stored in the email
property. If you want to use another property, define its name in the application.properties
file, for example:
jmix.notifications.user-email-property-name=emailAddress
Besides, you can create a Spring bean implementing the UserEmailResolver
interface to define a more complex logic of obtaining the user’s email address, for example:
@Component("sample_GetEmailService")
public class GetEmailService implements UserEmailResolver {
@Nullable
@Override
public String resolveEmail(UserDetails user) {
return user.getUsername() + "@company.com";
}
}