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)?
Asked
Active
Viewed 3,272 times
1 Answers
4
- Android device should sent a device token to the server to be able to receive notifications. Here an example how to do it.
- Get gcm key to send pushes from server.
- Send push with
node-gcm
package from npm with node.js app.
Basic example:
const gcm = require('node-gcm'); //Google Cloud Messaging
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');
}
});

TBouder
- 2,539
- 1
- 14
- 29

Paul Rumkin
- 6,737
- 2
- 25
- 35
-
Thanks buddy. How can I test this on my localhost? I am building the server and need to test things before. – Nimit Bedi Oct 19 '16 at 16:11
-
You can do it without any server with example code. All you need is to set proper gcmKey and deviceToken and run script. As I know push messages could not be received in emulator. Thus you need to install development version of your application into device and copy deviceToken from it manually or sending it via network to your local IP address. – Paul Rumkin Oct 19 '16 at 16:35