├── .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 | Push Notification 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 |
26 | 27 |
28 | 29 | 30 | 31 | 32 | Total Registered Devices : {{deviceCount}} 33 | 34 | 35 | 36 | 37 | 38 |
39 | 40 |

{{ device.deviceName }}

41 |

IMEI : {{ device.deviceId }}

42 | 43 | delete 44 | 45 | 46 |
47 | 48 |
49 | 50 |
51 |
52 |
53 |
54 |
55 | 56 |
57 | 58 | -------------------------------------------------------------------------------- /public/js/main.js: -------------------------------------------------------------------------------- 1 | var app = angular.module('Push', ['ngMaterial']); 2 | 3 | var socket = io(); 4 | 5 | 6 | app.controller('pushController', function($scope,$window,$http,$mdDialog,$mdToast) { 7 | 8 | socket.on('update', function (data) { 9 | 10 | socket.emit('update','ACK from Client !'); 11 | 12 | if (data.update) { 13 | 14 | getDevices(); 15 | } 16 | }); 17 | 18 | getDevices(); 19 | 20 | function getDevices(){ 21 | 22 | $http.get('/devices').then(function(response){ 23 | 24 | $scope.devices = response.data; 25 | $scope.deviceCount = response.data.length; 26 | 27 | }); 28 | } 29 | 30 | function deleteDevice(index,devices) { 31 | 32 | var registrationId = $scope.devices[index].registrationId; 33 | 34 | $http.delete('/devices/'+registrationId).then(function(response) { 35 | 36 | if (response.data.result == 'success') { 37 | 38 | devices.splice(index,1); 39 | $scope.deviceCount = devices.length; 40 | 41 | } 42 | 43 | }) 44 | 45 | }; 46 | 47 | 48 | $scope.showDeleteConfirmDialog = function(ev,index,devices) { 49 | 50 | var deviceName = $scope.devices[index].deviceName; 51 | 52 | var confirm = $mdDialog.confirm() 53 | .title('Delete device !') 54 | .textContent('Are you sure want to delete ' +deviceName +' ?') 55 | .targetEvent(ev) 56 | .ok('Delete Device') 57 | .cancel('Cancel'); 58 | 59 | $mdDialog.show(confirm).then(function() { 60 | 61 | deleteDevice(index,devices); 62 | 63 | }, function() { 64 | 65 | console.log('cancel'); 66 | 67 | }); 68 | }; 69 | 70 | 71 | $scope.showSendMessageDialog = function(ev,index) { 72 | 73 | var confirm = $mdDialog.prompt() 74 | .title('Send Message to '+ $scope.devices[index].deviceName) 75 | .textContent('Type a message and click Send Message.') 76 | .placeholder('Hello !') 77 | .targetEvent(ev) 78 | .ok('Send Message') 79 | .cancel('Cancel'); 80 | 81 | $mdDialog.show(confirm).then(function(result) { 82 | 83 | $scope.progress = true; 84 | var message = result; 85 | var registrationId = $scope.devices[index].registrationId; 86 | console.log('okay'+ message +registrationId); 87 | 88 | var params = { 89 | 90 | message:message, 91 | registrationId:registrationId 92 | }; 93 | 94 | $http.post('/send', params).then(function(response) { 95 | 96 | $scope.progress = false; 97 | 98 | showAlert(ev,response.data.message); 99 | 100 | 101 | },function(response){ 102 | 103 | console.log(response); 104 | 105 | }); 106 | 107 | }, function() { 108 | 109 | console.log('cancel'); 110 | 111 | }); 112 | }; 113 | 114 | function showAlert(ev,message) { 115 | 116 | $mdDialog.show( 117 | $mdDialog.alert() 118 | .clickOutsideToClose(true) 119 | .title('Alert !') 120 | .textContent(message) 121 | .ariaLabel('Alert Dialog') 122 | .ok('Okay') 123 | .targetEvent(ev) 124 | ); 125 | } 126 | 127 | }); -------------------------------------------------------------------------------- /routes/routes.js: -------------------------------------------------------------------------------- 1 | var constants = require('../constants/constants.json'); 2 | var registerFunction = require('../functions/register'); 3 | var devicesFunction = require('../functions/devices'); 4 | var deleteFunction = require('../functions/delete'); 5 | var sendFunction = require('../functions/send-message'); 6 | 7 | module.exports = function(app,io) { 8 | 9 | 10 | io.on('connection', function(socket){ 11 | 12 | console.log("Client Connected"); 13 | socket.emit('update', { message: 'Hello Client',update:false }); 14 | 15 | socket.on('update', function(msg){ 16 | 17 | console.log(msg); 18 | }); 19 | }); 20 | 21 | app.get('/',function(req,res) { 22 | 23 | res.sendFile('index.html'); 24 | 25 | }); 26 | 27 | app.post('/devices',function(req,res) { 28 | 29 | var deviceName = req.body.deviceName; 30 | var deviceId = req.body.deviceId; 31 | var registrationId = req.body.registrationId; 32 | 33 | if ( typeof deviceName == 'undefined' || typeof deviceId == 'undefined' || typeof registrationId == 'undefined' ) { 34 | 35 | console.log(constants.error.msg_invalid_param.message); 36 | 37 | res.json(constants.error.msg_invalid_param); 38 | 39 | } else if ( !deviceName.trim() || !deviceId.trim() || !registrationId.trim() ) { 40 | 41 | console.log(constants.error.msg_empty_param.message); 42 | 43 | res.json(constants.error.msg_empty_param); 44 | 45 | } else { 46 | 47 | registerFunction.register( deviceName, deviceId, registrationId, function(result) { 48 | 49 | res.json(result); 50 | 51 | if (result.result != 'error'){ 52 | 53 | io.emit('update', { message: 'New Device Added',update:true}); 54 | 55 | } 56 | }); 57 | } 58 | }); 59 | 60 | app.get('/devices',function(req,res) { 61 | 62 | devicesFunction.listDevices(function(result) { 63 | 64 | res.json(result); 65 | 66 | }); 67 | }); 68 | 69 | app.delete('/devices/:device',function(req,res) { 70 | 71 | var registrationId = req.params.device; 72 | 73 | deleteFunction.removeDevice(registrationId,function(result) { 74 | 75 | res.json(result); 76 | 77 | }); 78 | 79 | 80 | }); 81 | 82 | app.post('/send',function(req,res){ 83 | 84 | var message = req.body.message; 85 | var registrationId = req.body.registrationId; 86 | 87 | sendFunction.sendMessage(message,registrationId,function(result){ 88 | 89 | res.json(result); 90 | }); 91 | }); 92 | 93 | } --------------------------------------------------------------------------------