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
digitalTBM
Posts: 7
Registered: ‎12-15-2011
My Device: Developer

pushing notifications from a website using WebWorks

I really need to create a blackberry version of a simple iPhone application that downloads data from a website and when the website is updated a push notification is sent to the device.

 

I would prefer to build my BB application using Webworks as I already have a mobile version and I thought I may be able to get the PIN number from the device on login and then send a notification to the PIN from the website.

 

Having done some further research into this, it seems not possible?

 

I would really appreciate it if someone could help me work out the process to do this as it does seem to be overly complicated.

New Contributor
digitalTBM
Posts: 7
Registered: ‎12-15-2011
My Device: Developer

Re: pushing notifications from a website using WebWorks

Just to add to this....

 

I don't want to be restricted to using only BES or BIS if possible and I have seen lots of tutorials on this but I would really appreciate an explanation as to if what I'm trying to acheive is possible and the best way to acheive it.

 

Thanks again

Developer
shnuffy
Posts: 268
Registered: ‎08-19-2009
My Device: Bold 9700

Re: pushing notifications from a website using WebWorks

Push is only available on BES and BIS on BlackBerry. BIS, i.e., a data plan, is what most consumers have. I'm not sure what you mean by you don't want to use BES or BIS.

 

WebWorks also has an API that you can use to get the PIN of the device. You'll have to create your own methods for returning that PIN to your service, like a POST to your website or however you want to do it. 

 

WebWorks also has push APIs available, of which there are many examples and tutorials on this board and the Push board on this forum. 

 

Sounds like you want to do something like this:

1. User installs app

2. User logs into website. At this point, send your server the PIN and register the app to listen for pushes.

3. Server-side, push out updates to whichever PINs you want

New Contributor
digitalTBM
Posts: 7
Registered: ‎12-15-2011
My Device: Developer

Re: pushing notifications from a website using WebWorks

Hi Shnuffy

 

Thanks for the reply. That is exactly what I want to do. (sorry, I meant I don't want to be restricted to use one but not the other, but I may have misunderstood the options)

 

Could you please point me in the direction of a tutorial that will send push from a PHP page to a series of Blackberry PIN numbers.

 

You may be correct that there are many tutorials but I may be struggling to see the woord for the trees so to speak.

 

Many thanks again

Developer
shnuffy
Posts: 268
Registered: ‎08-19-2009
My Device: Bold 9700

Re: pushing notifications from a website using WebWorks

You need to apply and obtain from RIM free evaluation push credentials. They will email you back with an appID, a URL to push to, and a password. You can use those items to plug into the code below to test your push. This will help you get the server-side stuff working.

 

Read through the documentation here to get the client-side stuff working. It contains all the APIs available (only a couple, which makes it "nice and easy") and some example code that you can use.

https://bdsc.webapps.blackberry.com/html5/apis/blackberry.push.html

 

Server-side push tests code:

 

<?php
if ($_POST) {
    // APP ID provided by RIM
    $appid = [YOUR APP ID]; 

    // Password provided by RIM
    $password = [YOUR RIM PROVIDED PASSWORD];
    
    //Deliver before timestamp
    $deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+5 minutes'));
    
    //An array of address must be in PIN format or "push_all"
    $addresstosendto[] = $_POST['pin']; // PIN TAKEN FROM FORM
    
    $addresses = '';
    foreach ($addresstosendto as $value) {
        $addresses .= '<address address-value="' . $value . '"/>';
    }
    
    // create a new cURL resource
    $err = false;
    $ch = curl_init();
    $messageid = microtime(true);

    $data = '--mPsbVQo0a68eIL3OAxnm'. "\r\n" .
    'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" .
    '<?xml version="1.0"?>
    <!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
    <pap>
    <push-message push-id="' . $messageid . '" deliver-before-timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">'
    . $addresses .
    '<quality-of-service delivery-method="unconfirmed"/>
    </push-message>
    </pap>' . "\r\n" .
    '--mPsbVQo0a68eIL3OAxnm' . "\r\n" .
    'Content-Type: text/plain' . "\r\n" .
    'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
    stripslashes($_POST['message']) . "\r\n" .
    '--mPsbVQo0a68eIL3OAxnm--' . "\n\r";

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "https://[RIM PUSH SERVER]/mss/PD_pushRequest");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, "BB Push Server/1.0");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=mPsbVQo0a68eIL3OAxnm; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive"));

    // grab URL and pass it to the browser
    $xmldata = curl_exec($ch);
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    
    //Start parsing response into XML data that we can read and output
    $p = xml_parser_create();
    xml_parse_into_struct($p, $xmldata, $vals);
    $errorcode = xml_get_error_code($p);
    if ($errorcode > 0) {
        $err = true;
    }
    xml_parser_free($p);

    echo 'Our PUSH-ID: ' . $messageid . "<br \>\n";
    if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') {
        echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n";
        echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n";
        echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n";
        echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n";
    } elseif ($err) {
        echo '<p>An XML parser error has occured</p>' . "\n";
        echo '<pre>' . xml_error_string($errorcode) ."</pre>\n";
        echo '<p>Response</p>' . "\n";
        echo '<pre>' . $xmldata . '</pre>' . "\n";
    } else {
        echo '<p>An error has occured</p>' . "\n";
        echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n";
        echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n";
    }
} else {
    showhtml();
}

