BlackBerry Push Notifications , OS < 7.X -
i working on developing bb os<7.x application, implements push notifications. ended working code able receive push.
however, push notifications dont work first time deploy application on phone. if try send push on device, after deployed it, when send push server, can see on right corner of device little arrow loading , no push message displayed on screen.
if restart device, push notifications work properly!! whenever send push, see little arrow again on right screen , push message displayed corrected.
the code looks this(the parts consider important):
myapp.java
public class myapp extends uiapplication { public static void main(string[] args) { //every time start application register bis push if (args.length > 0 && args[0].equals("blackberrycity")) { system.out.println("!!!!!!!!!!!!!!i inside if"); //registering push push_main.registerbpas(); myapp app = new myapp(); app.entereventdispatcher(); } //every time restart phone , call background process responsible listening push else { system.out.println("!!!!!!!!!!!!!!i inside else"); //should put background classes listening pushed msgs :d backgroundapplication backapp=new backgroundapplication(); backapp.setupbackgroundapplication(); backapp.entereventdispatcher(); } } public myapp() { pushscreen(new myappscreen()); } } class myappscreen extends mainscreen { public myappscreen() { //some stuff here.. } }
push_main.java
public class push_main { /** * entry point application * @param args command line arguments (not used) */ private static final string register_successful = "rc=200"; private static final string deregister_successful = register_successful; private static final string user_already_subscribed = "rc=10003"; private static final string already_unsuscribed_by_user = "rc=10004"; private static final string already_unsuscribed_by_provider = "rc=10005"; private static final string push_port = ""+"33387"; //push port private static final string bpas_url = "http://pushapi.eval.blackberry.com"; private static final string app_id = ""+ "3592-m4587f9s9k836r930ko2395i32i66y10a34"; // add application id private static string url = "http://:"+ "33397"; // add push port. private static final int chunk_size = 256; public static listeningthread _listeningthread; public static streamconnectionnotifier _notify; private static final long id = 0x954a603c0dee81e0l; public push_main() { if(_listeningthread==null) { system.out.println("msg on listening thread 1"); _listeningthread = new listeningthread(); system.out.println("msg on listening thread 2"); _listeningthread.start(); system.out.println("msg on listhning thread 3 "); } } public static class listeningthread extends thread { private boolean _stop = false; /** * stops thread listening. */ private synchronized void stop() { _stop = true; try { // close connection thread return. _notify.close(); } catch (exception e) { } } /** * listen data http url. after data has been read, * render data onto screen. * @see java.lang.runnable#run() */ public void run() { streamconnection stream = null; inputstream input = null; mdspushinputstream pushinputstream=null; while (!_stop) { try { // synchronize here don't end creating connection never closed. synchronized(this) { // open connection once (or re-open after ioexception), don't end // in race condition, push lost if comes in before connection // open again. open url parameter indicates should // use mds when attempting connect. system.out.println("\n\n msg connection 1"); _notify = (streamconnectionnotifier)connector.open(url); system.out.println("\n\n msg connection 2"); } while (!_stop) { // note: following block until data received. system.out.println("\n\n msg notify 1"); stream = _notify.acceptandopen(); system.out.println("\n\n msg 1 "); try { system.out.println("\n\n msg 2"); input = stream.openinputstream(); system.out.println("\n\n msg 3 "); pushinputstream= new mdspushinputstream((httpserverconnection)stream, input); system.out.println("\n\n msg 4"); // extract data input stream. databuffer db = new databuffer(); byte[] data = new byte[chunk_size]; int chunk = 0; while ( -1 != (chunk = input.read(data)) ) { db.write(data, 0, chunk); } updatemessage(data); // method called accept push. pushinputstream.accept(); data = db.getarray(); } catch (ioexception e1) { // problem occurred input stream , however, original // streamconnectionnotifier still valid. // errordialog(e1.tostring()); } { if ( input != null ) { try { input.close(); } catch (ioexception e2) { } } if ( stream != null ) { try { stream.close(); } catch (ioexception e2) { } } } } } catch (ioexception ioe) { // stream closed. catches exception thrown // _notify.acceptandopen() when program exits. errordialog(ioe.tostring()); } { /* if ( _notify != null ) { try { _notify.close(); _notify = null; } catch ( ioexception e ) { } } */ } } } } private static void updatemessage(final byte[] data) { system.out.println("\n\n msg 6"); application.getapplication().invokelater(new runnable() { public void run() { // query user load received message. // dialog.alert( new string(data)); uiapplication.getuiapplication().invokelater( new runnable() { public void run() { notificationsmanager.triggerimmediateevent(id, 0, null, null); dialog d = new dialog( dialog.d_ok, new string(data) ,0, null, screen.default_close); // _dialogshowing = true; uiapplication.getuiapplication().pushglobalscreen( d, 10, uiapplication.global_modal ); // dialog closed @ point, cancel event. } } ); } }); } public static void registerbpas() { /** * connection suffix fixed use thread call connection code * **/ new thread() { public void run() { try { string registerurl = formregisterrequest(bpas_url, app_id, null) + ";deviceside=false;connectiontype=mds-public"; //dialog.alert(registerurl); if ((wlaninfo.getwlanstate() == wlaninfo.wlan_state_connected) && radioinfo .arewafssupported(radioinfo.waf_wlan)) { registerurl += ";interface=wifi"; } system.out.println("\n\n\n !!msg registerbpas url is: "+ registerurl + "\n\n"); httpconnection httpconnection = (httpconnection) connector.open(registerurl); inputstream = httpconnection.openinputstream(); system.out.println("\n\n\n !!!!!!!!!!!i here "); string response = new string(ioutilities.streamtobytes(is)); system.out.println("\n\n\n\n\n\n msg respose code : " + response); system.out.println("\n\n\n !!!!!!!!!!!i here2 "); httpconnection.close(); string nexturl = formregisterrequest(bpas_url, app_id, response) + ";deviceside=false;connectiontype=mds-public"; system.out.println("\n\n\n\n\n\n msg nexturl : " + nexturl); system.out.println("\n\n\n !!!!!!!!!!!i here 3"); if ((wlaninfo.getwlanstate() == wlaninfo.wlan_state_connected) && radioinfo .arewafssupported(radioinfo.waf_wlan)) { nexturl += ";interface=wifi"; system.out.println("\n\n\n !!!!!!!!!!!i here 4"); } httpconnection nexthttpconnection = (httpconnection) connector.open(nexturl); inputstream nextinputstream = nexthttpconnection.openinputstream(); response = new string(ioutilities.streamtobytes(nextinputstream)); system.out.println("\n\n\n !!!!!!!!!!!i here 5"); system.out.println("\n\n\n\n\n\n msg respose code 1: " + response); nexthttpconnection.close(); if (register_successful.equals(response) || user_already_subscribed.equals(response)) { dialog.alert("msg registered bis push"); system.out.println("\n\n\n !!!!!!!!!!!i here 6"); system.out.println("msg registered bis push"); } else { dialog.alert("msg bpas rejected registration"); system.out.println("msg bpas rejected registration"); } } catch (final ioexception e) { dialog.alert("msg ioexception on register() " + e + " " + e.getmessage()); system.out.println("msg ioexception on register() " + e + " " + e.getmessage()); } } }.start(); } public static void close(connection conn, inputstream is, outputstream os) { if (os != null) { try { os.close(); } catch (ioexception e) { } } if (is != null) { try { is.close(); } catch (ioexception e) { } } if (conn != null) { try { conn.close(); } catch (ioexception e) { } } } public static void errordialog(final string message) { uiapplication.getuiapplication().invokelater(new runnable() { public void run() { dialog.alert(message); } }); } private static string formregisterrequest(string bpasurl, string appid, string token) { stringbuffer sb = new stringbuffer(bpasurl); sb.append("/mss/pd_subreg?"); sb.append("serviceid=").append(appid); sb.append("&osversion=").append(deviceinfo.getsoftwareversion()); sb.append("&model=").append(deviceinfo.getdevicename()); if (token != null && token.length() > 0) { sb.append("&").append(token); } return sb.tostring(); } }
backgroundapplication.java
import java.io.ioexception; import java.io.inputstream; import javax.microedition.io.connector; import javax.microedition.io.serversocketconnection; import net.rim.device.api.io.http.httpserverconnection; import net.rim.device.api.io.http.mdspushinputstream; import net.rim.device.api.io.http.pushinputstream; import net.rim.device.api.system.application; import net.rim.device.api.system.radioinfo; import net.rim.device.api.system.wlaninfo; class backgroundapplication extends application { public backgroundapplication() { // todo auto-generated constructor stub } public void setupbackgroundapplication(){ messagereadingthread messagereadingthread = new messagereadingthread(); messagereadingthread.start(); } private static class messagereadingthread extends thread { private boolean running; private serversocketconnection socket; private httpserverconnection conn; private inputstream inputstream; private pushinputstream pushinputstream; public messagereadingthread() { this.running = true; } public void run() { string url = "http://:" + "33387" ;//here after + add port number url += ";deviceside=false;connectiontype=mds-public"; if ((wlaninfo.getwlanstate() == wlaninfo.wlan_state_connected) && radioinfo.arewafssupported(radioinfo.waf_wlan)) { url += ";interface=wifi"; } try { socket = (serversocketconnection) connector.open( url ); } catch( ioexception ex ) { // can't open port, taken application onlistenerror( ex ); } while( running ) { try { object o = socket.acceptandopen(); conn = (httpserverconnection) o; inputstream = conn.openinputstream(); pushinputstream = new mdspushinputstream( conn, inputstream ); pushmessagereader.process( pushinputstream, conn ); } catch( exception e ) { if( running ) { // logger.warn( "failed read push message, caused " + e.getmessage() ); running = false; } } { // pushutils.close( conn, pushinputstream, null ); } } // logger.log( "stopped listening push messages" ); } public void stoprunning() { running = false; //pushutils.close( socket, null, null ); } private void onlistenerror( final exception ex ) { // logger.warn( "failed open port, caused " + ex ); system.out.println(ex); } }
on main class , myapp.java , can see everytime application launched, register push. every time device restarts, start background process listen push. or @ least, believe doing here.
why should that?
i thought registering push, background listener somehow start too... guess doesn't.. that's why when restart phone, code executed , push starts coming in.
how fix problem start background listener:
the first time app deployed
every time restart device
but not on every launch of app.
Comments
Post a Comment