How to force a tray notification from server?

Hi,

I have an application where user logs in from different locations, and can leave messages to each others mail box (applications mail box), My problems is…

I want to be able to show a TRAY NOTIFICATION to user who is logged in, and has been sent a message. I think this has to be triggered from the server…but how am I gonna show this notification to the screen of this concerned user?

*not sure if this post should be in general help or here.

Thanks

Hi,

While you can call showNotification purely on the server , say, in some background thread, the actual notification will not be visible to a user until the nearest server roundtrip.

As a solution, you may place an invisible progress indicator, that has a polling capability and tune it to poll, for instance, every 5 seconds. This way the notification will reach the end user.

Upd. Do not forget, that you also must find a window instance of the particular user you want to send message to from your server messaging thread.

Maybe this should have been the original question :slight_smile:

how to get the window instance of individual users and user it to do some server notification?

Hi,

dll is right. I don’t know how far you have reached, but you need to build a messaging system at server side to communicate between the application instances (user sessions) and use the showNotification at server-side.

For a complete simple messaging architecture:

  • Create a “messenger” singleton class with a listener interface for that
  • Register every window (or whatever) that has to receive notifications
  • Whenever a message is posted to “messenger” all listeners are invoked
  • When a window receives a message the showNotification is called
  • The message is visible to user when the user next does something that interacts with the server.
  • If more real-time messaging is needed, it could be turned on using a ProgressIndicator with appropriate polling interval. This forces the client-side to continuously/periodically interact with the server.

This is just a simple approach, but it has been successfully been used in some applications.

I like Sami’s suggesiton on organization of the messenger service. Here is a sample code from our application server messenger, which does almost the same but just sends e-mails. I stripped out all e-mail stuff, so you only have to implement deliverMessage method, singleton and listeners stuff.


public class Messenger implements Runnable
{

    private Queue<String> mailSpool = new ConcurrentLinkedQueue<String> ();

    private int messagesSent = 0;
    private boolean shutdownRequested = false;

   public MailMessenger (  )
    {
        new Thread ( this, "Toolkit messenger service" ).start ();
    }

    public void sendMessage ( String message )
    {
        mailSpool.add ( message );

        synchronized ( this )
        {
            notifyAll ();
        }
    }

    public void shutdown ()
    {
        shutdownRequested = true;

        synchronized ( this )
        {
            notifyAll ();
        }
    }

    public void run ()
    {
        while ( !shutdownRequested )
        {
            while ( mailSpool.peek () != null )
            {
                deliverMessage ( mailSpool.poll () );
            }

            synchronized ( this )
            {
                try
                {
                    wait ();
                }
                catch ( InterruptedException e )
                {
                    //ignored
                }
            }
        }
    }

    private void deliverMessage ( String message )
    {
        // place your code for actual message broadcast to all clients
        messagesSent++;
    }

    public int getMessagesSent ()
    {
        return messagesSent;
    }
}

Thanks, I am gonna try this…

Kiitos.