10-19-2010 12:30 AM
I thought that I would post my code since I spent many hours scratching my head on getting widgets to work in the background while listening for push notifications. I just didn't have the bigger picture when I started but now I do.... so here ya go!
(1) Download the example push code from the Lab and get that working first.
http://supportforums.blackberry.com/t5/Web-Develop
(2) In your app, make sure that you give permissions to:
(3) Set up the push listener and callback function
var port = 100;
blackberry.push.openPushListener(handleReturnData, port);
function handleReturnData(data) {
if (data != null) {
var text = blackberry.utils.blobToString(data.payload);
alert("text recieved from push: " + text);
}
}
(4) Handle the user closing the app
blackberry.app.event.onExit(handleExit);
function handleExit() {
blackberry.app.requestBackground();
}(5) Now within the handleReturnData function (before you do anything with the data) check if it's in the foreground or not. If it's not, move it to the foreground
if ( !blackberry.app.isForeground ) {
blackberry.app.event.onForeground( handleForeground ); //you don't need this line of code if you don't want it
blackberry.app.requestForeground();
}
function handleForeground(){
//could do something here if you want to
}
Hope this helps!
10-19-2010 08:35 AM
Thanks for the post pearlchen!
That is a very informative post that will definitely help others ![]()
10-19-2010 11:36 AM
hi guys,
I'm reading about BlackBerry Push Service, when in fact they use technology.
One question, the Facebook application using BlackBerry Push Service?
10-20-2010 12:03 PM
Yes the BlackBerry Facebook application uses the Push Service
10-26-2010 09:54 AM
Good information.
It would be great to see a RIM tutorial on registering the push listeners on startup and processing pushes while the application may be in the background. I think that a lot of devs would find it helpful to see the RIM best practices for handling that in WebWorks.
11-29-2010 02:44 PM
I have implemented the requestForeground code for when my app receives a push, but it doesn't make the app bring itself to the foreground. I am testing on the Blackberry Torch device. When the app is open, it receives the messages OK. i do notice the activity animation on the top right of the screen when i should be receiving a notification, but nothing happens ![]()
I handle exiting the app by implementing blackberry.app.event.onExit(handleExit); and in the handleExit function i call blackberry.app.requestBackground();
any suggestions?
11-29-2010 02:47 PM
Binnyb,
I've posted some code recently that I use in production which brings the application to the foreground, processes the push request, and then sends the app back to the background if necessary. It may help you to glance and compare with your own...
http://supportforums.blackberry.com/t5/Web-Develop
11-29-2010 03:04 PM - edited 11-29-2010 03:10 PM
I have compared the code from my project, your link, and this original posting's code, and i still cannot make the app bring itself to the foreground, ugh!!
is it maybe an issue with the torch device?
blackberry 9800
6.0 bundle 695(v6.0.0.246, platform 6.4.0.105)
using widget sdk: 1.5.0.201009142140-18
blackberry web plug-in: 2.5.0.201009142140-15
11-29-2010 03:12 PM
just something to check...
is the blackberry.app feature defined in your config.xml?
I have it working on a Torch (6.0.0.246) ,3G Curve (6.0.0.3440), and a Bold 9700 (6.0.0.358)
11-29-2010 03:14 PM
Yes, i have that defined in my config.xml:
<feature id="blackberry.app" required="true" version="1.0.0.0"/>