This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
/
pushClient.js
276 lines (248 loc) · 9.23 KB
/
pushClient.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
/*global window, FileReader, blackberry, autoExit, utils */
/*
* Copyright 2014 BlackBerry Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var pushClient = {
/**
* lastActivity: Keeps track of the time of the last push activity.
* pushService: Our PushService object for this session.
*/
'lastActivity': 0,
'pushService': 0,
'queue': [],
'waiting': false,
/**
* ops: You will need to populate these with your own Push credentials.
* The invokeTarketId needs to match the custom invoke-target in your
* config.xml.
*
* For more information on registering for Push credentials, please see:
* https://developer.blackberry.com/services/push/
*/
'ops': {
'invokeTargetId': 'com.@@@@@@.pushclient.invoke.push',
'appId': '@@@@-@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',
'ppgUrl': 'http://cp@@@@.pushapi.eval.blackberry.com'
},
/**
* Responsible for creating the PushService object.
*/
'register': function () {
/* Retrieve the last time (in milliseconds) of Push activity. */
pushClient.lastActivity = window.parseInt(window.localStorage.lastActivity || 0, 10);
if (pushClient.pushService === 0) {
/* We only need a new object if we don't already have one; i.e. once per launch. */
utils.log('Creating PushService object.');
try {
/* This is the core Push functionality to create the PushService object. */
blackberry.push.PushService.create(
pushClient.ops,
pushClient.createSuccess,
pushClient.createFailure,
pushClient.simChangeCallback,
pushClient.pushTransportReadyCallback
);
} catch (err) {
utils.log(err);
}
} else {
/* Forcing Push Service registration. */
utils.log('PushService object already exists.');
pushClient.registerChannel();
}
},
/**
* If the PushService object is created / exists, proceed to registering the Push Channel.
*/
'createSuccess': function (result) {
/* On success, the PushService object is passed into this function. */
utils.log('PushService created successfully.');
pushClient.pushService = result;
/* Accept pushes if the application is not running. */
pushClient.pushService.launchApplicationOnPush(
true,
function launchApplicationOnPushCallback(result) {
/* Log whether we will be launching on Push invocations. */
if (result === blackberry.push.PushService.SUCCESS) {
utils.log('Application will be launched on push.');
} else {
utils.log('Application will not be launched on push: ' + result);
}
}
);
if (new Date().getTime() - pushClient.lastActivity > 2 * 24 * 60 * 60 * 1000) {
/* If we've gone more than two days without any activity, recreate the Push Channel. This is subjective. */
utils.log('Expired Push Channel registration.');
pushClient.registerChannel();
} else {
/* We have seen activity within two days, so likely everything is okay, take no action. */
utils.log('No need to recreate the Push Channel.');
}
},
/**
* When required, we will register a new Push Channel with the Push Service. It is NOT
* required to create a new Push Channel every time. Once created, a Push Channel will
* tend to last the lifetime of an application unless the Push Channel is intentionally
* destroyed. There are some rare cases that can lead to a Push Channel becoming invalid.
* To counteract this, we have implemented a two-day expectation on Push activity in the
* createSuccess function above. If we do not see any activity within two days, we will
* force the Push Channel to be recreated. The two days are purely arbitrary/subjective,
* you will need to decide on an appropriate timeline for your own applications.
*/
'registerChannel': function () {
try {
/* Call the createChannel API to open communication with the Push services. */
utils.log('Creating Push Channel.');
pushClient.pushService.createChannel(
function createChannelCallback(result, token) {
if (result === blackberry.push.PushService.SUCCESS) {
/* Channel was successfully created, update Push activity. */
utils.log('Successfully created Push Channel.');
window.localStorage.lastActivity = new Date().getTime().toString();
} else {
/* Channel failed to be created. */
utils.log('Failed to create Push Channel: ' + result);
}
}
);
} catch (err) {
utils.log(err);
}
},
'createFailure': function (result) {
utils.log('PushService creation error: ' + result);
},
'simChangeCallback': function () {
utils.log('SIM Card has changed.');
},
'pushTransportReadyCallback': function (lastFailedOperation) {
utils.log('Last failed operation: ' + lastFailedOperation);
},
/**
* If we need to intentionally destroy an existing Push Channel, we can do so
* with this API.
*/
'unregister': function () {
try {
/* Call the destroyChannel API to cease communication with Push services. */
utils.log('Destroying Push Channel.');
pushClient.pushService.destroyChannel(
function destroyChannelCallback(result) {
if (result === blackberry.push.PushService.SUCCESS) {
/* Channel was successfully destroyed, reset Push activity. */
utils.log('Successfully destroyed Push Channel.');
window.localStorage.lastActivity = 0;
} else {
/* Channel could not be destroyed. */
utils.log('Failed to destroy Push Channel: ' + result);
}
}
);
} catch (err) {
utils.log(err);
}
},
/**
* Iterate recursively through the pushClient.queue array until we've processed all pushes.
*/
'processQueue': function (invokeRequest) {
var pushPayload, reader;
/* Check if there is anything left to process. */
if (!invokeRequest) {
/* We've processed everything. */
utils.log('Processing complete.');
pushClient.waiting = false;
/* If we were processing pushes in the background, exit the app. */
if (autoExit === true) {
utils.log('Exit application.');
/* blackberry.app.exit(); */
}
return;
}
try {
/* Extract the payload from the Push Invocation. */
pushPayload = pushClient.pushService.extractPushPayload(invokeRequest);
/* Process a text data payload. */
reader = new FileReader();
reader.onload = function (result) {
var text = result.target.result;
utils.log(text);
utils.log('Processing next item.');
pushClient.processQueue(pushClient.queue.shift());
};
reader.onerror = function (result) {
utils.log('Error converting blob to text: ' + result.target.error);
utils.log('Processing next item.');
pushClient.processQueue(pushClient.queue.shift());
};
reader.readAsText(pushPayload.data, 'UTF-8');
} catch (err) {
utils.log(err);
}
},
/**
* Our application was invoked before the PushService object had a chance to be created.
* We'll keep checking periodically until the object is ready and then process any
* outstanding pushes that we've received in the meantime.
*/
'waitForPushService': function () {
if (pushClient.pushService === 0) {
/* We still don't have a PushService object, wait a little longer. */
utils.log('Waiting.');
window.setTimeout(pushClient.waitForPushService, 100);
} else {
/* We have a PushService object, begin processing from the beginning of the queue. */
utils.log('Processing push queue.');
pushClient.processQueue(pushClient.queue.shift());
}
},
/**
* This function will be called when a Push Invocation is received. In this example,
* we are assuming a text-based data payload (see pushInitiator.js) to be received.
* This is the most common case for many applications.
*/
'onInvoke': function (invokeRequest) {
/* Ensure the invocation has an action associated with it. */
if (invokeRequest.action) {
/* Only process Push Invocations. */
if (invokeRequest.action === 'bb.action.PUSH') {
/* Update our Push Activity to track this received push. */
utils.log('Push invocation received.');
window.localStorage.lastActivity = new Date().getTime();
/* Add this invokeRequest to our processing queue. */
utils.log('Added new push to queue.');
pushClient.queue.push(invokeRequest);
/* Wait for the PushService object if we need to. */
if (pushClient.pushService === 0) {
if (pushClient.waiting === false) {
pushClient.waiting = true;
/* Begin waiting for PushService object. */
utils.log('Waiting for PushService object.');
window.setTimeout(pushClient.waitForPushService, 100);
}
} else if (pushClient.waiting === false) {
pushClient.waiting = true;
/* We have a PushService object, begin processing from the beginning of the queue. */
utils.log('Processing push queue.');
pushClient.processQueue(pushClient.queue.shift());
}
} else {
utils.log('Invocation received: ' + invokeRequest.action);
}
} else {
utils.log('Invocation received but no associated action.');
}
}
};