PinchProject / Node-GCMService

Node-GCMService
15 stars 10 forks source link

Node-GCMService

NPM

NPM

Description

Node.js wrapper to send notification to Android devices using Google Cloud Messaging service.

Badges

dependencies

Installation

npm install node-gcm-service

Requirements

The only thing you need is a google server api key. For more information go to GCM getting started guide.

Usage

Require the module

var gcm = require('node-gcm-service');

Create a message object with default values and set all variables after, or with another object:

var message = new gcm.Message();

// set data with another object
message.setDataWithObject({
    key1: 'value1'
});

// add new key-value to data if key does not exists
message.addDataWithKeyValue('key2','value2');

// set collapse key
message.setCollapseKey('string');

// set dry run
message.setDryRun(false);

// set delay while idle
message.setDelayWhileIdle(true);
var message = new gcm.Message({
    collapse_key: 'test',
    data: {
        key1: 'value1'
    },
    delay_while_idle: true,
    time_to_live: 34,
    dry_run: false
});

Create a sender object with default values and set the api key after, or with another object:

var sender = new gcm.Sender();

// set api key
sender.setAPIKey('key');
var sender = new gcm.Sender({
    key: 'my_api_key'
});

Important : you can set the endpoint and end path URLs if Google change these URLs

And finally send the message to the specified registration id(s) with retries or not, in JSON format or plain-text format :

sender.sendMessage(message.toJSON(), registration_ids, true, function(err, data) {
    if (!err) {
        // do something
    } else {
        // handle error
    }
});
sender.sendMessage(message.toString(), registration_ids, false, function(err, data) {
    if (!err) {
        // do something
    } else {
        // handle error
    }
});

GCM Response

Multicast (JSON response)

{
    "multicast_id": 216,
    "success": 3,
    "failure": 3,
    "canonical_ids": 1,
    "results": [
        { "message_id": "1:0408" },
        { "error": "Unavailable" },
        { "error": "InvalidRegistration" },
        { "message_id": "1:1516" },
        { "message_id": "1:2342", "registration_id": "32" },
        { "error": "NotRegistered"}
    ]
}
{
    "multicast_id": 216,
    "success_length": 3,
    "failures_length": 3,
    "failures": {
        "NotRegistered": ["42"],
        "Unavailable": ["8"],
        "InvalidRegistration": ["15"]
    },
    "canonical_ids_length": 1,
    "canonical_ids": [
        {
            "message_id": "1:2342",
            "registration_id": "23",
            "new_registration_id": "32"
        }
    ]
}

Simple (plain-text response)

id=1:2342
registration_id=32
{
    "id": "1:2342",
    "registration_id": "32"
    "old_registration_id": "14"
}

Message class methods

Sender class methods

License

(The MIT License)

Copyright (C) 2013 PinchProject

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Credits

This module was written based on node-gcm

Change log

<<<<<<< HEAD

v0.2.7

v0.2.6

v0.2.3

v0.2.1

v0.2.0

v0.1.4

v0.1.3

v0.1.2

v0.1.1

v0.1.0