diff --git a/ChatService/app/routes.js b/ChatService/app/routes.js index 54a3371db2fb8d4b81fa11ffc3f38eba51755ce5..26edb4e99b797acdc5a6efd25b38d34048451eec 100644 --- a/ChatService/app/routes.js +++ b/ChatService/app/routes.js @@ -4,7 +4,82 @@ module.exports = function(app) { var Driver = require('./models/driver'); var FcmToken = require('./models/fcmtoken'); var Chat = require('./models/chat'); - + + + // We need this to build our post string + var querystring = require('querystring'); + var http = require('http'); + var fs = require('fs'); + + function sendMessages(to, codestring) { + // Build the post string from an object + var post_data = JSON.stringify({ + "notification": { + "title": "ini judul", + "body" : codestring, + "icon" : "firebase-logo.png", + "click-action" : "" + }, + "to": to + }); + + // An object of options to indicate where to post to + var post_options = { + host: 'fcm.googleapis.com', + path: '/fcm/send', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization' : 'key=AAAAj5zwaR8:APA91bEs0mPlX_EKQFn3hBAkz7bUFJJt-3RW5eZ-EfBr4wHIhcMwRl6ka6COI3qaAU136KLjIC3TDHLS0M3QPbfG-SJLxG5fNACB9srlxViHNZkZ5TBpKNTxVkD5o9czlt0S3xeQZoGF', + 'Content-Length': Buffer.byteLength(post_data) + } + }; + + // Set up the request + var post_req = http.request(post_options, function(res) { + res.setEncoding('utf8'); + res.on('data', function (chunk) { + console.log('Response: ' + chunk); + }); + }); + + // post the data + post_req.write(post_data); + post_req.end(); + } + + + // Requirement to post to firebase-admin + // var admin = require("firebase-admin"); + // var serviceAccount = require("../config/serviceAccountKey.json"); + // console.log(serviceAccount); + + // admin.initializeApp({ + // credential: admin.credential.cert(serviceAccount), + // databaseURL: "https://chatservice-kia.firebaseio.com", + // }); + + // function sendMessages(to, message){ + // var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera) + // // to: to, + // notification: { + // title: 'Title of your push notification', + // body: 'Body of your push notification' + // }, + // data: { //you can send only notification or only data(or include both) + // // to: ro, + // message: message + // } + // }; + // admin.messaging().sendToDevice(to, message) + // .then(function(response) { + // console.log("response: ", response); + // }) + // .catch(function(err) { + // console.log("error: ", err); + // }); + // }; + // ====================== SERVER ROUTES ============================ // FOR HANDLING DRIVER STATUS @@ -82,7 +157,7 @@ module.exports = function(app) { } else { if (!foundUser){ - FcmToken.create({"id" : req.body.id, "status" : "available"}, function(error, addUser){ + FcmToken.create({"id" : req.body.id, "token" : req.body.token}, function(error, addUser){ if(!error && addUser){ var response = {"status" : 200, "message" : "User token has been added to token list"}; res.json(response); @@ -113,6 +188,19 @@ module.exports = function(app) { }); }); + app.get('/listfcmtoken',function(req, res){ + FcmToken.find({},function(err, listUser){ + if (err){ + var response = {"status" : 503, "message" : "Database error detected"}; + res.json(response); + } + else { + var response = {"status" : 200, "message" : "User token has been deleted"}; + res.json(listUser); + } + }); + }) + // CHAT SERVICE // Send message from passangger app.post('/sendmessagefrompassangger', function(req, res){ @@ -147,6 +235,8 @@ module.exports = function(app) { newChat.history = []; newChat.history.push({'from' : senderId.id, 'to' : receiverId, 'message' : message}); newChat.save(); + console.log(receiverToken); + sendMessages(receiverToken.token, message); res.json({'status' : 200, 'message' : 'chat history created and message sent.'}); } }); @@ -154,6 +244,8 @@ module.exports = function(app) { else { chat.history.push({'from' : senderId.id, 'to' : receiverId, 'message' : message}); chat.save(); + console.log(receiverToken); + sendMessages(receiverToken.token, message); res.json({'status' : 200, 'message' : 'message sent.'}); } } @@ -197,6 +289,7 @@ module.exports = function(app) { newChat.history = []; newChat.history.push({'from' : senderId.id, 'to' : receiverId, 'message' : message}); newChat.save(); + sendMessages(receiverToken.token, message); res.json({'status' : 200, 'message' : 'chat history created and message sent.'}); } }); @@ -204,6 +297,7 @@ module.exports = function(app) { else { chat.history.push({'from' : senderId.id, 'to' : receiverId, 'message' : message}); chat.save(); + sendMessages(receiverToken.token, message); res.json({'status' : 200, 'message' : 'message sent.'}); } }