-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.js
44 lines (39 loc) · 1.2 KB
/
firebase.js
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
const admin = require('firebase-admin');
require('dotenv').config();
const logger = require('./logger');
// eslint-disable-next-line import/no-dynamic-require
const serviceAccount = require(process.env.FIREBASE_SERVICE_ACCOUNT);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
const payload = {
data: {},
};
const options = {
priority: 'high',
timeToLive: 60 * 60 * 24,
};
// send push notification to android devices using firebase
exports.sendToDevice = (
deviceToken, fcmPayload,
) => {
const {
callId, messageText, localNumber, remoteNumber, roomId, roomToken,
} = fcmPayload;
payload.data.uuid = callId;
payload.data.message = messageText;
payload.data.localPhoneNumber = localNumber;
payload.data.remotePhoneNumber = remoteNumber;
payload.data.roomId = roomId;
payload.data.roomToken = roomToken;
admin.messaging().sendToDevice(deviceToken, payload, options)
.then((response) => {
logger.info('Successfully sent message:');
logger.info(JSON.stringify(response));
})
.catch((error) => {
logger.info('Error sending message:');
logger.info(JSON.stringify(error));
});
};