15
PushRegistry

Push registrysup

  • Upload
    smijava

  • View
    1.098

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Push registrysup

PushRegistry

Page 2: Push registrysup

Introduction

Push registry is introduced in MIDP 2.0 (JSR 118)

Push Registry maintains a list of inbound connections and associated applications

The event is triggered by the server and delegated to the mobile device where an application is registered for this event

With the advent of push registry your application can now be launched in three ways Select the application from Application Launcher screen. rigger by inbound network request Timer based activation

Page 3: Push registrysup

PushRegistry Package

The push registry is part of the Generic Connection Framework (GCF) and is encapsulated within a single class,javax.microedition.io.PushRegistry

getFilter()getMidlet()listConnections() PushRegistry MethodsregisterAlarm()registerConnection()unregisterConnection()

Page 4: Push registrysup

MIDlet Activation and Life Cycle

Page 5: Push registrysup

PushRegistry Overview

The MIDlet registers a port along with the protocol name in the mobile device such that, if any message arrives in the specified port with the protocol mentioned, the AMS delivers it to the MIDlet

From the server, a message is sent to the specific mobile device using the particular protocol and port where the MIDlet application is registered to listen

After the message is delivered to the mobile device, the AMS calls the MIDlet application, which has registered to listen to that particular port and particular protocol

Once the message is delivered to the MIDlet, it is the application's responsibility to process the message accordingly

Page 6: Push registrysup

PushRegistry Overview

To become push-enabled, MIDlets must register with the push registry,

using one of two types of registration:

Static Registration

Dynamic Connection

Page 7: Push registrysup

Static Registration

Registrations of static (well known) connections occur during the installation of the MIDlet suite

Static registrations are defined by listing one or more MIDlet-Push attributes in the JAD file or JAR manifest

The installation will fail if you attempt to register an address that's already bound

Uninstalling a MIDlet suite automatically unregisters the connection

The format of the MIDlet-Push attribute is... MIDlet-Push-<n>: <ConnectionURL>, <MIDletClassName>, <AllowedSender>

Example: MIDlet-Push-1: socket://:79, com.j2me.salsa.midletsuite.FirstMIDlet, 200.200.50.1

MIDlet-Push-2: datagram://:50000, com.j2me.salsa.midletsuite.SecondMIDlet, *

Page 8: Push registrysup

Dynamic Connection

Dynamic registration are used for both inbound network connections and timer-based alarms, at runtime

Example code for dynamic registration:

PushRegistry.registerConnection("socket://:79","com.FirstMIDlet", "200.200.50.1");

PushRegistry.registerConnection("datagram://:50000,com.SecondMIDlet", "*");

To unregistered dynamically use unregisterConnection(String connection) Method

Page 9: Push registrysup

Alarm Based Push Registry

Only Dynamic Push Registry can be implemented in Alarm Based Push Registry

Use registerAlarm(String midlet, long time) Method to register your MIDLet

Example code to register Alarm long day = 1000*60*60*24;

long t = new Date().getTime() + day;PushRegistry.registerAlarm("PushDemoMIDlet", t);

Page 10: Push registrysup

Security Considerations

Application signing enables the platform to determine whether to trust another application

Network and push operations are considered restricted: applications must request permission before using them

Attempting to use a restricted operation without proper permission causes the system to throw a SecurityException

Add permissions in the JAD file or the JAR manifest, by creating MIDlet-Permissions property entries

For example, to request permission to use the PushRegistry and ServerSocketConnection APIs, define the following property entry:

MIDlet-Permissions: javax.microedition.io.PushRegistry, javax.microedition.io.Connector.serversocket

Page 11: Push registrysup

List of all Inbound connections

To Display List of all Inbound connections

public void displayConnections() {

//Passing false in listConnections() method of PushRegistry class return the complete list of registered connections for the current MIDlet suite

    String[] allConnections = PushRegistry.listConnections(false);

    StringBuffer sbuf = new StringBuffer();if (allConnections != null && allConnections.length > 0) {  for (int i = 0; i < allConnections.length; i++ ) { String midlet = PushRegistry.getMIDlet(connections[i]);

String filter = PushRegistry.getFilter(connections[i]);      sbuf.append(“pushInfo=”+allConnections[i]+” ”+midlet+” ”+filter);

     sbuf.append("\n");  }}String str = sbf.toString();

}

Page 12: Push registrysup

Register or UnRegister An Inbound Connection

Way to register or unregister an inbound connection

public void regConnection() { try {

String url = "socket://:5000";String classname = this.getClass.getName(); //Midlet to be launched    if (register) {         PushRegistry.registerConnection(url, classname, "*");    } else {            PushRegistry.unregisterConnection(url);    }

} catch (IllegalArgumentException iae) {   

 } catch (IOException ioe) {

  }

}

Page 13: Push registrysup

Discovering Whether a MIDlet was push-Activated

The PushRegistry.listConnections() method allows you to discover all the inbound connections registered by the MIDlet suite

Determine if activated due to inbound connection and if so dispatch a PushProcessor to handle incoming return true if MIDlet was activated due to inbound connection, false otherwise

private boolean handlePushActivation() { // Discover if there are pending push inbound // connections and if so, dispatch a // PushProcessor for each one. String[] connections = PushRegistry.listConnections(true); if (connections != null && connections.length > 0) { for (int i=0; i < connections.length; i++) { PushProcessor pp = new PushProcessor(connections[i]); } return(true); } return(false); }

Page 14: Push registrysup

Receiving and Processing Push Events

Way to process the received push events

if (url.startsWith("socket://")) { // "Open" connection. ServerSocketConnection ssc = (ServerSocketConnection) Connector.open(url); // Wait for (and accept) inbound connection. SocketConnection sc = (SocketConnection) ssc.acceptAndOpen(); InputStream is = sc.openInputStream(); // Read data from inbound connection. int ch; byte[] data = null; ByteArrayOutputStream tmp = new ByteArrayOutputStream(); while( ( ch = is.read() ) != -1 ) { tmp.write( ch ); } data = tmp.toByteArray();

// Here do something with received data

System.out.print(new String(data)); }

Page 15: Push registrysup

Thank You