Git Product home page Git Product logo

node-gcm-ccs's Introduction

Getting Started

Install via

npm install git+https://github.com/jacobp100/node-gcm-ccs.git

Use via

var GCM = require('node-gcm-ccs');
var gcm = GCM(<project number>, <api key>);

Getting an API Key

  • Go to https://console.developers.google.com, create a project and open it
  • Navigate to overview, and you'll see your <project id> at the top
  • Go to APIs & auth -> APIs, add Google Cloud Messaging for Android
  • Go to APIs & auth -> credentials, and create a new public <api key>

Functions

Send Message

Use send to send a message.

gcm.send(to, data, [options, callback(error, messageId, to)]);
Argument Details
to A single user
data Data to be sent to the client
options (optional) See Message Paremeters from https://developer.android.com/google/gcm/server.html#send-msg. If delivery_receipt_requested = true, an event will be sent when the message is received by the target.
callback (optional) function(error, messageId, to) called back individually for each target.

End Connection

gcm.end;

Events

Events are defined as below.

gcm.on('message', function(messageId, from, category, data)); // Messages received from client (excluding receipts)
gcm.on('receipt', function(messageId, from, category, data)); // Only fired for messages where options.delivery_receipt_requested = true

gcm.on('connected', console.log);
gcm.on('disconnected', console.log);
gcm.on('online', console.log);
gcm.on('error', console.log);

Example

var GCM = require('node-gcm-ccs');
var gcm = GCM(<project id>, <api key>);

gcm.on('message', function(messageId, from, category, data) {
	console.log('received message', arguments);
});

gcm.on('receipt', function(messageId, from, category, data) {
	console.log('received receipt', arguments);
});

gcm.send(<device id>, { message: 'hello world' }, { delivery_receipt_requested: true }, function(err, messageId, to) {
	if (!err) {
		console.log('sent message to', to, 'with message_id =', messageId);
	} else {
		console.log('failed to send message');
	}
});

Echo Client

gcm.on('message', function(_, from, __, data) {
	gcm.send(from, data);
});

Notes on GCM

  • No events are emitted from GCM or this library when a device new registers: you'll have to send a message from the device and process it yourself
  • This library doesn't have functions to create user notifications (https://developer.android.com/google/gcm/notifications.html). However, if you implement this yourself, you'll be able to send to a user group by paassing the notification_key_name as a device_id for gcm.send.
  • Occasionally, GCM performs load balancing, so the connection is sometimes restarted. This library handles this transparently, and your messages will be queued in these situations.

node-gcm-ccs's People

Contributors

jacobp100 avatar thomasdelaet avatar devoto13 avatar haemp avatar joselpez avatar nsiitk avatar sainaetr avatar lalon avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.