박권수

Merge branch 'server' into web

......@@ -8,7 +8,7 @@ const Mongoose = require('mongoose');
const api = require('./src/api');
const MqttServer = require('./src/util/MqttServer');
const BatchSystem = require('./src/util/Batch');
const FCM = require('./src/util/FCM');
// const FCM = require('./src/util/FCM');
require('dotenv').config();
// eslint-disable-next-line no-undef
......@@ -38,7 +38,7 @@ app.use(router.routes()).use(router.allowedMethods());
app.listen(SERVER_PORT, () => {
console.log('\x1b[1;36mPORT : ', SERVER_PORT, 'is connected\x1b[0m');
MqttServer.on();
FCM.initializeFCM();
// FCM.initializeFCM();
BatchSystem.removeQrCode();
BatchSystem.pushNotifyByDosage();
});
\ No newline at end of file
......
......@@ -7,6 +7,7 @@ const hub = require('./hub');
const medicine = require('./medicine');
const doctor = require('./doctor');
const manage = require('./manage');
const test = require('./test');
const api = new Router();
......@@ -17,5 +18,6 @@ api.use('/hub', hub.routes());
api.use('/medicine', medicine.routes());
api.use('/doctor', doctor.routes());
api.use('/manage', manage.routes());
api.use('/test', test.routes());
module.exports = api;
\ No newline at end of file
......
//test용 api입니다.
const Router = require('koa-router');
const testCtrl = require('./test.ctrl');
const test = new Router();
//푸쉬메시지 테스트
test.post('/fcm', testCtrl.sendFcmMessage);
module.exports = test;
//테스트용 api service
const { sendPushMessage } = require('../../util/FCM');
exports.sendFcmMessage = async ctx => {
const { deviceToken, title, body } = ctx.request.body;
try {
await sendPushMessage(ctx.request.body);
} catch(e) {
console.log('Error at FCM Sending : ', e);
}
};
\ No newline at end of file
const fcm = require('firebase-admin');
// const fcm = require('firebase-admin');
const axios = require('axios');
exports.initializeFCM = () => {
fcm.initializeApp({
credential : fcm.credential.applicationDefault(),
});
};
// exports.initializeFCM = () => {
// fcm.initializeApp({
// credential: fcm.credential.applicationDefault(),
// });
// };
exports.sendPushMessage = async ({ deviceToken, title, body }) => {
const notifyMessage = {
// const notifyMessage = {
// notification : {
// title,
// body,
// },
// token : deviceToken,
// };
// fcm.messaging().send(notifyMessage).then(res => {
// console.log(res);
// }).catch(e => {
// console.log(e);
// });
const message = {
to : deviceToken,
notification : {
title,
body,
priority : 'high',
},
token : deviceToken,
};
fcm.messaging().send(notifyMessage);
data : null,
}
const url = 'https://fcm.googleapis.com/fcm/send';
const result = await axios.post(url, message, {
headers : {
'Content-Type' : 'application/json',
// eslint-disable-next-line no-undef
'Authorization' : `key=${process.env.FCM_KEY}`,
},
});
console.log(result.data);
};
\ No newline at end of file
......