Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

BlackBerry Push Development

Reply
New Contributor
Posts: 7
Registered: ‎07-26-2013
My Device: Blackberry 8520
My Carrier: Movistar
Accepted Solution

Push and Midlets

I'm trying to add push notifications, via Blackberry Push Service, to my already developed and productive app with midlets. As i can't instanciate an UIApplication on a MIDlet, i thought for letting my app "wake app" on every push, use PushRegistry with the AMS software.

 

Its posible to register for using the Blackberry Push Notification Service, in a very low level, using PushRegistry on a MIDlet app?

 

 

Thanks,

Nico.

 

 

 

 

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: Push and Midlets

Hi Nico, 

 

Welcome to the forums!

 

Yes the PushRegistry is the way to go, but it's not a nice or easy solution. You need to create an auto-start MIDlet:

http://supportforums.blackberry.com/t5/Java-Development/Create-an-auto-start-MIDlet-using-the-PushRe...

 

Once your app launches for the first time you can remove the auto-start then register with the PushRegistry to receive the Push messages:

http://supportforums.blackberry.com/t5/Java-Development/Register-a-MIDlet-with-the-PushRegistry/ta-p...

Garett
@garettBeuk
--
Goodbye everybody!
New Contributor
Posts: 7
Registered: ‎07-26-2013
My Device: Blackberry 8520
My Carrier: Movistar

Re: Push and Midlets

 

I will try that!

 

Thanks!

 

New Contributor
Posts: 7
Registered: ‎07-26-2013
My Device: Blackberry 8520
My Carrier: Movistar

Re: Push and Midlets

 

I was trying to get this done, but i get anError while opening the socket connection.

On the my code, i used the port that Blackberry site send me when i registered for push.

 

url="socket://:50000"; 

Connection.open(url);

 

The error is: 

javax.microedition.io.ConnectionNotFoundException: socket://:50000 does not support push.

 

The excecution of the line its on a secondary thread, not on the main thread.

 

I'm testing on a Blackberry 8900 with OS 5.2.0.104

 

The jad has the following permissions:

javax.microedition.io.Pushregistry, javax.microedition.io.Connector.serversocket, javax.microedition.io.Connector.http, javax.microedition.io.Connector.socket, javax.microedition.io.Connector.sensor, javax.microedition.io.Connector.cbs,   javax.microedition.io.Connector.sms, javax.microedition.io.Connector.com, javax.microedition.io.Connector.https, javax.wireless.messaging.sms.receive,  javax.wireless.messaging.cbs.receive

 

Added the PushRegistry parameter on the jad:  MIDlet-Push-1: socket://:50000,manager.DynamicPushRegistry,*

 

I tryied a los of different options for url and still this exception.

 

 

 

Thanks!

 

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: Push and Midlets

Could you try the sample app from the following URL and let me know if it works successfully for you?

http://supportforums.blackberry.com/t5/Java-Development/Register-a-MIDlet-with-the-PushRegistry/ta-p...

Garett
@garettBeuk
--
Goodbye everybody!
New Contributor
Posts: 7
Registered: ‎07-26-2013
My Device: Blackberry 8520
My Carrier: Movistar

Re: Push and Midlets

 

Yes i tried that same sample, i had a copy mistake when i posted the message.

 

The error its the same:

javax.microedition.io.ConnectionNotFoundException: socket://:50000 does not support push.

 

 

Any ideas on why its happening?

 

Thanks.

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: Push and Midlets

Are you using port "50000" or the port provided to you?

Are you usign a real device or simulator?

Garett
@garettBeuk
--
Goodbye everybody!
New Contributor
Posts: 7
Registered: ‎07-26-2013
My Device: Blackberry 8520
My Carrier: Movistar

Re: Push and Midlets

I tryied with both ports. 50000, and the one that Blackberry send to me when i registered for push.

 

Yes on a Real Device, Blackberry 8900 with  OS 5.2.0.104.

 

Also on a Blackberry 9700 with 6.6.0.236

 

The Blackberry SDK used is 5.0.0

 

 

New Contributor
Posts: 7
Registered: ‎07-26-2013
My Device: Blackberry 8520
My Carrier: Movistar

Re: Push and Midlets

 

 

Solved by some on here at work, by adding this code. Post here If anyone helps.

 

 

package test.javax.microedition.midlet;

import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import java.util.*;
import java.io.*;
import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;


