The purpose of web notifications is to allow executing actions in real time so that the server sends content to the browser without being solicited by the client.
Therefore, the user doesn't need to refresh the web browser to get the information in real time. For example: online chat/messaging systems and monitoring consoles.
To use Web Notifications, you must configure the Web User Experience property to "Smooth".
Web Notifications API
The Web Notifications API consists of the WebNotification external object and the NotificationInfo SDT.
WebNotification External Object
An external object called WebNotification exposes all the methods and properties needed for this functionality.
WebNotification external object methods
||Most common use case
|WebNotification.Notify(NotificationInfo sdtNotification): Numeric
||Send a notification to the user who originated the action. It is valid during the entire session.
||Give feedback to a user when a submitted procedure has finished - if the submit action has been executed from the user's session.
|WebNotification.NotifyClient(String clientId, NotificationInfo sdtNotification ): Numeric
||Send a notification to a given user.
||To implement a chat, only some users will receive the notification.
||Send a notification to all connected users.
||To send an alert to all users.
WebNotification external object properties
||Get the ClientId of the current session.
WebNotification methods - Error Codes
||Could not start WebSocket Server
||WebSocket Session not found
||WebSocket Session is closed or invalid
||Message could not be delivered to client
NotificationInfo Structured Data Type
The NotificationInfo SDT is provided to specify the notification information that will be sent to the above methods (Notify, NotifyClient, BroadCast) of the WebNotification external object.
||By identifying the notification, the developer is able to specify which control has to capture it.
||By specifying this property, the developer can determine which object has to listen to this notification (especially in case of Broadcast)
||Message of the notification.
Example: How to send a notification to everyone (Broadcast)
Suppose that you are implementing a messaging system where you need to notify any post made by a user. In this case, we load the NotificationInfo SDT with the data of the post message. Next, we use the WebNotification external object to broadcast the message.
//&NotificationInfo is NotificationInfo SDT data type. &PostId is the identifier of the post.
//The following is an SDT where data to be notified is loaded. You can send any data because it is sent in json format. First load an SDT with this data.
&commentNotificationInfo.PostId = &PostId
&commentNotificationInfo.PostCommentContent = &PostCommentContent
//Now assign the data to be sent to the NotificationInfo SDT in json format.
&webnotification.Broadcast(&NotificationInfo) //&webnotification is WebNotification external object data type
Example: How to receive the notification
In the web object that will receive the notification, use the OnMessage event that receives a variable based on the NotificationInfo SDT data type. The notification information will be processed there.
for each line
//processs the notification data
Example: How to send a notification to a specific client
Consider a system where only a specific client has to be notified. This client is identified by using the ClientId property of the WebNotification external object.
Below is the web panel that receives the notification. In the start event, we save the &clientId so we can identify the session afterwards.
&clientId = &webnotification.ClientId
//save the information of the client: RegisteredClientId = &clientId//
In the procedure that sends the notification, we get the &ClientId (that may have been saved in the database). The Notifyclient method is executed by passing the &ClientId as a parameter, so only that client will receive the message.
&ClientId = RegisteredClientId
&NotificationInfo.Message="El mensaje " + &cont.ToString() + "cliente " + &ClientId
Note: As since GeneXus 15 upgrade 3, you can use the ClientInformation.Id Property instead of using &webnotification.ClientId.
As in the previous example, the web panel that receives the message has to implement the OnMessage Event.
//Do something with the ¬ificationInfo
Remember that the broadcast method should only be executed when the message is public because it is sent to all the browser instances which are subscribed to the notification event.
See Web Notifications and Progress UC requirements
Here is a list of Web Browsers that support WebSocket and, therefore, Web Notifications. Note that Web Notifications can be received on Smart Device Apps using the Component Domain, taking into account the OS version indicated in the link.
- Warning Message: "Warning: WebNotifications are not supported with "Web User Experience": "Previous versions". You must use Smooth.". Web Notifications are only supported with Web User Experience property : "Smooth".
Html5 Web Notifications User Control
HowTo:Develop a messaging web page
HowTo: Receiving and processing a notification message from an external app