01-31-2012 08:40 AM
Hello, everyone, I'm trying to incorporate push services to my blackberry applications. Since they are targeted for users wich have an MDS service already working.. we just need the BES RIM Push, however I couldn't find a clear example of how it works... until know I have 2 implemenations of it :
The first one:
public class BESApplication extends UiApplication implements PushApplication
{
/**
* Entry point for application
* @param args Command line arguments (not used)
*/
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
BESApplication theApp = new BESApplication();
theApp.enterEventDispatcher();
ApplicationDescriptor descriptor = ApplicationDescriptor
.currentApplicationDescriptor();
ApplicationDescriptor pushDescriptor = new ApplicationDescriptor(
descriptor, new String[] { "push" });
PushApplicationDescriptor registeredDescriptor = new
PushApplicationDescriptor(4242, pushDescriptor);
PushApplicationRegistry
.registerApplication(registeredDescriptor);
}
/**
* Creates a new MyApp object
*/
public BESApplication()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new BESScreen());
}
public void onMessage(PushInputStream inputStream, StreamConnection conn) {
System.out.println("ON MESSAGE");
}
public void onStatusChange(PushApplicationStatus status) {
// TODO Auto-generated method stub
}
}
/**
* This class extends the UiApplication class, providing a graphical user
* interface.
*/
public class BESPushApplication extends UiApplication {
private static final String URL = "http://:4242"; // PORT 4242
private ListeningThread _listeningThread;
private BESPushScreen pushScreen;
/**
* Entry point for application
*
* @param args
* Command line arguments (not used)
*/
public static void main(String[] args) {
BESPushApplication theApp = new BESPushApplication();
theApp.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public BESPushApplication() {
pushScreen=new BESPushScreen();
pushScreen(pushScreen);
_listeningThread = new ListeningThread();
_listeningThread.start();
}
private class ListeningThread extends Thread {
private boolean _stop = false;
private StreamConnectionNotifier _notify;
private synchronized void stop() {
_stop = true;
try {
// Close the connection so the thread will return.
_notify.close();
} catch (IOException e) {
System.err.println(e.toString());
} catch (NullPointerException e) {
// The notify object likely failed to open, due to an
// IOException.
}
}
public void run() {
StreamConnection stream = null;
InputStream input = null;
MDSPushInputStream pushInputStream = null;
while (!_stop) {
try {
synchronized (this) {
_notify = (StreamConnectionNotifier) Connector.open(URL
+ ";deviceside=false");
}
while (!_stop) {
stream = _notify.acceptAndOpen();
try {
input = stream.openInputStream();
pushInputStream = new MDSPushInputStream(
(HttpServerConnection) stream, input);
DataBuffer db = new DataBuffer();
byte[] data = new byte[256];
int chunk = 0;
while (-1 != (chunk = input.read(data))) {
db.write(data, 0, chunk);
}
updateMessages(data);
input.close();
stream.close();
data = db.getArray();
} catch (Exception e) {
if (input != null) {
try {
input.close();
} catch (IOException e2) {
}
}
if (stream != null) {
try {
stream.close();
} catch (IOException e2) {
// TODO: handle exception
}
}
}
}
_notify.close();
_notify = null;
} catch (IOException e) {
if (_notify != null) {
try {
_notify.close();
_notify = null;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
}
public void onExit(){
_listeningThread.stop();
try{
_listeningThread.join();
}catch (InterruptedException e) {
System.err.println(e.toString());
}
}
private void updateMessages(final byte[] data){
Application.getApplication().invokeLater(new Runnable() {
public void run() {
String[] choices = {"Ok","Cancel"};
if (0 != Dialog.ask("New message received do you want to render it?",choices,0)){
return;
}
UiApplication.getUiApplication().popScreen(pushScr een);
UiApplication.getUiApplication().pushScreen(new PushScreen(new String(data)));
}
});
}
The one that uses a Thread is working fine... it's reading messages and prompting them... however it only works while the application is active... And this is not the behaviour I want. Because of this I searched for the first implementation since I saw posts where they say that the application would launch as a popup ( I.E: when looking at the Home screen, my app should launch automatically). What I am doing wrong in the first one?.
PD: The application that's pushing messages to the phone is working Ok... don't think the problem is in that side.
Thanks a lot.