Node-Red: Push Notifications to Google Cloud Messaging GCM

The following instructions will allow Android devices to receive notifications from Node-red flows using the Google Cloud Messaging infra-structure. This is achieved by using the Node-js library for accessing GCM (Google Cloud Messaging) node-gcm.

Installing node-gcm:

We need to install the Google Cloud Messaging node support module either locally on Node-red or globally. If installing locally just cd to the node-red directory and do npm install node-gcm. Globally the command needs the -g switch added like this: sudo npm install node-gcm -g.

Configuring Node-red:

To be possible to invoke Node-gcm functions from Node-red steps, like functions, we need to make the node-gcm module available to Node-Red. For that, go to the base directory of Node-red and edit the settings.js file.

Modify the file so that the following configuration is now defined:

functionGlobalContext: {
// os:require('os'),
// bonescript:require('bonescript'),
// arduino:require('duino')
 gcm:require('node-gcm')
},
Starting up Node-red and our first push notification to GCM:
Start up Node-red by using the command line: node red.js -s settings.js . It’s important and imperative that we now start uo with the modified settings.js file or some other file with the above configuration.
We can now add a function step with the following code to push a notification:
var gcm = context.global.gcm;
/// Define our message:
var message = new gcm.Message(
  { collapseKey: ‘node-red’,
     delayWhileIdle: true,
     timeToLive: 3,
     data: { key1: ‘Node-Red Notification’, key2: ‘Hello android from Node-red’ }
  }
);
// Set up the sender with you API key
var sender = new gcm.Sender(‘AIza…’); // Replace with your KEY!!!!!
// Add the registration IDs of the devices you want to send to
var registrationIds = [];
registrationIds.push(‘APA…..’);  // Replace with your device registration ID!!!!!
// Send the message
// … trying only once
sender.sendNoRetry(message, registrationIds, function(err, result)
    { if(err)
           console.error(err);
       else
           console.log(result);
    });
return msg;
And that’s it. Note that on the Data JSON object the key names, key1 and key2 are arbitrary. The keys can have any valid name as long that the receiving application knows about them.
For testing I’m using an inject node to activate the above function and I can see at the console (Not on the Debug tab) the following:
{ multicast_id: 9123452345452345, success: 1, failure: 0, canonical_ids: 0, results: [ { message_id: ‘0:7957832452%3676573df353’ } ] }

 

If you receive other messages, like failure:1, there is something wrong with your Google GCM key or your registration device id is invalid.

For example:

{ multicast_id: 5439642763222344000, success: 0, failure: 1, canonical_ids: 0, results: [ { error: ‘InvalidRegistration’ } ] }

This means that the registrationId.push value doesn’t belong to any registered device to our GCM project.

If you just receive 401 that means that you’re using an Invalid Browser GCM key. Check the Google developer console to get the correct key.

Testing:

I’ve done my basic testing with the sample Google GCM client available at https://code.google.com/p/gcm/source/checkout.

I’ve used Android Studio and the provided gcm-client source code to compile and test the communication.

The problem with the above gcm-client code is that we need first to compile and deploy at the device (or emulator) to make the GCM client application to at least run at least once to get the registration id of the device, so that we can provide the registration key to the above node-red code.

We also can change the provided Google code to send to Node-red the registration ID. This process alows devices to inform the Node-red flows of their registation ID, and for that we need to change the gcm-client program adding a registration process and a registration server on our side.

Advertisements

Android SDK and NetBeans

I’m user of Netbeans for my Java related work.

The Android SDK instructions refer the use of Eclipse as the IDE of choice, but is also possible to use Netbeans with the NBAndroid plugin, available at http://www.nbandroid.org/

Just following the instructions for installing the plugin in into Netbeans, namely by adding the plugin URL and installing them.

One issue that I’ve found was that the New Project Wizard didn’t show the Android type applications for creation…

For making on the New Project Wizard the Android Projects available to be created, you must enable the JAVA ME plugin, so just go again to Plugins and select JAVA ME and press Enable at the bottom of the window.

It should work now.