Identify which notification launched the MIDlet

Your MIDlet can identify which notification launched it as follows:

You can utilize JSR-211 to let your MIDlet know which of the incoming notifications caused it to launch. For users, this will provide direct access to the content that they were notified about. Thus, the user experience becomes more streamlined. From push notifications viewpoint you get the following by implementing JSR-211 capability into your MIDlet:

  • Your MIDlet knows whether user launched it from app grid or by clicking a notification on the notification UI. In latter case, the MIDlet recognizes which of the incoming notifications launched it.

  • Every NotificationMessage instance includes a unique value called notification item ID.

  • When using JSR-211, your MIDlet receives the notification item ID value as an invocation parameter.

  • Your MIDlet gets the notification item ID value by calling NotificationMessage.getNotificationItemId() method. The value of the notification item ID is returned as a String object.

  • After receiving notification(s) the MIDlet needs to find matching NotificationMessage with the notification item id received as an invocation parameter, by comparing the value to the notification item IDs of all incoming notifications. This match means that the user clicked on this particular notification in the Notification UI.

Required changes to your MIDlet’s JAD file

You need to add a MicroEdition-Handler-1 property with values to the JAD file to utilize J2ME support of the JSR-211. Here is the format of the property and its values:

MicroEdition-Handler-1: <path>.<class name>,<mime type>, , <JSR-211 supported commands>,

Here is an example of the how it could look like:

MicroEdition-Handler-1: com.example.MyExampleMIDlet, application/vnd.nokia.push.notification, , open,

<path> is “com.example”

<class name> is “MyExampleMIDlet”

<mime type> is “application/vnd.nokia.push.notification” (This is the MIME type to be used for using JSR-211 for push notifications.)

<JSR-211 supported commands> is “open” (This is the only action which is supported by push notifications for JSR-211.)

In addition, you need to add MIDlet permission javax.microedition.content.ContentHandler as a value of MIDlet-Permissions. Here is an example of how to define in this case all MIDlet persmissions required by the API usage.

MIDlet-Permissions: javax.microedition.io.Connector.file.read, javax.microedition.io.Connector.file.write, javax.microedition.io.Connector.http, javax.microedition.content.ContentHandler
MIDlet-Permissions-Opt: com.nokia.mid.s40.io.Connector.localmsg

Required changes to your MIDlet’s source code

There are two alternative approaches for implementing JSR-211 usage:

  1. Implementing RequestListener class from ContentHandler.

  2. Calling ContentHandlerServer.getRequest

The first approach is the preferred way to implement JSR-211 support for push notifications. Thus, implementing class RequestListener and modifying your MIDlet class source code is described below.

Add the following import statement into your MIDlet class.

import javax.microedition.content.RequestListener;
import javax.microedition.content.Invocation;
import javax.microedition.content.ContentHandlerServer;
import javax.microedition.content.Registry;
import javax.microedition.content.ContentHandlerException;

Make your MIDlet class implement the interface class RequestListener.

class MyExample extends Midlet implements RequestListener{

Add declarations for the member attributes. Here we use names “invocation” and “contentHandlerServer”.

class MyExample extends Midlet implements RequestListener{
private Invocation invocation;
private ContentHandlerServer contentHandlerServer;

You need to make some changes into the MIDlet’s constructor as in the following snippet. First your MIDlet needs to get a reference to the ContentHandlerServer object and then set the listener.

public NapiExample() {
    try {
        contentHandlerServer = Registry.getServer(this.getClass().getName());
        contentHandlerServer.setListener(this);
    }
    catch (ContentHandlerException ex) {
        System.out.println("exception caught: " + ex.getErrorCode());
    }
}

Add implementation of the RequestListener.invocationRequestNotify method as shown in the snippet.

public void invocationRequestNotify(ContentHandlerServer contentHandlerServer){
    if(contentHandlerServer != null ){
            invocation = contentHandlerServer.getRequest(false);
            handleInvocation();
    }
}

Next you need implement the handleInvocation() method that is called in the snippet above.

private void handleInvocation() {
    System.out.println("handleInvocation() - starts");
    if (invocation != null) {
        if ((invocation.getArgs() != null) && (invocation.getArgs().length > 0)) {
            String notificationItemId = invocation.getArgs()[0];
            System.out.println ("Invocation action: " + invocation.getAction());
            if(notificationItemId != null ){
                System.out.println ("Invocation argument was:" + notificationItemId);
            }
        }
        System.out.println("call handlerServer.finish");
        handlerServer.finish(invocation, Invocation.OK);
    }
    System.out.println("handleInvocation() - ends");
}