forked from benjie/phonegap-parse-plugin
-
Notifications
You must be signed in to change notification settings - Fork 104
/
ParsePushPluginProxy.js
363 lines (311 loc) · 12.7 KB
/
ParsePushPluginProxy.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
var jstz = require('parse-push-plugin.jsTimezoneDetect');
var pushNotifications = Windows.Networking.PushNotifications;
var localSettings = Windows.Storage.ApplicationData.current.localSettings.values;
cordova.commandProxy.add("ParsePushPlugin", {
registerCallback: function (successCb, errorCb) {
var pnCallback = successCb;
//localSettings.remove(ParseUtil._installationKey);
ParseUtil.initializeViaConfigXML();
WNSUtil.registerChannelAsync(pnCallback).then(function (channel) {
ParseUtil.saveInstallationAsync(channel);
}, function (err) {
console.error("Unable to register WNS channel. Error: " + JSON.stringify(err));
errorCb(err);
});
},
getInstallationId: function (successCb, errorCb) {
var installation = ParseUtil.getCurrentInstallation();
if (installation.objectId) {
successCb(installation.installationId);
} else {
var msg = "You don't have a stored installation";
console.error(msg);
errorCb(msg);
}
},
getInstallationObjectId: function(){
var installation = ParseUtil.getCurrentInstallation();
if (installation.objectId) {
successCb(installation.objectId);
} else {
var msg = "You don't have a stored installation";
console.error(msg);
errorCb(msg);
}
},
getSubscriptions: function(successCb, errorCb){
successCb(ParseUtil.getCurrentInstallation().channels || []);
},
subscribe: function (successCb, errorCb, argArray) {
var channel = argArray[0];
ParseUtil.subscribeAsync(channel).then(function () {
successCb()
}, errorCb);
},
unsubscribe: function(successCb, errorCb, argArray){
var channel = argArray[0];
ParseUtil.unsubscribeAsync(channel).then(function () {
successCb()
}, errorCb);
},
register: function(successCb, errorCb){
//noop
}
});
var ParseUtil = {
_installationKey: "ParsePushPluginInstallation",
_parseVersion: "1.7.0.0", //we're actually using the REST api, so this is just a filler
_serverUrl: null,
_xhrHeaders: {},
_hexOctet: function () {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
},
_generateId: function () {
return this._hexOctet() + this._hexOctet() + '-'
+ this._hexOctet() + '-'
+ this._hexOctet() + '-'
+ this._hexOctet() + '-'
+ this._hexOctet() + this._hexOctet() + this._hexOctet();
},
_formUrl: function(tail){
return [this._serverUrl, tail].join('');
},
initializeViaConfigXML: function(){
this._xhrHeaders = {
"Content-Type": "application/json",
"X-Parse-Application-Id": CordovaUtil.getPreference("ParseAppId")
};
this._serverUrl = CordovaUtil.getPreference("ParseServerUrl");
if (this._serverUrl === "PARSE_DOT_COM") {
//legacy mode
this._serverUrl = "https://api.parse.com/1/";
this._xhrHeaders["X-Parse-Windows-Key"] = CordovaUtil.getPreference("ParseWindowsKey");
}
if (!this._serverUrl.endsWith('/')) {
this._serverUrl += '/';
}
},
getCurrentInstallation: function(){
var stringCache = localSettings[ParseUtil._installationKey];
if (stringCache) {
return JSON.parse(stringCache);
} else {
//return a new unsaved installation object
var appMeta = CordovaUtil.getAppMeta();
var deviceMeta = {
deviceType: 'winrt',
deviceUris: null, //initialized deviceUris to null, but must be set appropriately before/during save
timeZone: LocaleUtil.getIANATimeZone(),
localeIdentifier: LocaleUtil.getLocalId(),
};
var installationMeta = {
parseVersion: ParseUtil._parseVersion,
installationId: ParseUtil._generateId()
};
return Object.assign({}, appMeta, deviceMeta, installationMeta);
}
},
_updateInstallationAsync: function (installationData) {
console.log("db-update installation objectId: " + installationData.objectId);
return WinJS.xhr({
type: "PUT",
url: ParseUtil._formUrl('installations/' + installationData.objectId),
headers: ParseUtil._xhrHeaders,
data: JSON.stringify(installationData)
});
},
_createInstallationAsync: function (installationData) {
console.log("db-create new installation: " + installationData.installationId);
return WinJS.xhr({
type: "POST",
url: ParseUtil._formUrl('installations'),
headers: ParseUtil._xhrHeaders,
data: JSON.stringify(installationData)
});
},
_saveLocalInstallation: function (installationData) {
//
// save to WinJS's local storage mechanism
localSettings[this._installationKey] = JSON.stringify(installationData);
},
saveInstallationAsync: function (wnsChannel) {
var installationData = this.getCurrentInstallation();
if (wnsChannel) {
var storedUri = (installationData.deviceUris || {})._Default;
if (installationData.objectId && storedUri === wnsChannel.uri) {
//
// channel has not changed, no need to save to database
return WinJS.Promise.as(installationData);
} else {
installationData.deviceUris = { _Default: wnsChannel.uri };
var promise = (installationData.objectId) ? ParseUtil._updateInstallationAsync(installationData) : ParseUtil._createInstallationAsync(installationData);
//
// store newly saved object to local settings cache
return promise.then(function (xhr) {
var response = JSON.parse(xhr.response);
if (response.objectId) {
installationData.objectId = response.objectId;
}
ParseUtil._saveLocalInstallation(installationData);
return installationData;
});
}
} else {
throw "Missing parameter 'wnsChannel'";
}
},
subscribeAsync: function (channel) {
var installationData = ParseUtil.getCurrentInstallation();
if (!installationData || !installationData.objectId) {
var msg = "You have not saved an installation. Can't subscribe.";
console.error(msg);
return WinJS.Promise.wrapError(msg);
}
if (!installationData.channels)
installationData.channels = [];
if (installationData.channels.indexOf(channel) > -1) {
console.log("Noop. Already subscribed to channel: " + channel);
return WinJS.Promise.as(true);
} else {
installationData.channels.push(channel);
}
return ParseUtil._updateChannelsAsync(installationData);
},
unsubscribeAsync: function (channel) {
var installationData = ParseUtil.getCurrentInstallation();
if (!installationData || !installationData.objectId) {
var msg = "You have not saved an installation. Can't unsubscribe.";
console.error(msg);
return WinJS.Promise.wrapError(msg);
}
if (!installationData.channels) {
var msg = "You have not subscribed to any channel. Can't unsubscribe";
console.warn(msg);
return WinJS.Promise.wrapError(msg);
}
var index = installationData.channels.indexOf(channel);
if (index >= 0) {
installationData.channels.splice(index, 1);
} else {
var msg = "You are not subscribed to: '" + channel + "'. Can't unsubscribe";
console.warn(msg);
return WinJS.Promise.wrapError(msg);
}
return ParseUtil._updateChannelsAsync(installationData);
},
_updateChannelsAsync: function (installationData) {
return WinJS.xhr({
type: "PUT",
url: ParseUtil._formUrl('installations/' + installationData.objectId),
headers: ParseUtil._xhrHeaders,
data: JSON.stringify({ channels: installationData.channels })
}).then(function (xhr) {
ParseUtil._saveLocalInstallation(installationData);
return true;
});
}
};
var WNSUtil = {
registerChannelAsync: function (pnCallback) {
//
// Note: WNS channels can expire (30 days) and a call to createPushNotificationChannelForApplicationAsync() may return
// a new channel so call this function each time the app starts. Let MS take care of returning same channel
// or new one.
//
// Readings:
// https://msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-windows-push-notification-services--wns--overview
// https://msdn.microsoft.com/en-us/library/windows/apps/hh465412.aspx
//
var onPushReceive = function(e) {
debugger;
console.log("***** ONPUSH RECEIVE", e);
WNSUtil._onPushReceive(e, pnCallback);
}
return pushNotifications.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync().then(
function (wnsChannel) {
wnsChannel.addEventListener("pushnotificationreceived", onPushReceive);
wnsChannel.onpushnotificationreceived = onPushReceive;
console.log('channel', wnsChannel);
WNSUtil.channel = wnsChannel; //save channel so we can unregister events
//var context = cordova.require('cordova/platform').activationContext;
//console.log('CONTEXt', context);
return wnsChannel;
}
);
},
_onPushReceive: function (e, pnCallback) {
var pnPayload;
switch (e.notificationType) {
case pushNotifications.PushNotificationType.toast:
console.log('toast', e);
pnPayload = e.toastNotification.content.getXml();
break;
case pushNotifications.PushNotificationType.tile:
console.log('tile', e);
pnPayload = e.tileNotification.content.getXml();
break;
case pushNotifications.PushNotificationType.badge:
console.log('badge', e);
pnPayload = e.badgeNotification.content.getXml();
break;
case pushNotifications.PushNotificationType.raw:
console.log('raw', e);
pnPayload = e.rawNotification.content;
break;
}
console.log('payload', pnPayload);
e.cancel = true;
pnCallback(pnPayload, "RECEIVE");
}
};
var CordovaUtil = {
_configXML: null,
getConfigXML: function () {
if (!this._configXML) {
//
// load configXML synchronously.
// making this synchronous simplifies the rest of the code because the rest of
// the code depends on these config parameters
//
var req = new XMLHttpRequest();
req.open("GET", "../config.xml", false); //false -> synchronous
req.send();
this._configXML =req.responseXML.querySelector("widget");
}
return this._configXML;
},
getAppMeta: function(){
var configXML = this.getConfigXML();
return {
appName: configXML.querySelector("name").textContent,
appIdentifier: configXML.getAttribute("id"),
appVersion: configXML.getAttribute("version"),
}
},
getPreference: function(name){
var configXML = this.getConfigXML();
var selector = "preference[name='" + name + "']";
return configXML.querySelector(selector).getAttribute("value");
},
getPreferences: function (prefNames) {
//
// Input: array of preference names
// Output: Object of preferences with preference names as keys
//
var configXML = this.getConfigXML();
var prefs = {};
prefNames.forEach(function (name) {
var selector = "preference[name='" + name + "']";
prefs[name] = configXML.querySelector(selector).getAttribute("value");
});
return prefs;
}
}
var LocaleUtil = {
getLocalId: function(){
return Intl.DateTimeFormat().resolvedOptions().locale || navigator.browserLanguage || navigator.language;
},
getIANATimeZone: function(){
return jstz.determine().name();
}
}