/
PushNotification.js
51 lines (46 loc) · 1.18 KB
/
PushNotification.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
/* globals Push */
class PushNotification {
getNotificationId(roomId) {
let serverId = RocketChat.settings.get('uniqueID');
return this.hash(`${serverId}|${roomId}`); // hash
}
hash(str) {
let hash = 0,
i = str.length;
while (i) {
hash = ((hash << 5) - hash) + str.charCodeAt(--i);
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
send({ roomName, roomId, username, message, usersTo, payload }) {
let title;
if (roomName && roomName !== '') {
title = `${roomName}`;
message = `${username}: ${message}`;
} else {
title = `${username}`;
}
let icon = RocketChat.settings.get('Assets_favicon_192').url || RocketChat.settings.get('Assets_favicon_192').defaultUrl;
const config = {
from: 'push',
badge: 1,
sound: 'default',
title: title,
text: message,
payload,
query: usersTo,
notId: this.getNotificationId(roomId),
gcm: {
style: 'inbox',
summaryText: '%n% new messages',
image: RocketChat.getURL(icon, { full: true })
},
apn: {
text: title + ((title !== '' && message !== '') ? '\n' : '') + message
}
};
return Push.send(config);
}
}
RocketChat.PushNotification = new PushNotification();