-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcm.js
55 lines (48 loc) · 1.21 KB
/
fcm.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
45
46
47
48
49
50
51
52
53
54
55
const https = require('https');
require('dotenv').config();
exports.sendToDevice = (
deviceToken, fcmPayload,
) => {
const {
callId, messageText, localNumber, remoteNumber, roomId, roomToken,
} = fcmPayload;
const data = JSON.stringify({
to: deviceToken,
data: {
UUID: callId,
message: messageText,
localPhoneNumber: localNumber,
remotePhoneNumber: remoteNumber,
roomId,
roomToken,
},
notification: {
title: 'Video Call',
body: `Video Call from ${localNumber}`,
click_action: `${process.env.ENABLX_VIDEO_WEBAPP}/confo.html?token=${roomToken}`,
icon: 'img/enablex-logo.png',
},
});
const options = {
hostname: 'fcm.googleapis.com',
port: 443,
path: '/fcm/send',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
Authorization: `key=${process.env.FIREBASE_SERVER_KEY}`,
},
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (error) => {
console.error(error);
});
req.write(data);
req.end();
};