-
Notifications
You must be signed in to change notification settings - Fork 2
/
send_notifications_handler.js
79 lines (78 loc) · 2.92 KB
/
send_notifications_handler.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require('dotenv').config();
var db = require('./db_connection.js');
var tokenCache = require('./token_cache.js');
const { component, xml, jid } = require("@xmpp/component");
var fcm = require("firebase-admin");
const { text } = require('body-parser');
fcm.initializeApp({
credential: fcm.credential.applicationDefault()
});
module.exports = async function (ctx) {
var result = await handleNotification(ctx.element.getChild("publish").attrs.node, ctx.element.getChild("publish").getChild('item').getChild('notification').getChild('x').getChildren('field'));
console.log(result);
return xml(
"iq", { from: ctx.stanza.attrs.to, type: "result", to: ctx.stanza.attrs.from, id: ctx.id },
);
};
async function handleNotification(userJID, msgData) {
var msgCount = 0;
var msgSender = null;
var msgBody = null;
var targetDeviceType = null;
var targetDeviceToken = null;
var message = null;
try {
msgData.forEach(element => {
switch (element.attrs.var) {
case 'message-count':
msgCount = element.getChild("value").text();
break;
case 'last-message-sender':
msgSender = element.getChild("value").text();
break;
case 'last-message-body':
msgBody = element.getChild("value").text();
break;
default:
break;
}
});
if ((msgSender == null) || (msgBody == null)) return null;
if (tokenCache.hasOwnProperty(userJID)) {
targetDeviceToken = tokenCache[userJID].notification_token;
targetDeviceType = tokenCache[userJID].device_type;
} else {
var resp = await db.searchDB(userJID);
if (resp.hasError == false) {
targetDeviceToken = resp.notification_token;
targetDeviceType = resp.device_type;
tokenCache[userJID] = { notification_token: resp.notification_token, device_type: resp.device_type };
}
}
if ((targetDeviceToken == null) || (targetDeviceType == null)) return null;
if (targetDeviceType == 1) { // 1 = Android so it will trigger FCM notification
message = {
data: {
fromJid: msgSender.toString(),
toUsername: userJID.toString(),
count: msgCount.toString(),
msgBody: msgBody.toString()
},
android: {
priority: "high"
},
token: targetDeviceToken
};
console.log(message);
var resp = await fcm.messaging().send(message, false);
console.log(resp);
return resp;
}
else {
return null;
}
} catch (e) {
//console.log(e);
return null;
}
}