You first need to register for the Blackberry Push service here
They will then send you an email in a couple of days with the app ID, port and push initiator password etc.
On the client side you can then listen for push requests:
var port = 100; // Change this to the port you received from RIM
var appId = "The app ID you received from RIM";
var serverUrl = "http://pushapi.eval.blackberry.com";
var max = 100;
var wakeUpPage = "index.html";
function init() {
var ops = {port : port, appId : appId, serverUrl : serverUrl, wakeUpPage : wakeUpPage, maxQueueCap : max};
blackberry.push.openBISPushListener(ops, pushReceive, pushRegister, simChange);
}
function pushReceive(data) {
try {
var message = blackberry.utils.blobToString(data.payload);
return 0;
}
catch(err) {
}
}
function pushRegister(status) {
if (status == 0) {
alert("success");
}
else if (status == 1) {
alert("network error");
}
else if (status == 2) {
alert("rejected by server");
}
else if (status == 3) {
alert("invalid parameters");
}
else if (status == -1) {
alert("general error");
}
else {
alert("unknown status");
}
}
window.onload = function() {
init();
}
Also make sure you have <feature id="blackberry.push" /> and <feature id="blackberry.utils" /> in your config.xml.
What I also noted was that when the app is closed the pushReceive() doesn't get called. You can bypass this by running the app in the background:
function handleExit() {
blackberry.app.requestBackground();
}
blackberry.app.event.onExit(handleExit);
and use blackberry.app.requestForeground(); to bring app to the front.
The other thing which took me a while to figure out was that it might take a while before pushRegister() is called (this registers the PIN on BIS).
So make sure this gets called otherwise you will never be able to receive push messages.