1 // ${project.version}
2 var exec = require("cordova/exec");
3
4 /**
5 * The Kapsel Push plugin provides an abstraction layer over the
6 * <a href="http://developer.android.com/google/gcm/index.html">Google Cloud Messaging for Android (GCM)</a>
7 * and
8 * <a href="http://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW9">Apple Push Notification Service (APNS)</a>.
9 * <br/><br/>
10 * A notification can be sent to a device registered with an application through a
11 * rest call at <pre>http://SMP_3.0_SERVER:8080/Notifications/application_registration_id</pre>
12 * <br/><br/>
13 * <b>Adding and Removing the Push Plugin</b><br/>
14 * Add or remove the Push plugin using the
15 * <a href="http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface">Cordova CLI</a>.<br/>
16 * <br/>
17 * To add the Push plugin to your project, use the following command:<br/>
18 * cordova plugin add <path to directory containing Kapsel plugins>\push<br/>
19 * <br/>
20 * To remove the Push plugin from your project, use the following command:<br/>
21 * cordova plugin rm com.sap.mp.cordova.plugins.push
22 * <br/>
23 *
24 * @namespace
25 * @alias Push
26 * @memberof sap
27 */
28
29 module.exports = {
30
31
32
33 /**
34 * Helper method for handling failure callbacks. It is configured as a failure callback in <code> call_native() </code>
35 *
36 *
37 * @param {msg} Error message with the cause of failure
38 *
39 * @private
40 * @name failure
41 * @function
42 */
43
44 failure: function (msg) {
45 sap.Logger.debug("Javascript Callback Error: " + msg,"PUSHJS",function(m){},function(m){});
46
47 },
48
49 /**
50 * Helper method for calling native methods.
51 *
52 * @param {function} callback
53 * @param {string} Name of the action to invoke on the plugin
54 * @param {array} List of arguments
55 * @private
56 * @name call_native
57 * @function
58 */
59 call_native: function (callback, name, args) {
60
61 if(arguments.length == 2) {
62 args = []
63 }
64 ret = exec(
65 callback, /** Called when signature capture is successful */
66 sap.Push.failure, /** Called when signature capture encounters an error */
67 'SMPPushPlugin', /** Tell Cordova that to run "PushNotificationPlugin" */
68 name, /** Tell the plugin the action to perform */
69 args); /** List of arguments to the plugin */
70 return ret;
71 },
72
73 /**
74 * Helper method to check if platform is iOS.
75 *
76 * @return {bool} Whether the current platform is iOS or not.
77 * @private
78 * @name isPlatformIOS
79 * @function
80 */
81 isPlatformIOS: function () {
82 return device.platform == "iPhone" || device.platform == "iPad" || device.platform == "iPod touch" || device.platform == "iOS"
83 },
84 /**
85 * Function called by the application to get connection information.
86 *
87 * @param {string} [types] Types of notifications the application wants to receive. The different types of notifications are expressed in <code>notificationType</code>
88 * @param {string} [successCB] Success callback to call when registration is successful.
89 * @param {string} [errorCB] Error callback to call when registration attempt fails.
90 * @private
91 * @memberof sap.Push
92 * @function getConnectionSettings
93 * @example
94 * sap.Push.getConnectionSettings(function(){
95 * sap.Logger.debug("getting Connection Settings","PUSHJS",function(m){},function(m){});
96 * console.log("getting Connection Settings");
97 * sap.Push.registerForNotification(types, successCallback, errorCallback, notificationListenerFunc, senderId );
98 **/
99 getConnectionSettings : function (successCB, errorCB) {
100
101
102 if (sap.Settings.isInitialized == true)
103 {
104 /*It is already initialized */
105 successCB();
106
107 } else {
108 sap.Settings.isInitialized = true;
109 var pd ="";
110 sap.Logon.unlock(function (connectionInfo) {
111 var userName = connectionInfo["registrationContext"]["user"];
112 var password = connectionInfo["registrationContext"]["password"];
113 var applicationConnectionId = connectionInfo["applicationConnectionId"];
114 var securityConfig = connectionInfo["registrationContext"]["securityConfig"];
115 var endpoint = connectionInfo["applicationEndpointURL"];
116 var keySSLEnabled = "false";
117 var splitendpoint = endpoint.split("/");
118 if (splitendpoint[0] == "https:")
119 {
120 keySSLEnabled="true";
121 }
122 if (securityConfig == null) {
123 securityConfig = "";
124 }
125 var burl = splitendpoint[2];
126 var appId = splitendpoint[3];
127 pd = appId+userName+password;
128 sap.Settings.store = new sap.EncryptedStorage("SettingsStore", pd);
129 connectionData = {
130 "keyMAFLogonOperationContextConnectionData": {
131 "keyMAFLogonConnectionDataApplicationSettings":
132 {
133 "DeviceType":device.platform,
134 "DeviceModel":device.model,
135 "ApplicationConnectionId":applicationConnectionId
136 },
137 "keyMAFLogonConnectionDataBaseURL":burl
138 },
139 "keyMAFLogonOperationContextApplicationId":appId,
140 "keyMAFLogonOperationContextBackendUserName":userName,
141 "keyMAFLogonOperationContextBackendPassword":password,
142 "keyMAFLogonOperationContextSecurityConfig":securityConfig,
143 "keySSLEnabled":keySSLEnabled
144 };
145 sap.Settings.start(connectionData,
146 function(mesg) {
147 sap.Settings.isInitialized = true;
148 sap.Logger.debug("Setting Exchange is succesful ","SETTINGSJS",function(m){},function(m){});
149 successCB();
150 },
151 function(mesg){
152 sap.Logger.debug("Setting Exchange failed" + mesg,"SETTINGSJS",function(m){},function(m){});
153 sap.Settings.isInitialized = false;
154 errorCB();
155 });
156 }
157 , function () {
158 console.log("unlock failed");
159 sap.Logger.debug("unlock failed ","SETTINGSJS",function(m){},function(m){});
160 }
161 );
162
163 }
164
165
166 },
167
168 /**
169 * Function called by the application to register the notification types to receive.
170 *
171 * @param {string} [types] Types of notifications the application wants to receive. The different types of notifications are expressed in <code>notificationType</code>
172 * @param {string} [successCallback] Success callback to call when registration is successful.
173 * @param {string} [errorCallback] Error callback to call when registration attempt fails.
174 * @param {string} [notificationlistenerfunc] The function that receives the notification for processing by the application.
175 * @param {string} [senderId] The sender ID that is used for GCM registration. For other platforms it is null.
176 * @private
177 * @memberof sap.Push
178 * @function registerForNotificationTypes
179 * @example
180 * regid = "211112269206";
181 * function registerSuccess(mesg){}
182 * function registerFailure(mesg) {}
183 * function ProcessNotification(mesg){}
184 * sap.Push.registerForNotificationTypes(sap.Push.notificationType.badge | sap.Push.notificationType.sound | sap.Push.notificationType.alert, registerSuccess, registerFailure, ProcessNotification, regid);
185 */
186
187 registerForNotification: function (types, successCallback, errorCallback, notificationListenerFunc, senderId ) {
188 if(device.platform == "iPhone" || device.platform == "iPad" || device.platform == "iPod touch" || device.platform == "iOS" || device.platform == "Android") {
189 sap.Push.RegisterSuccess = successCallback;
190 sap.Push.RegisterFailed = errorCallback;
191 sap.Push.ProcessNotificationForUser = notificationListenerFunc;
192 sap.Push.call_native(successCallback, "registerForNotificationTypes", [types, senderId]);
193
194 }
195
196 },
197
198 /* Core APIS */
199
200 /**
201 * Function called by the application to register the notification types to receive.
202 *
203 * @param {string} types Types of notifications the application wants to receive. The different types of notifications are expressed in <code>notificationType</code>
204 * Notificaion types allowed are Disable all notifications (NONE: 0), Set badge count on app icon (BADGE: 1), Play sounds on receiving notification (SOUNDS: 2) and Show alert on receiving notification (ALERT: 4).
205 * @param {string} successCallback Success callback to call when registration is successful.
206 * @param {string} errorCallback Error callback to call when registration attempt fails.
207 * @param {string} notificationlistenerfunc The function that receives the notification for processing by the application.
208 * @param {string} [senderId] The sender ID that is used for GCM registration. For other platforms it is null.
209 * @public
210 * @memberof sap.Push
211 * @function registerForNotificationTypes
212 * @example
213 * regid = "211112269206";
214 * function registerSuccess(mesg){}
215 * function registerFailure(mesg) {}
216 * function ProcessNotification(mesg){}
217 * sap.Push.registerForNotificationTypes(sap.Push.notificationType.badge | sap.Push.notificationType.sound | sap.Push.notificationType.alert, registerSuccess, registerFailure, ProcessNotification, regid);
218 */
219 registerForNotificationTypes: function (types, successCallback, errorCallback, notificationListenerFunc, senderId ) {
220 sap.Push.getConnectionSettings(function(){
221 sap.Logger.debug("getting Connection Settings","PUSHJS",function(m){},function(m){});
222 console.log("getting Connection Settings");
223 sap.Push.registerForNotification(types, successCallback, errorCallback, notificationListenerFunc, senderId );
224 },
225 function(){});
226 },
227
228
229 /**
230 * Function called by the application to unregister from future notifications.
231 *
232 * @param {function} callback Success callback to call when deregistration is successful. This callback function will contain a string with a message. This message is for informative purposes only.
233 * @public
234 * @memberof sap.Push
235 * @function unregisterForNotificationTypes
236 * @example
237 * function unregCallback(mesg){}
238 * sap.Push.unregisterForNotificationTypes(unregCallback);
239 */
240
241 unregisterForNotificationTypes: function (callbak) {
242 if(device.platform == "iPhone" || device.platform == "iPad" || device.platform == "iPod touch" || device.platform == "iOS" || device.platform == "Android") {
243 sap.Push.call_native(callbak,"unregisterForNotification");
244 }
245 },
246
247 /**
248 * Used to fetch the badge count for the application. This function is used by iOS only. Other platforms do not have the badge count concept.
249 *
250 * @param {function} callback Success callback to call when to send the badge count. The callback function will contain an argument in JSON format with the current badge count. Look at the example for the details on how to use them.
251 * @public
252 * @memberof sap.Push
253 * @function getBadgeNumber
254 * @example
255 * function getBadgeNumCallback(data) { badgecount = data["badgecount"];}
256 * sap.Push.getBadgeNumber(getBadgeNumCallback);
257 */
258 getBadgeNumber: function(callback)
259 {
260 if (sap.Push.isPlatformIOS()) {
261 sap.Push.call_native(callback, "getBadgeNumber");
262 }
263 },
264
265 /**
266 * Used to set the badge count for the application. This function is used by iOS only. Other platforms do not have the badge count concept.
267 *
268 * @param {number} number The badge count to set for the application.
269 * @param {function} callback Success callback to call when to send the badge count. The callback function will contain an argument in string format. This argument can be used for informative purposes.
270 * @public
271 * @memberof sap.Push
272 * @function setBadgeNumber
273 * @example
274 * function badgeCallback(mesg){}
275 * badgenum = 10;
276 * sap.Push.setBadgeNumber(badgenum, badgeCallback);
277 */
278 setBadgeNumber: function (number, callback) {
279 if (sap.Push.isPlatformIOS()) {
280 sap.Push.call_native(callback, "setBadgeNumber", [number]);
281 }
282 },
283
284 /**
285 * Used to reset the badge count for the application. This function is used by iOS only. Other platforms do not have the badge count concept.
286 *
287 * @param {function} callback Success callback to call when the badge count is reset. The callback function will contain an argument in string format. This argument can be used for informative purpose.
288 * @public
289 * @memberof sap.Push
290 * @function resetBadge
291 * @example
292 * function badgeCallback(mesg){}
293 * sap.Push.resetBadge(badgeCallback);
294 */
295 resetBadge: function (callback) {
296 if (sap.Push.isPlatformIOS()) {
297 sap.Push.call_native(callback, "resetBadge");
298 }
299 },
300
301
302
303
304
305 /**
306 * This method updates the application with the new device token in the SAP Mobile Platform server.
307 *
308 * @param {string} [devtok] The device token received from the APNS/GCM device registration.
309 * @public
310 * @callback {function} [callback] The callback function that is called with the registration result.
311 * @memberof sap.Push
312 * @example
313 * function callback(mesg) {}
314 * devToken ="123123213213";//sample device token
315 * sap.Push.updateWithDeviceToken(devToken, callback);
316 */
317
318 updateWithDeviceToken: function (devtok, callback) {
319 if (sap.Push.isPlatformIOS() || device.platform == "Android" ) {
320 sap.Push.call_native(callback, "updateWithDeviceToken", [devtok]);
321 }
322 },
323
324 /**
325 * This method checks for any notifications received while the application was not running in the foreground. You can call this
326 * function directly or register with an event handler to be called automatically. It is okay to call this function even if the device is not yet registered for push notifications.
327 * @param {function} callback The callback function that receives the notification. The callback function will receive a string as its argument. This string will contain the notification message sent from the server, intact.
328 * @memberof sap.Push
329 * @example
330 * function processBackgroudMessage(mesg){
331 *
332 * }
333 * function checkBackgroundNotification() {
334 * sap.Push.checkForNotification(processBackgroudMessage);
335 * }
336 * document.addEventListener("onSapLogonSuccess", checkBackgroundNotification, false);
337 * document.addEventListener("onSapResumeSuccess", checkBackgroundNotification, false);
338 **/
339
340 checkForNotification: function(callback) {
341 if (sap.Push.isPlatformIOS() || device.platform == "Android" ) {
342 sap.Push.call_native(callback, "checkForNotification");
343 }
344 },
345
346 /**
347 * This is an internal function, which is called when there is a push notification.
348 * @private
349 **/
350 ProcessNotification: function(message) {
351 if (sap.Push.ProcessNotificationForUser == null )
352 {
353 console.log("No Processing function provided");
354 sap.Logger.debug("Notification listener function is not registered. Register it by calling registerForNotificationTypes","PUSHJS",function(m){},function(m){});
355 } else {
356 sap.Push.ProcessNotificationForUser(message);
357 }
358 },
359 /**
360 * This is an internal function, which is automatically called when the plugin is initialized. Used for Android only.
361 * @private
362 **/
363 initPlugin: function(callback) {
364 if ( device.platform == "Android")
365 {
366 args = [];
367 exec(
368 callback,
369 function(){ sap.Logger.debug("Plugin Initialization","PUSHJS",function(m){},function(m){}); } ,
370 'SMPPushPlugin',
371 "initPlugin",
372 args);
373 }
374 }
375
376 };
377
378
379 /**
380 * Local private variables
381 */
382 module.exports.RegisterSuccess = null;
383 module.exports.RegisterFailed = null;
384 module.exports.ProcessNotificationForUser = null;
385 /**
386 * Enum for types of push notification.
387 * @enum {number}
388 * @private
389 */
390 module.exports.notificationType = {
391 /** Disable all notifications */
392 NONE: 0,
393 /** Set badge count on app icon */
394 BADGE: 1,
395 /** Play sounds upon receiving notification */
396 SOUNDS: 2,
397 /** Show alert upon receiving notification */
398 ALERT: 4
399 };
400
401
402
403
404 document.addEventListener('deviceready', module.exports.initPlugin, false);
405
406