0
0

Notification Hub Reference Manual

Docs
Docs EInnovator Posted 10 Jun 20

Java / Spring Boot Client Library for Notifications Hub

Overview

Getting Started

External Configuration

Table below provides a quick summary for the configuration options:

PropertyTypeDefault ValueRequiredDescription
serverStringhttp://localhost:2100Yes*Server URL
amqpAmqpConfiguration YesAMQP Configuration
templatesTemplatesConfiguration NoTemplates Configuration
connectionConnectionConfiguration NoConnection Settings
registrationNotificationsRegistration NoApplication Registration Descriptor
enabledBoolean NoTrue if remote server is configured

Connecting to the Server

Example: Configuring Server Access and AMQP Queue

notifications:
  server : "https://notifications.mydomain.com"
  amqp:
    notifications-queue: myapp

Application Registration

NotificationsRegistration:

Table below provides a quick summary for the configuration options:

PropertyTypeDefault ValueRequiredDescription
autobooleanfalseNoTrue to auto register application on startup.
applicationApplication YesApplication descriptor.
typesList<NotificationType> NoNotificationTypes to Register
templatesList<Template> NoList of Templates to Register
propertiesMap<String, Object> NoServer Properties to set.

Example: Configuring Registration of a custom Notification Template and NotificaitonType

notifications:
  server : "https://notifications.mydomain.com"
  amqp:
    notifications-queue: myapp
  templates:
    uri: "https://cdn.mydomain.com/templates/myapp"  
    app: notifications/app
    sms: notifications/sms
    mail: notifications/mail
  registration:
    templates:
      - name: "Customer Invitation"
        medium: EMAIL
        category: SYSTEM
        uri: "invitation.html"
        subject: "Invitation from ${principal.name} to Join the ACME Community"
    types:
    - source-type: Customer
      action-type: create
      label: "Do you want to receive notification when a Customer is Created ?"
      category: Customer
      subcategory: CRUD
      admin: false
      mail-template:
        name: "Customer Created"
        uri: customer.html
        subject: "A new Customer has been Created"

Programmatic Configuration

Notifications API

Building Events

Example:

public static final String CUSTOMER = "Customer";

public Event makeCustomerEvent(Customer customer, String actionType, String... targets) {
	Source source = (Source)new Source().withType(CUSTOMER).withDetails(MappingUtils.toMap(customer));
	Action action = new Action(actionType);
	return new Event()
		.withSource(source)
		.withAction(action)
		.withPrincipal(PrincipalDetails.principal())
		.withTargets(TargetParser.makeTargets(targets));
}

Publishing Events

Example: Publish Event to Users with Global Role

public void publishCreateEvent(Customer customer) {
	Event event = makeCustomerEvent(ActionType.CREATED);
	client.publishEvent(event, ".customer-manager");
}

Notification Lookup

Example: Getting Recent User Notifications

Page<Notification> notifications = client.listNotifications(null, new PageRequest(0,20), null);

Example: Couting User Notifications

NotificationFilter filter = (NotificationFilter)new NotificationFilter().withType(CUSTOMER + ":" + ActionType.CREATE);
Long n = client.countNotifications(filter, null);

Event Handling API and Annotations

NotificationListener Interface

Example: Implementing a NotificationListener

@NotificationSelector(source=@SourceSelector(CUSTOMER), action=@ActionSelector(ActionType.CREATE))
public class CustomerCreationListener implements NotificationListener {
	@Override
	public void onNotification(Notification notification) throws Exception {
		Customer customer = MappingUtil.convert(notification.getSource().getDetails(), Customer.class);
		//...
	}	
}

POJO Listeners

Example: POJO Event Listener

@NotificationSelector(source=@SourceSelector(CUSTOMER), action=@ActionSelector(ActionType.CREATE))
public class CustomerCreationListener implements NotificationListener {

	@NotificationSelector(action=@ActionSelector(ActionType.CREATE))			
	public void onCustomerCreate(Customer customer) {
		//...
	}

	@NotificationSelector(action=@ActionSelector(ActionType.UPDATE))			
	public void onCustomerUpdate(Customer customer, Notification notification) {
		String username = notification.getPrincipalDetails().getId();
		//...
	}

	@NotificationSelector(action=@ActionSelector(ActionType.DELETE))			
	public void onCustomerDelete(Customer customer) {
		//...
	}

	@NotificationSelector(action=@ActionSelector(CUSTOMER_LEAD_CREATED))			
	public void onCustomerLeadCreation(Customer customer, Notification notification) {
		//...
	}
}

Preferences API

Preferences ThemeResolver

Rest Endpoints

This section describes the REST endpoints automatically setup the the einnovator-notifications-starter.

Table below provides a quick summary:

NotificationsRestController:

MethodPathQuery ParametersRequest BodyResponse BodyStatusLogSecurityDescription
GET/api/notificationNotificationFilter PageOKnotificationsowner
ADMIN
CLIENT
Get Notifications (of Principal, by default)
DELETE/api/notification/NotificationOptions NO_CONTENTdeleteowner
ADMIN
CLIENT
Delete Notification

PreferencesRestController:

MethodPathQuery ParametersRequest BodyResponse BodyStatusLogSecurityDescription
GET/api/preferencekey Preference (JSON)NO_CONTENTgetPreferenceowner
ADMIN
CLIENT
Update Preference
POST PUT/api/preferencekey value{? Preference (JSON)} NO_CONTENTsetPreferenceowner
ADMIN
CLIENT
Create Preference
DELETE/api/preference NO_CONTENTremovePreferenceowner
ADMIN
CLIENT
Delete Preferece
Comments and Discussion

Content