This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnotifications.js
270 lines (244 loc) · 9.74 KB
/
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
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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var Event = require('cordova-plugin-chrome-apps-common.events');
var storage = require('cordova-plugin-chrome-apps-storage.Storage');
var exec = require('cordova/exec');
var channel = require('cordova/channel');
var runtime = require('cordova-plugin-chrome-apps-runtime.runtime');
var helpers = require('cordova-plugin-chrome-apps-common.helpers');
var eventsToFireOnStartUp = [];
var notifications = null;
var notificationOptions = null;
var RESOLVE_PATTERN = /^(?:chrome-extension|gopher|data|blob):/;
function resolveUri(uri) {
if (RESOLVE_PATTERN.exec(uri)) {
return uri;
}
return runtime.getURL(uri);
}
function removeNotification(notificationId) {
delete notifications[notificationId];
delete notificationOptions[notificationId];
storeNotifications();
}
function setNotification(notificationId, options) {
notifications[notificationId] = true;
// Store a copy of the options used to create/update the notification
// - Must be a separate copy, in case the caller later mutates the object
notificationOptions[notificationId] = JSON.parse(JSON.stringify(options));
storeNotifications();
}
function storeNotifications() {
storage.internal.set({'notifications':notifications, 'notificationOptions':notificationOptions});
}
function setLastError(message) {
console.error(message);
runtime.lastError = {'message':message};
}
function mergeOptions(previousOptions, newOptions) {
var mergedOptions = previousOptions ? JSON.parse(JSON.stringify(previousOptions)) : {};
// Clone the new options, so any contained objects cannot be modified
// after the merge
var newClone = JSON.parse(JSON.stringify(newOptions));
for (var propName in newClone) {
mergedOptions[propName] = newClone[propName];
}
return mergedOptions;
}
function checkNotificationOptions(options) {
// For create, there are some required properties, all others are optional
// For update, all properties are optional
// However, only some combinations are valid, even when updating. Thus,
// the caller must merge the original create options with updates for
// validation
// For consistency with the desktop API, varying validation behaviour is
// required:
// - Required options that are missing should still invoke the callback,
// but chrome.runtime.lastError needs to be set
// - Options provided with invalid values (i.e. type), should raise an error
runtime.lastError = null;
var requiredOptions = [ 'type', 'iconUrl', 'title', 'message' ];
for (var i = 0; i < requiredOptions.length; i++) {
if (!(requiredOptions[i] in options)) {
setLastError('Some of the required properties are missing: type, iconUrl, title and message.');
return false;
}
}
var permittedTypes = [ 'basic', 'image', 'list', 'progress' ];
if (permittedTypes.indexOf(options.type) == -1) {
var invalidType = 'Property \'type\': Value must be one of: ' +
JSON.stringify(permittedTypes) + '.';
console.error('Error: Invalid notification options.' + invalidType);
throw new Error(invalidType);
}
options.iconUrl = resolveUri(options.iconUrl);
if ('buttons' in options) {
for (var i = 0; i < options.buttons.length; i++) {
if (!('title' in options.buttons[i])) {
console.error('Error: Invalid notification options. Buttons must specify a title.');
return false;
} else if ('iconUrl' in options.buttons[i]) {
options.buttons[i].iconUrl = resolveUri(options.buttons[i].iconUrl);
}
}
}
if ('imageUrl' in options) {
if (options.type != 'image') {
setLastError('Image resource provided for notification type != image');
return false;
}
options.imageUrl = resolveUri(options.imageUrl);
}
if ('items' in options) {
if (options.type != 'list') {
setLastError('List items provided for notification type != list');
return false;
}
for (var i = 0; i < options.items.length; i++) {
var item = options.items[i];
if (!('title' in item)) {
console.error('Error: Invalid notification options. List items must specify a title.');
return false;
} else if (!('message' in item)) {
console.error('Error: Invalid notification options. List items must contain a message.');
return false;
}
}
}
if ('progress' in options && options.type != 'progress') {
setLastError('The progress value should not be specified for non-progress notification');
return false;
}
return true;
}
exports.create = function(notificationId, options, callback) {
if (!checkNotificationOptions(options)) {
// For consistency with desktop, invoke the callback even if the options
// are invalid
// - The validation will raise/log errors as appropriate
setTimeout(function() {
callback(notificationId);
}, 0);
return;
}
if (notificationId === '') {
notificationId = Math.floor(Math.random()*10000000000).toString();
}
setNotification(notificationId, options);
var win = function() {
callback(notificationId);
};
exec(win, undefined, 'ChromeNotifications', 'create', [notificationId, options]);
};
exports.update = function(notificationId, options, callback) {
// Merge the update options with the original options used to create
// - All properties are optional on update, but must be validated for
// invalid combinations (even if not specified)
var previousOptions = notificationOptions[notificationId];
var mergedOptions = mergeOptions(previousOptions, options);
if (!checkNotificationOptions(mergedOptions)) {
// For consistency with desktop, invoke the callback even if the options
// are invalid
// - The validation will raise/log errors as appropriate
setTimeout(function() {
callback(false);
}, 0);
return;
}
var win = function(wasUpdated) {
wasUpdated = !!wasUpdated;
if (wasUpdated) {
// Store the merged options as the original (necessary to support
// multiple updates to same notification)
setNotification(notificationId, mergedOptions);
}
callback(wasUpdated);
};
var fail = function() {
// How to set last error?
callback(false);
};
// Pass the current options, as well as the original options used to create
// - The native implementation may not have the ability to retrieve an
// existing notification to change options (i.e. Android)
// - Providing the original options allows for updates to be handled as
// "cancel previous" + "create from scratch"
// - The native side is responsible for combining the option as needed
exec(win, fail, 'ChromeNotifications', 'update', [notificationId, options, previousOptions]);
};
exports.clear = function(notificationId, callback) {
removeNotification(notificationId);
var win = function(wasCleared) {
callback(!!wasCleared);
};
exec(win, undefined, 'ChromeNotifications', 'clear', [notificationId]);
};
exports.getAll = function(callback) {
setTimeout(function() {
callback(notifications);
}, 0);
};
exports.onClosed = new Event('onClosed');
exports.onClicked = new Event('onClicked');
exports.onButtonClicked = new Event('onButtonClicked');
function firePendingEvents() {
var msg;
while (msg = eventsToFireOnStartUp.shift()) {
processMessage(msg);
}
eventsToFireOnStartUp = null;
}
function onMessageFromNative(msg) {
if (eventsToFireOnStartUp) {
eventsToFireOnStartUp.push(msg);
} else {
processMessage(msg);
}
}
function processMessage(msg) {
var action = msg.action;
var notificationId = msg.id;
var buttonIndex = msg.buttonIndex;
if (action == 'Click') {
exports.onClicked.fire(notificationId);
} else if (action == 'Close') {
removeNotification(notificationId);
exports.onClosed.fire(notificationId, true);
} else if (action == 'ButtonClick') {
exports.onButtonClicked.fire(notificationId, buttonIndex);
} else {
throw new Error('Unknown notification action' + msg.action);
}
}
if (cordova.platformId != 'android') {
for (var k in exports) {
if (typeof exports[k] == 'function') {
exports[k] = (function(k) {
return function() {
console.warn('chrome.notifications.' + k + ' is not supported on ' + cordova.platformId);
}
})(k);
}
}
return;
}
channel.createSticky('onChromeNotificationsReady');
channel.waitForInitialization('onChromeNotificationsReady');
channel.onCordovaReady.subscribe(function() {
storage.internal.get(['notifications','notificationOptions'], function(values) {
notifications = values.notifications || {};
notifications.__proto__ = null;
notificationOptions = values.notificationOptions || {};
notificationOptions.__proto__ = null;
exec(onMessageFromNative, undefined, 'ChromeNotifications', 'messageChannel', []);
helpers.runAtStartUp(function() {
if (eventsToFireOnStartUp.length) {
helpers.queueLifeCycleEvent(firePendingEvents);
} else {
eventsToFireOnStartUp = null;
}
});
channel.initializationComplete('onChromeNotificationsReady');
});
});