function showhtml() {
    ?>
<html>
<head><title>Blackberry Push</title></head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>Message to push:</p>
<textarea name="message" rows="5" cols="50"></textarea>
<input name="pin" value='FF0000'></input>
<p></p><input type="submit" value="Push Data"></p>
</form>
</body>
</html>
<?php
}
?>
New Contributor
digitalTBM
Posts: 7
Registered: ‎12-15-2011
My Device: Developer

Re: pushing notifications from a website using WebWorks

Thank you shnuffy, that is very helpful

 

I will give this a try!

New Contributor
digitalTBM
Posts: 7
Registered: ‎12-15-2011
My Device: Developer

Re: pushing notifications from a website using WebWorks

Hi Shnuffy

 

I can get the php code to submit and I get Response DESC: The request has been accepted for processing. however I never receive the push notification on my blackberry.

 

Also the quota on my RIM evaluation account doesn't ever reduce so it doesn't seem to be working correctly.

 

I would appreciate any advice

Trusted Contributor
foufou
Posts: 109
Registered: ‎11-15-2011
My Device: blackberry 9800

Re: pushing notifications from a website using WebWorks

Hi digitalTBM

 

Did you find the problem? Why you didn't receive notification. I get the same message "The request has been accepted for processing. I didn't  receive the push notification on my blackberry".

New Contributor
digitalTBM
Posts: 7
Registered: ‎12-15-2011
My Device: Developer

Re: pushing notifications from a website using WebWorks

Hi

 

No i haven't managed to get it to work. I don't know if ut's to do with the app listening for pushes or the PHP code that's not connecting.

 

It would be great if I could get a "hello world" example of code for a index.html that listens for the push to go with the PHP code here. 

 

Thanks

 

 

Developer
shnuffy
Posts: 268
Registered: ‎08-19-2009
My Device: Bold 9700

Re: pushing notifications from a website using WebWorks

Just to be sure, make sure your listening on HTTP (the website pushes to HTTPS) and you're listening on the correct port. 

 

///////////////////////////////////////////////////
// 	BB PUSH
///////////////////////////////////////////////////
function onRegister(status)
{
	if (status == 0)
	{
		alert("registered for push");
	}
	else
	{
		alert("push registration failed: " + status);
	}
}

function onPush(data)
{
	alert("received push: " + blackberry.utils.blobToString(data.payload));
}

function onSimChange()
{
	alert("sim changed");
	//should re-register the push listener
}

var port = 30705; 						//listener port, provided by RIM
var BBserverUrl 	= "http://pushapi.eval.blackberry.com"; 	//listen-to URL
var appId 		= "[your app id]"; 					//unique app-id, provided by RIM
var max 		= 100;					//max queue size
var wakeUpPage		= "index.html";				//if the app is closed and push comes in, wake up here

function openBISPushListener()
{
	var ops =
	{
		port : port,
		appId : appId,
		serverUrl : BBserverUrl,
		wakeUpPage : wakeUpPage,
		maxQueueCap : max
	};

	try
	{
		blackberry.push.openBISPushListener(ops, onPush, onRegister, onSimChange);
	}
	catch (err)
	{
		alert("Called openBISPushListener():Error - " + err);
	}     
}