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

Web and WebWorks Development

Reply
New Contributor
pearlchen
Posts: 5
Registered: ‎10-14-2010
My Device: Bold 9000

Push notifications when widget is in background

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-Development/How-to-Create-a-Push-Widget/ta-p/494141

 

(2) In your app, make sure that you give permissions to:

  • blackberry.app
  • blackberry.app.event
  • blackberry.push
  • blackberry.utils

 

(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!

 

Please use plain text.
Retired
tneil
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Push notifications when widget is in background

Thanks for the post pearlchen!

 

That is a very informative post that will definitely help others :smileyhappy:

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Please use plain text.
Trusted Contributor
chicoxml
Posts: 227
Registered: ‎09-03-2010
My Device: Not Specified

Re: Push notifications when widget is in background

hi guys,

I'm reading about BlackBerry Push Service, when in fact they use technology.

One question, the Facebook application using BlackBerry Push Service?

Please use plain text.
Retired
tneil
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Push notifications when widget is in background

Yes the BlackBerry Facebook application uses the Push Service

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Please use plain text.
Developer
jmace
Posts: 482
Registered: ‎10-24-2008
My Device: 9300

Re: Push notifications when widget is in background

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.

_________________________________________
www.jasonmace.com/blackberry
Please use plain text.
Contributor
binnyb
Posts: 19
Registered: ‎11-02-2010
My Device: Not Specified

Re: Push notifications when widget is in background

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 :smileysad:

 

I handle exiting the app by implementing blackberry.app.event.onExit(handleExit); and in the handleExit function i call blackberry.app.requestBackground();

 

any suggestions?

Please use plain text.
Developer
jmace
Posts: 482
Registered: ‎10-24-2008
My Device: 9300

Re: Push notifications when widget is in background

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-Development/Complet-Widget-Push-example-code/td-p/650819

_________________________________________
www.jasonmace.com/blackberry
Please use plain text.
Contributor
binnyb
Posts: 19
Registered: ‎11-02-2010
My Device: Not Specified

Re: Push notifications when widget is in background

[ Edited ]

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

Please use plain text.
Developer
jmace
Posts: 482
Registered: ‎10-24-2008
My Device: 9300

Re: Push notifications when widget is in background

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)

_________________________________________
www.jasonmace.com/blackberry
Please use plain text.
Contributor
binnyb
Posts: 19
Registered: ‎11-02-2010
My Device: Not Specified

Re: Push notifications when widget is in background

Yes, i have that defined in my config.xml:

 

<feature id="blackberry.app" required="true" version="1.0.0.0"/>

Please use plain text.