├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── constants └── constants.json ├── functions ├── delete.js ├── devices.js ├── register.js └── send-message.js ├── models └── device.js ├── package.json ├── public ├── img │ └── icon.png ├── index.html └── js │ └── main.js └── routes └── routes.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Learn2Crack 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # android-gcm-server-node 2 | 3 | Visit the following url and register your app. Download the **google-services.json** file and note the API Key. 4 | 5 | https://developers.google.com/mobile/add?platform=android&cntapi=gcm 6 | 7 | Modify **constants.json** file in constants directory. Paste your GCM server key for the JSON object **gcm_api_key**. 8 | 9 | ## Download dependencies 10 | Open terminal and use the command. 11 | 12 | > npm install 13 | 14 | ## Executing the project 15 | Open terminal and use the command. 16 | 17 | > node app 18 | 19 | ## Complete tutorial 20 | 21 | https://www.learn2crack.com/2016/05/gcm-push-notification-server-node-js-mongodb.html 22 | 23 | ## Youtube Video Demo 24 | https://www.youtube.com/watch?v=J8XqOQQmvgI 25 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var bodyParser = require('body-parser'); 3 | var app = express(); 4 | var port = process.env.PORT || 8080; 5 | var logger = require('morgan'); 6 | var io = require('socket.io'); 7 | 8 | app.use(express.static(__dirname + '/public')); 9 | app.use(bodyParser.json()); 10 | app.use(logger('dev')); 11 | 12 | 13 | var listen = app.listen(port); 14 | var socket = io.listen(listen); 15 | 16 | require('./routes/routes')(app,socket); 17 | 18 | console.log('The App runs on port ' + port); -------------------------------------------------------------------------------- /constants/constants.json: -------------------------------------------------------------------------------- 1 | { 2 | "gcm_api_key" :"Your GCM Server Key", 3 | 4 | "error" : { 5 | 6 | "msg_invalid_param" : { 7 | 8 | "result" : "error", 9 | "message" : "Invalid request parameters." 10 | 11 | }, 12 | 13 | "msg_empty_param" : { 14 | 15 | "result" :"error", 16 | "message" :"Parameters should not be empty." 17 | 18 | }, 19 | 20 | "msg_reg_failure" : { 21 | 22 | "result" :"error", 23 | "message" :"Registration failure." 24 | 25 | }, 26 | 27 | "msg_reg_exists" : { 28 | 29 | "result" :"error", 30 | "message" :"Device already registered." 31 | 32 | }, 33 | 34 | "msg_del_failure" : { 35 | 36 | "result" : "error", 37 | "message" : "Unable to remove device" 38 | 39 | }, 40 | 41 | "msg_send_failure" : { 42 | 43 | "result" : "error", 44 | "message" : "Unable to send message" 45 | 46 | } 47 | 48 | }, 49 | 50 | "success" : { 51 | 52 | "msg_reg_success" : { 53 | 54 | "result" : "success", 55 | "message" : "Device successfully registered." 56 | 57 | }, 58 | 59 | "msg_del_success" : { 60 | 61 | "result" : "success", 62 | "message" : "Device successfully removed" 63 | 64 | }, 65 | 66 | "msg_send_success" : { 67 | 68 | "result" : "success", 69 | "message" : "Message successfully sent !" 70 | 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /functions/delete.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var request = require('request'); 3 | var device = require('../models/device'); 4 | var constants = require('../constants/constants.json'); 5 | 6 | 7 | exports.removeDevice = function(registrationId,callback){ 8 | 9 | 10 | device.findOneAndRemove({registrationId:registrationId},function(err){ 11 | 12 | 13 | if (!err) { 14 | 15 | callback(constants.success.msg_del_success); 16 | 17 | } else { 18 | 19 | callback(constants.error.msg_del_failure); 20 | } 21 | 22 | }); 23 | 24 | } -------------------------------------------------------------------------------- /functions/devices.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var request = require('request'); 3 | var device = require('../models/device'); 4 | var constants = require('../constants/constants.json'); 5 | 6 | 7 | exports.listDevices = function(callback) { 8 | 9 | device.find( {}, {_id : false,__v : false }, function(err,devices){ 10 | 11 | if(!err){ 12 | 13 | callback(devices) 14 | 15 | } 16 | }); 17 | } -------------------------------------------------------------------------------- /functions/register.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var request = require('request'); 3 | var device = require('../models/device'); 4 | var constants = require('../constants/constants.json'); 5 | 6 | 7 | exports.register = function(deviceName,deviceId,registrationId,callback){ 8 | 9 | 10 | var newDevice = new device({ 11 | 12 | deviceName : deviceName, 13 | deviceId : deviceId, 14 | registrationId : registrationId 15 | 16 | }); 17 | 18 | 19 | device.find({registrationId : registrationId}, function(err,devices){ 20 | 21 | var totalDevices = devices.length; 22 | 23 | if (totalDevices == 0) { 24 | 25 | newDevice.save(function(err){ 26 | 27 | if (!err) { 28 | 29 | callback(constants.success.msg_reg_success); 30 | 31 | } else { 32 | 33 | callback(constants.error.msg_reg_failure); 34 | 35 | } 36 | }); 37 | } else { 38 | 39 | callback(constants.error.msg_reg_exists); 40 | 41 | } 42 | 43 | 44 | }); 45 | 46 | } -------------------------------------------------------------------------------- /functions/send-message.js: -------------------------------------------------------------------------------- 1 | var gcm = require('node-gcm'); 2 | var constants = require('../constants/constants.json'); 3 | 4 | exports.sendMessage = function(message,registrationId,callback){ 5 | 6 | var message = new gcm.Message({data: {message: message}}); 7 | var regTokens = [registrationId]; 8 | var sender = new gcm.Sender(constants.gcm_api_key); 9 | sender.send(message, { registrationTokens: regTokens }, function (err, response) { 10 | 11 | if (err){ 12 | 13 | console.error(err); 14 | callback(constants.error.msg_send_failure); 15 | 16 | } else { 17 | 18 | console.log(response); 19 | callback(constants.success.msg_send_success); 20 | } 21 | 22 | }); 23 | 24 | } -------------------------------------------------------------------------------- /models/device.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | 3 | var Schema = mongoose.Schema; 4 | 5 | var deviceSchema = mongoose.Schema({ 6 | 7 | deviceName : String, 8 | deviceId : String, 9 | registrationId : String 10 | 11 | }); 12 | 13 | mongoose.connect('mongodb://localhost:27017/node-android-push'); 14 | 15 | module.exports = mongoose.model('device', deviceSchema); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gcm-push", 3 | "version": "0.0.1", 4 | "main": "app.js", 5 | "dependencies": { 6 | "body-parser": "^1.15.0", 7 | "express": "^4.13.4", 8 | "mongoose": "^4.4.12", 9 | "morgan": "^1.7.0", 10 | "node-gcm": "^0.14.0", 11 | "socket.io": "^1.4.5" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /public/img/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Learn2Crack/android-gcm-server-node/994fc97df151832d5d3737db99e5644c20369a66/public/img/icon.png -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |