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