Nimit Bedi Nimit Bedi - 1 month ago 20
Node.js Question

Google Cloud Messaging in Node js

What are the procedures to follow in using GCM for a node based application?
Are there specific codes required on both server side(Node js) and client side(Android/iOS)?

Answer
  1. Android device should sent a device token to the server to be able to receive notifications. Here an example how to do it.
  2. Get gcm key to send pushes from server.
  3. Send push with node-gcm package from npm with node.js app.

Basic example:

const gsm = require('node-gsm');
const gcmKey = ''; // Your gcm key in quotes
const deviceToken = ''; // Receiver device token
const sender = new gcm.Sender(gcmKey);

var message = new gcm.Message();

message.addData({
  title: 'Push',
  body: 'This is push notification',
  otherProperty: true,
});

sender.send(message, {registrationIds: [token]}, (err) => {
  if (err) {
    console.error(err);
  }
  else {
    console.log('Sent');
  }
});
Comments