Initiating Public Push using NodeJS

Web Development

 

push

This post was written by Naveenan Murugesu

Initiating BIS Push using NodeJS

Push is a very interesting feature to add to your application.  It can dramatically enhance the performance and the user experience of your application.  The BlackBerry Push Service has always been a major part of the BlackBerry platform and provides an extremely efficient way for delivering real-time data to users without requiring your app to run all the time.  One of the main component of the BlackBerry Push SDK is the Push Initiator.

The Push Initiator is the application that actually creates the push request and submits it to the push proxy gateway (PPG).  As a developer, you can choose the library of your choice to develop the push initiator.  In this blog post, I would like to show you how to use NodeJS to initiate the push.

NodeJS provides a very simple and efficient way to submit your push requests to the PPG.  It does not involve installing the SDK, setting up the web server and so on.  You simply have to install NodeJS and write a few lines of java script to get this working.  Let’s take a closer look.

If you have not done so already, download and install NodeJS at this link.  Next, install the node-BB10 library that allows us to push notifications to a BlackBerry 10 device.  You can do so using the following command.

 npm install node-bb10

Now, we are ready to write some code! First, let’s import the BlackBerry 10 library that allows us to push notifications.

var pushLibrary = require(‘node-BB10’);

Next, we need to create a globally unique message/push ID.  It is up to you how you want to make this ID unique. One suggestion is to tie this push ID to the time stamp.  The following will return the number of milliseconds since Jan 1st, 1970.

var messageID = new Date().getTime();

Now, create the message that needs to be pushed out and associate the message with the message ID created in the previous step.

var message = new pushLibrary.PushMessage(messageID, ‘This is test push using node.js’);

Then, we will need to inform the PPG where to deliver the push message and choose a default delivery method, which is unconfirmed.

message.addRecipient(‘<BlackBerry PIN>’);

message.setDeliveryMethod (‘unconfirmed’);

Now that we have created the message to be pushed out let’s create the initiator.  The initiator is tied with the Application ID, password and Content Provider ID (CPID), which you get from BlackBerry when you register with the BlackBerry Push Services.  You will also need to indicate whether you are using the evaluation URL or the production URL using a Boolean value as shown below.

var initiator = new pushLibrary.PushInitiator(<Application ID>,<Password>, <CPID>,true||false);

Finally link the message we created earlier to the initiator and submit the request to the Push Proxy Gateway (PPG) using the following command.

initiator.push (message, function(err, result);

That’s it.  You have submitted a push message request to the PPG and PPG will ensue that your message is delivered before its expiry time.

Stay tuned for the next blog! In the next blog, I will show you how to initiate a push request using a Cordova plugin.

 

About Lee Van Cromvoirt

As part of BlackBerry Ecosystem Team, I enjoy working to enable Enterprises and Application Developers to reach their mobile goals.

Join the conversation

Show comments Hide comments
+ -
  • http://devblog.blackberry.com/2015/04/initiating-public-push-using-javascript/ Initiating Public Push using JavaScript | BlackBerry Developer Blog

    […] Push is very interesting feature to add to your application.  It can dramatically enhance the performance and the user experience of your application.  There are several ways to initiate the push request and as a developer, you can choose the library or the programming language of your choice to develop the push initiator.  In my previous blog post, we looked at how you can initiate a push request using NodeJS. […]

blog comments powered by Disqus