public final class DynamicPushRegistry extends MIDlet
{
//The data we expect to receive.
private static final String TEST_DATA = "This is just a test";
private static final ApplicationPermissionsManager apm = ApplicationPermissionsManager.getInstance();

public DynamicPushRegistry()
{
setPermission(ApplicationPermissions.PERMISSION_CROSS_APPLICATION_COMMUNICATION);
setPermission(ApplicationPermissions.PERMISSION_BROWSER_FILTER);
setPermission(ApplicationPermissions.PERMISSION_INTERNET);
setPermission(ApplicationPermissions.PERMISSION_SERVER_NETWORK);
setPermission(ApplicationPermissions.PERMISSION_APPLICATION_MANAGEMENT);
}

//Signals the MIDlet that it has entered the Active state.
public void startApp()
{
//Get the display object for this MIDlet.
Display d = Display.getDisplay(this);

//Create and start a new WorkerThread.
Thread t = new WorkerThread();
t.start();

//Create a new Alert.
Alert alert = new Alert("Starting DynamicPushRegisry");

//Display the alert for 2 seconds.
alert.setTimeout(2000);

d.setCurrent(alert);
}

private class WorkerThread extends Thread
{
public void run()
{

//Get all connections registered with the PushRegistry
//for the current MIDlet suite.
String[] connections = PushRegistry.listConnections(true);

if ( connections.length == 0 )
{
//Nothing is registered, register a connection.
try
{
//Dynamically register a socket connection
//on port 50000.
PushRegistry.registerConnection("socket://:50000",
"test.javax.microedition.midlet.DynamicPushRegistry",
"*");
} catch (ConnectionNotFoundException e)
{
message(e.toString());
} catch (ClassNotFoundException e)
{
message(e.toString());
} catch (IOException e)
{
message(e.toString());
}
}
else
{
//A connection is already registered and data is arriving.
//Fetch the incoming data.
fetchData(connections[0]);

//Shut down to wait for data.
message("Done");
}
notifyDestroyed();
}
}

//Fetches the incoming data.
private void fetchData(String connection)
{
try
{
//Open the connection.
Connection c = Connector.open(connection);

if ( c instanceof StreamConnectionNotifier )
{
//Open an InputStream.
StreamConnectionNotifier scn = (StreamConnectionNotifier)c;
StreamConnection sc = scn.acceptAndOpen();
InputStream input = sc.openInputStream();

//Extract the data from the InputStream.
StringBuffer sb = new StringBuffer();
byte[] data = new byte[256];
int chunk = 0;
while ( -1 != (chunk = input.read(data)) )
{
sb.append(new String(data, 0, chunk));
}

//Close the InputStream and StreamConnection.
input.close();
sc.close();

String s = sb.toString();

//Test the data for validity.
//If invalid an exception is thrown.
Assert.assertTrue(TEST_DATA.equals(s),
"Failed to receive proper string: " + s);

//Display the received data.
message("Received: " + sb.toString());
}
} catch (IOException e)
{
message(e.toString());
}

}

//Display a String message to the user.
private void message(String msg)
{
//Create a new Alert with the message.
Alert alert = new Alert(msg);

//Display the alert for 5 seconds.
alert.setTimeout(5000);

Display.getDisplay(this).setCurrent(alert);
}

//Signals the MIDlet to stop and enter the Pause state.
public void pauseApp()
{
}

//Signals the MIDlet to terminate and enter the Destroyed state.
//Unconditional when set to true. The MIDlet must cleanup and release
//all resources. Otherwise, the MIDlet may throw a
//MIDletStateChangeException to indicate it does not want to be
//destroyed at this time.
public void destroyApp(boolean unconditional)
{
}


private boolean setPermission(int permission) {

boolean updatedPermissions = false;
ApplicationPermissions ap = apm.getApplicationPermissions();
if (ap.containsPermissionKey(permission)) {
int eventInjectorPermission = ap.getPermission(permission);

if (eventInjectorPermission != ApplicationPermissions.VALUE_ALLOW) {

ap.addPermission(permission);
updatedPermissions = apm.invokePermissionsRequest(ap);
}
} else {
ap.addPermission(permission);
updatedPermissions = apm.invokePermissionsRequest(ap);
}

return updatedPermissions;
}
}

//Throws an IllegalStateException with the supplied error message.
final class Assert
{
private Assert()
{
}

static public void assertTrue(boolean exp, String failmsg)
{
if (!exp)
throw new IllegalStateException(failmsg);
}
}