-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnotification.js
168 lines (143 loc) · 4.27 KB
/
notification.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
// Copyright 2014 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.
//
// Notification javascript code.
/** @const */
var notifications = chrome.notifications;
/**
* @constructor
*
* Sets up a component to handle notification messages from the runtime,
* translating them to Chrome notifications.
*
* @param plugin Plugin instance.
*/
function NotificationManager(plugin) {
if (!notifications)
return;
/** @private */
this.activeIds_ = {};
/** @private */
this.plugin_ = plugin;
notifications.onClosed.addListener(this.postMessage_.bind(this, 'onClosed'));
notifications.onClicked.addListener(this.onClicked_.bind(this));
notifications.onButtonClicked.addListener(
this.postMessage_.bind(this, 'onButtonClicked'));
plugin.addMessageListener('jsNotification', this.handleMessage_.bind(this));
}
/**
* @private
*
* Handles notification being clicked.
*
* @param id A string to identify this notification.
*/
NotificationManager.prototype.onClicked_ = function(id) {
// Restore and raise the app window when a notification is clicked.
this.plugin_.showWindow();
this.postMessage_('onClicked', id, null);
};
/**
* @private
*
* Posts a notification.
*
* @param command Notification command to post.
* @param id A string to identify this notification.
* @param extra Extra data for this notification.
*/
NotificationManager.prototype.postMessage_ = function(command, id, extra) {
var message = {
namespace: 'androidNotification',
command: command,
data: {
id: id,
extra: extra
}
};
this.plugin_.postMessage(message);
};
/**
* @private
*
* Determines the configuration of the Chrome notification from the request.
*
* @param message Source notification request message to use.
* @return The equivalent Chrome notification options structure.
*/
NotificationManager.prototype.getOptionsFromMessage_ = function(message) {
// TODO(crbug.com/350031): Just return the notification object once we
// either are not getting String objects in it, or Chrome apps functions
// take String objects.
return JSON.parse(JSON.stringify(message.data.notification));
};
/**
* @private
*
* Adds a Chrome notification based on a request message.
*
* @param message Source notification request message to use.
*/
NotificationManager.prototype.addNotification_ = function(message) {
var id = message.data.id.valueOf();
this.activeIds_[id] = true;
var options = this.getOptionsFromMessage_(message);
notifications.create(id, options, function() {});
};
/**
* @private
*
* Updates a Chrome notification based on a request message.
*
* @param message Source notification request message to use.
*/
NotificationManager.prototype.updateNotification_ = function(message) {
var id = message.data.id.valueOf();
var options = this.getOptionsFromMessage_(message);
notifications.update(id, options, function() {});
};
/**
* @private
*
* Removes a Chrome notification based on a request message.
*
* @param id Notification id to remove.
*/
NotificationManager.prototype.removeNotification_ = function(id) {
delete this.activeIds_[id];
notifications.clear(id, function() {});
};
/**
* @public
*
* Cleans up all active notifications upon destroying the notification manager.
*
*/
NotificationManager.prototype.destroy = function() {
var activeIds = [];
// Make a copy of this.activeIds_ before iterating over it and calling
// removeNotification which will remove items from this.activeIds_.
for (var id in this.activeIds_) activeIds.push(id);
for (var i = 0; i < activeIds.length; ++i) {
this.removeNotification_(activeIds[i]);
}
};
/**
* @private
*
* Dispatcher for all requests to the notification component.
*
* @param {Message} message Notification message to handle.
*/
NotificationManager.prototype.handleMessage_ = function(message) {
if (message.command == 'addNotification') {
this.addNotification_(message);
} else if (message.command == 'updateNotification') {
this.updateNotification_(message);
} else if (message.command == 'removeNotification') {
this.removeNotification_(message.data.id.valueOf());
} else {
console.log('Unknown notification command[' + message.command + ']');
}
};