-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Servidor Node.js Heroku Push Notificaciones Firebase
- Loading branch information
Anahi Salgado
authored and
Anahi Salgado
committed
Jun 14, 2016
1 parent
3c915f7
commit 8c84e24
Showing
887 changed files
with
105,505 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: node index.js |
39 changes: 39 additions & 0 deletions
39
FirebasePushNotificationsWithServerNodeJS/server/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# node-js-getting-started | ||
|
||
A barebones Node.js app using [Express 4](http://expressjs.com/). | ||
|
||
This application supports the [Getting Started with Node on Heroku](https://devcenter.heroku.com/articles/getting-started-with-nodejs) article - check it out. | ||
|
||
## Running Locally | ||
|
||
Make sure you have [Node.js](http://nodejs.org/) and the [Heroku Toolbelt](https://toolbelt.heroku.com/) installed. | ||
|
||
```sh | ||
$ git clone git@github.com:heroku/node-js-getting-started.git # or clone your own fork | ||
$ cd node-js-getting-started | ||
$ npm install | ||
$ npm start | ||
``` | ||
|
||
Your app should now be running on [localhost:5000](http://localhost:5000/). | ||
|
||
## Deploying to Heroku | ||
|
||
``` | ||
$ heroku create | ||
$ git push heroku master | ||
$ heroku open | ||
``` | ||
or | ||
|
||
[![Deploy to Heroku](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy) | ||
|
||
## Documentation | ||
|
||
For more information about using Node.js on Heroku, see these Dev Center articles: | ||
|
||
- [Getting Started with Node.js on Heroku](https://devcenter.heroku.com/articles/getting-started-with-nodejs) | ||
- [Heroku Node.js Support](https://devcenter.heroku.com/articles/nodejs-support) | ||
- [Node.js on Heroku](https://devcenter.heroku.com/categories/nodejs) | ||
- [Best Practices for Node.js Development](https://devcenter.heroku.com/articles/node-best-practices) | ||
- [Using WebSockets on Heroku with Node.js](https://devcenter.heroku.com/articles/node-websockets) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "Node.js Getting Started", | ||
"description": "A barebones Node.js app using Express 4", | ||
"repository": "https://github.com/heroku/node-js-getting-started", | ||
"logo": "http://node-js-sample.herokuapp.com/node.svg", | ||
"keywords": ["node", "express", "static"], | ||
"image": "heroku/nodejs" | ||
} |
133 changes: 133 additions & 0 deletions
133
FirebasePushNotificationsWithServerNodeJS/server/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
var express = require('express'); | ||
var app = express(); | ||
app.set('port', (process.env.PORT || 5000)); | ||
|
||
var bodyParser = require("body-parser"); | ||
app.use(bodyParser.json()); //soporte para codificar json | ||
app.use(bodyParser.urlencoded({ extended: true })); //Soporte para decodificar las url | ||
|
||
var firebase = require("firebase"); | ||
firebase.initializeApp({ | ||
serviceAccount: "TU_ARCHIVO_JSON_AQUI", | ||
databaseURL: "LA_URL_BASEDATOS_FIREBASE" | ||
}); | ||
|
||
var FCM = require('fcm-push'); | ||
|
||
|
||
app.use(express.static(__dirname + '/public')); | ||
|
||
// views is directory for all template files | ||
app.set('views', __dirname + '/views'); | ||
app.set('view engine', 'ejs'); | ||
|
||
app.get('/android', function(request, response) { | ||
response.render('pages/index'); | ||
}); | ||
|
||
//POST | ||
//https://afternoon-lowlands-49612.herokuapp.com/token-device | ||
//token | ||
//animal | ||
var tokenDevicesURI = "token-device"; | ||
app.post('/' + tokenDevicesURI, function(request, response) { | ||
var token = request.body.token; | ||
var animal = request.body.animal; | ||
var db = firebase.database(); | ||
var tokenDevices = db.ref(tokenDevicesURI).push(); | ||
tokenDevices.set({ | ||
token: token, | ||
animal: animal | ||
}); | ||
|
||
var path = tokenDevices.toString(); //https://project-4284821003924177471.firebaseio.com/token-device/-KJlTaOQPwP-ssImryV1 | ||
var pathSplit = path.split(tokenDevicesURI + "/") | ||
var idAutoGenerado = pathSplit[1]; | ||
|
||
var respuesta = generarRespuestaAToken(db, idAutoGenerado); | ||
response.setHeader("Content-Type", "application/json"); | ||
response.send(JSON.stringify(respuesta)); | ||
}); | ||
|
||
function generarRespuestaAToken(db, idAutoGenerado) { | ||
var respuesta = {}; | ||
var usuario = ""; | ||
var ref = db.ref("token-device"); | ||
ref.on("child_added", function(snapshot, prevChildKey) { | ||
usuario = snapshot.val(); | ||
respuesta = { | ||
id: idAutoGenerado, | ||
token: usuario.token, | ||
animal: usuario.animal | ||
}; | ||
}); | ||
return respuesta; | ||
} | ||
|
||
//GET | ||
//https://afternoon-lowlands-49612.herokuapp.com/toque-animal | ||
//id | ||
//animal | ||
app.get("/toque-animal/:id/:animal", function(request, response){ | ||
var id = request.params.id; | ||
var animal = request.params.animal; | ||
|
||
var db = firebase.database(); | ||
var ref = db.ref("token-device/" + id); | ||
var usuario = ""; | ||
var respuesta = {}; | ||
|
||
ref.on("value", function(snapshot) { | ||
console.log(snapshot.val()); | ||
usuario = snapshot.val(); | ||
var mensaje = animal + " te dio un toque"; | ||
enviarNotificaion(usuario.token, mensaje); | ||
respuesta = { | ||
id: id, | ||
token: usuario.token, | ||
animal: usuario.animal | ||
}; | ||
response.send(JSON.stringify(respuesta)); | ||
}, function (errorObject) { | ||
console.log("The read failed: " + errorObject.code); | ||
respuesta = { | ||
id: "", | ||
token: "", | ||
animal: "" | ||
}; | ||
response.send(JSON.stringify(respuesta)); | ||
|
||
}); | ||
}); | ||
|
||
function enviarNotificaion(tokenDestinatario, mensaje) { | ||
var serverKey = 'TU_APYKEY'; | ||
var fcm = new FCM(serverKey); | ||
|
||
var message = { | ||
to: tokenDestinatario, // required | ||
collapse_key: '', | ||
data: {}, | ||
notification: { | ||
title: 'Notificacion desde Servidor', | ||
body: mensaje, | ||
icon: "notificacion", | ||
sound: "default", | ||
color: "#00BCD4" | ||
} | ||
}; | ||
|
||
fcm.send(message, function(err, response){ | ||
if (err) { | ||
console.log("Something has gone wrong!"); | ||
} else { | ||
console.log("Successfully sent with response: ", response); | ||
} | ||
}); | ||
} | ||
|
||
app.listen(app.get('port'), function() { | ||
console.log('Node app is running on port', app.get('port')); | ||
}); | ||
|
||
|
Binary file not shown.
Oops, something went wrong.