Google Cloud Messaging (GCM) in Node.js

Google Cloud Messaging (GCM) in Node.js



Introduction

GCM (Google Cloud Messaging) is a service provides by google that is used to send data to android device. It is free service from google that helps to send data from servers to android applications.

How to get Google APIs Key:

1. Create a new project from Google APIs Console page.
2. After creating project note down project id from url. Later it will be used as a sender id.
3. After that click on Services.
4. Once you are done, Click on API Access and write down the API Key. This key will be used for sending request to GCM Server.

How to use GCM service in our server side.

Install: npm install android-gcm

Simplest Code :

var gcm = equire('android-gcm');
var gcmObject = new gcm.AndroidGcm('API Key');  //get from Google APIs Console page.  

var message = new gcm.Message({ 
 registration_ids: ['device tokens1','device tokens2'],  
 data: {   
   title: 'message title here',    
  message: 'your message here' 
 }});  
   gcmObject.send(message, function(err,response){   
     callback(err,response);  
   });
});



Source : Google Cloud