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
|
/* global cordova:false */
/*!
* Module dependencies.
*/
var exec = cordova.require('cordova/exec');
/**
* PushNotification constructor.
*
* @param {Object} options to initiate Push Notifications.
* @return {PushNotification} instance that can be monitored and cancelled.
*/
var PushNotification = function(options) {
this._handlers = {
'registration': [],
'notification': [],
'error': []
};
// require options parameter
if (typeof options === 'undefined') {
throw new Error('The options argument is required.');
}
// store the options to this object instance
this.options = options;
// triggered on registration and notification
var that = this;
var success = function(result) {
if (result && typeof result.registrationId !== 'undefined') {
that.emit('registration', result);
} else if (result && typeof result.callback !== 'undefined') {
var executeFunctionByName = function(functionName, context /*, args */) {
var args = Array.prototype.slice.call(arguments, 2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for (var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
}
executeFunctionByName(result.callback, window, result);
} else if (result) {
that.emit('notification', result);
}
};
// triggered on error
var fail = function(msg) {
var e = (typeof msg === 'string') ? new Error(msg) : msg;
that.emit('error', e);
};
// wait at least one process tick to allow event subscriptions
setTimeout(function() {
exec(success, fail, 'PushNotification', 'init', [options]);
}, 10);
};
/**
* Unregister from push notifications
*/
PushNotification.prototype.unregister = function(successCallback, errorCallback, options) {
if (errorCallback == null) { errorCallback = function() {}}
if (typeof errorCallback != "function") {
console.log("PushNotification.unregister failure: failure parameter not a function");
return
}
if (typeof successCallback != "function") {
console.log("PushNotification.unregister failure: success callback parameter must be a function");
return
}
exec(successCallback, errorCallback, "PushNotification", "unregister", [options]);
};
/**
* Call this to set the application icon badge
*/
PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallback, errorCallback, badge) {
if (errorCallback == null) { errorCallback = function() {}}
if (typeof errorCallback != "function") {
console.log("PushNotification.setApplicationIconBadgeNumber failure: failure parameter not a function");
return
}
if (typeof successCallback != "function") {
console.log("PushNotification.setApplicationIconBadgeNumber failure: success callback parameter must be a function");
return
}
exec(successCallback, errorCallback, "PushNotification", "setApplicationIconBadgeNumber", [{badge: badge}]);
};
/**
* Listen for an event.
*
* The following events are supported:
*
* - registration
* - notification
* - error
*
* @param {String} eventName to subscribe to.
* @param {Function} callback triggered on the event.
*/
PushNotification.prototype.on = function(eventName, callback) {
if (this._handlers.hasOwnProperty(eventName)) {
this._handlers[eventName].push(callback);
}
};
/**
* Emit an event.
*
* This is intended for internal use only.
*
* @param {String} eventName is the event to trigger.
* @param {*} all arguments are passed to the event listeners.
*
* @return {Boolean} is true when the event is triggered otherwise false.
*/
PushNotification.prototype.emit = function() {
var args = Array.prototype.slice.call(arguments);
var eventName = args.shift();
if (!this._handlers.hasOwnProperty(eventName)) {
return false;
}
for (var i = 0, length = this._handlers[eventName].length; i < length; i++) {
this._handlers[eventName][i].apply(undefined,args);
}
return true;
};
/*!
* Push Notification Plugin.
*/
module.exports = {
/**
* Register for Push Notifications.
*
* This method will instantiate a new copy of the PushNotification object
* and start the registration process.
*
* @param {Object} options
* @return {PushNotification} instance
*/
init: function(options) {
return new PushNotification(options);
},
/**
* PushNotification Object.
*
* Expose the PushNotification object for direct use
* and testing. Typically, you should use the
* .init helper method.
*/
PushNotification: PushNotification
};
|