-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
77 lines (66 loc) · 1.89 KB
/
index.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
"use strict";
// UrbanAirshipPushAdapter is an adapter for Parse Server push
const typeToAudience = {
"ios": "device_token",
"android": "apid",
"winrt": "wns",
"winphone": "mpns",
"dotnet": "wns"
}
const UrbanAirshipPush = require('urban-airship-push');
function UrbanAirshipPushAdapter(pushConfig) {
this.validPushTypes = ['ios', 'android', 'winrt', 'winphone', 'dotnet'];
pushConfig = pushConfig || {};
this.config = {};
this.config.key = pushConfig.key;
this.config.secret = pushConfig.secret;
this.config.masterSecret = pushConfig.masterSecret;
this.UA = new UrbanAirshipPush(this.config);
}
/**
* Get an array of valid push types.
* @returns {Array} An array of valid push types
*/
UrbanAirshipPushAdapter.prototype.getValidPushTypes = function() {
return this.validPushTypes;
}
UrbanAirshipPushAdapter.prototype.send = function(data, installations) {
const requests = UrbanAirshipPushAdapter.createRequests(data, installations);
return new Promise((resolve, reject) => {
this.UA.push.send(requests, function(err, res){
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
UrbanAirshipPushAdapter.createRequests = function(data, installations) {
return installations.map(function(installation){
const type = typeToAudience[installation.deviceType];
if (!type) {
return;
}
const deviceToken = installation.deviceToken;
if (!deviceToken) {
return;
}
if (!data.data) {
return;
}
const audience = {};
audience[type] = deviceToken;
const specificData = {};
specificData[installation.deviceType] = data.data;
return {
audience: audience,
notification: specificData,
device_types: "all"
}
}).filter(function(obj){
return obj !== undefined;
});
}
module.exports = UrbanAirshipPushAdapter;
module.exports.default = UrbanAirshipPushAdapter;