-
Notifications
You must be signed in to change notification settings - Fork 123
/
push-notifications.js
191 lines (167 loc) · 4.65 KB
/
push-notifications.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* eslint-disable import/no-import-module-exports */
import sendGCM from './sendGCM';
import APN from './sendAPN';
import sendADM from './sendADM';
import sendWNS from './sendWNS';
import sendWebPush from './sendWeb';
import {
DEFAULT_SETTINGS,
UNKNOWN_METHOD,
WEB_METHOD,
WNS_METHOD,
ADM_METHOD,
GCM_METHOD,
APN_METHOD,
} from './constants';
class PN {
constructor(options) {
this.setOptions(options);
}
setOptions(opts) {
this.settings = { ...DEFAULT_SETTINGS, ...opts };
if (this.apn) {
this.apn.shutdown();
}
this.apn = new APN(this.settings.apn);
}
sendWith(method, regIds, data, cb) {
return method(regIds, data, this.settings)
.then((results) => {
(cb || ((noop) => noop))(null, results);
return results;
})
.catch((error) => {
(cb || ((noop) => noop))(error);
return Promise.reject(error);
});
}
getPushMethodByRegId(regId) {
if (typeof regId === 'object' && (!regId.type || !regId.id)) {
return { regId, pushMethod: WEB_METHOD };
}
if (typeof regId === 'object' && regId.id && regId.type) {
return {
regId: regId.id,
pushMethod: this.settings.isAlwaysUseFCM ? GCM_METHOD : regId.type,
};
}
// TODO: deprecated, remove of all cases below in v3.0
// and review test cases
if (this.settings.isAlwaysUseFCM) {
return { regId, pushMethod: GCM_METHOD };
}
if (regId.substring(0, 4) === 'http') {
return { regId, pushMethod: WNS_METHOD };
}
if (/^(amzn[0-9]*.adm)/i.test(regId)) {
return { regId, pushMethod: ADM_METHOD };
}
if (
(regId.length === 64 || regId.length === 160) &&
/^[a-fA-F0-9]+$/.test(regId)
) {
return { regId, pushMethod: APN_METHOD };
}
if (regId.length > 64) {
return { regId, pushMethod: GCM_METHOD };
}
return { regId, pushMethod: UNKNOWN_METHOD };
}
send(_regIds, data, callback) {
const promises = [];
const regIdsGCM = [];
const regIdsAPN = [];
const regIdsWNS = [];
const regIdsADM = [];
const regIdsWebPush = [];
const regIdsUnk = [];
const regIds = Array.isArray(_regIds || []) ? _regIds || [] : [_regIds];
// Classify each pushId for corresponding device
regIds.forEach((regIdOriginal) => {
const { regId, pushMethod } = this.getPushMethodByRegId(regIdOriginal);
if (pushMethod === WEB_METHOD) {
regIdsWebPush.push(regId);
} else if (pushMethod === GCM_METHOD) {
regIdsGCM.push(regId);
} else if (pushMethod === WNS_METHOD) {
regIdsWNS.push(regId);
} else if (pushMethod === ADM_METHOD) {
regIdsADM.push(regId);
} else if (pushMethod === APN_METHOD) {
regIdsAPN.push(regId);
} else {
regIdsUnk.push(regId);
}
});
try {
// Android GCM
if (regIdsGCM.length > 0) {
promises.push(this.sendWith(sendGCM, regIdsGCM, data));
}
// iOS APN
if (regIdsAPN.length > 0) {
promises.push(
this.sendWith(this.apn.sendAPN.bind(this.apn), regIdsAPN, data)
);
}
// Microsoft WNS
if (regIdsWNS.length > 0) {
promises.push(this.sendWith(sendWNS, regIdsWNS, data));
}
// Amazon ADM
if (regIdsADM.length > 0) {
promises.push(this.sendWith(sendADM, regIdsADM, data));
}
// Web Push
if (regIdsWebPush.length > 0) {
promises.push(this.sendWith(sendWebPush, regIdsWebPush, data));
}
} catch (err) {
promises.push(Promise.reject(err));
}
// Unknown
if (regIdsUnk.length > 0) {
const results = {
method: 'unknown',
success: 0,
failure: regIdsUnk.length,
message: [],
};
regIdsUnk.forEach((regId) => {
results.message.push({
regId,
error: new Error('Unknown registration id'),
});
});
promises.push(Promise.resolve(results));
}
// No regIds detected
if (promises.length === 0) {
promises.push(
Promise.resolve({
method: 'none',
success: 0,
failure: 0,
message: [],
})
);
}
return Promise.all(promises)
.then((results) => {
const cb = callback || ((noop) => noop);
cb(null, results);
return results;
})
.catch((err) => {
const cb = callback || ((noop) => noop);
cb(err);
return Promise.reject(err);
});
}
}
module.exports = PN;
module.exports.WEB = WEB_METHOD;
module.exports.WNS = WNS_METHOD;
module.exports.ADM = ADM_METHOD;
module.exports.GCM = GCM_METHOD;
module.exports.APN = APN_METHOD;