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 Developer
Posts: 3
Registered: ‎11-06-2008
My Device: Not Specified

Help! Need sample client app code for handling onMessage() when push content arrives

I have implemented the client side of push functionality in my BlackBerry app.  I am successfully registering, and I am getting push alerts in my onMessage() handler.  My problem is that I am unable to read the data from the PushInputStream.

 

I have not found any examples on how to do this, here is the code I have right now:

 

 

    public void onMessage(PushInputStream inputStream, StreamConnection conn)   
    {

                inputStream.accept();

 

                
try
{
byte[] buffer = new byte[1000];
int size = inputStream.read(buffer);
                String strAlert = new String(buffer);
// display the push content
Dialog.alert(strAlert);
}
       catch(IOException ioex)
{
Dialog.alert ("Error reading input stream");
}
                // close the connection
                inputStream.close();
                conn.close();
}
I am getting "Error reading input stream" every time.  I'm sure I'm missing something obvious here. Can someone post the proper way to handling the parameters PushInputStream and StreamConnection in the onMessage handler?
Thanks!
Highlighted
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Help! Need sample client app code for handling onMessage() when push content arrives

check the PushMessageReader from the sample app.

 

it uses a bigger buffer

private static byte[] buffer = new byte[15 * 1024];

 

checks the message header, and, for a textmessage uses

int size = pis.read(buffer);
binaryData = new byte[size];
System.arraycopy(buffer, 0, binaryData, 0, size);
PushMessage message = new PushMessage(msgId, System.currentTimeMillis(), binaryData, true, true);

 

personally i use IOUtilities to read inputstreams, but in this case i just took the code as it is.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter