WebNotifications external object

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.

WebNotificationsEO15

WebNotification external object methods

Method Description 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.
WebNotification.Broadcast(NotificationInfo sdtNotification) Send a notification to all connected users. To send an alert to all users.

WebNotification external object properties

WebNotification.ClientId: String Get the ClientId of the current session.
ErrCode Error Code
ErrDescription Error Message

WebNotification methods - Error Codes

0 OK
1 Could not start WebSocket Server
2 WebSocket Session not found
3 WebSocket Session is closed or invalid
4 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.

NotificationInfoSDT15

Id By identifying the notification, the developer is able to specify which control has to capture it.
Object  By specifying this property, the developer can determine which object has to listen to this notification (especially in case of Broadcast)
Message  Message of the notification.

Examples

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.

&NotificationInfo.Id=&PostId.ToString()
//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.
&NotificationInfo.Message=&commentNotificationInfo.ToJson()
&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.

Event OnMessage(&NotificationInfo)
for each line
  if (&NotificationInfo.Id=&postid.ToString())
   //processs the notification data
  endif
endfor
Endevent

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.

Event Start
    &clientId = &webnotification.ClientId
   //save the information of the client: RegisteredClientId = &clientId//
Endevent

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.Id=&cont.ToString()
&NotificationInfo.Message="El mensaje " + &cont.ToString() + "cliente " + &ClientId
&webnotification.NotifyClient(&ClientId, &NotificationInfo) 

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.

Event onmessage(&notificationInfo)
    //Do something with the &notificationInfo
endevent

Note

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.

Requirements

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.

Troubleshooting

  • The following line can be added to the gxgral.js file "gx.dbg.enabled = true;" in order to get additional information in the javascript console.
  • 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".

See Also

Html5 Web Notifications User Control
HowTo:Develop a messaging web page
HowTo: Receiving and processing a notification message from an external app