Sample Application for Android

You can use this code to test the Push and Settings APIs on Android.

Example 1

Make sure you examine the code carefully and make the necessary changes as explained in the comments.

<html>
    <head>
        <script src="cordova.js"></script>
        <script>
            applicationContext = null;
            appId = "bobapp2"; //Place your application id here
           
            smpURL = null;
            
            function init() {
                // Optional initial connection context
                var context = {   
                    "serverHost": "machine_name.com", //Place your SMP 3.0 server name here  
                    "https": "false",
                    "serverPort": "80",
                    "user": "smpAdmin", //Place your user name for the OData Endpoint here
                    "password": "s3pAdmin",  //Place your password for the OData Endpoint here
                    "communicatorId": "REST",
                    "passcode": "password",
                    "unlockPasscode": "password"
                };
                sap.Logon.init(logonSuccessCallback, function() { alert("Logon Failed"); }, appId, context, sap.Logon.IabUi);
                sap.Logger.setLogLevel(sap.Logger.DEBUG);
            }
            
            function register() {
                try {
                    sap.Logon.registerOrUnlock(logonSuccessCallback, errorCallback);
                }
                catch (e) {
                    alert("Problem with register");
                }
            }
            
            function unRegister() {
                try {
                    sap.Logon.core.deleteRegistration(logonUnregisterSuccessCallback, errorCallback);
                }
                catch (e) {
                    alert("problem with unregister");
                }
            }
            
            function logonSuccessCallback(result) {
                console.log("logonSuccessCallback " + JSON.stringify(result));
                if (result) {  //calling registerOrUnlock returns null the second time it is called.  Possible bug.
                    applicationContext = result;
                    smpURL = applicationContext.applicationEndpointURL;
                    console.log(smpURL);
                    if (smpURL.charAt(smpURL.length - 1) == "/") {
                        smpURL = smpURL.substring(0, applicationContext.applicationEndpointURL.length - 1);
                    }
                    console.log(smpURL);
                    smpURL = smpURL.substring(0, smpURL.lastIndexOf("/"));
                    console.log(smpURL);
                }
            }
            
            function logonUnregisterSuccessCallback(result) {
                console.log("logonUnregisterSuccessCallback " + JSON.stringify(result));
                applicationContext = null;
            }
            
            function errorCallback(e) {
                alert("An error occurred");
                alert(JSON.stringify(e));
            }
            
            function registerForPush() {
                var nTypes = sap.Push.notificationType.SOUNDS | sap.Push.notificationType.ALERT | sap.Push.notificationType.BADGE;
                sap.Push.registerForNotificationTypes(nTypes, regSuccess, regFailure, proccessNotification, "186452565698");  //GCM Sender ID, null for APNS
                
            }
            
            function unregisterForPush() {
                var nTypes = sap.Push.notificationType.SOUNDS | sap.Push.notificationType.ALERT;
                sap.Push.unregisterForNotificationTypes(unregCallback);
            }
            
            function regSuccess(mesg) {
                alert("Successfully registered"+mesg);
            }
            
            function regFailure(errorInfo) {
                alert("Failed to register");
                alert(JSON.stringify(errorInfo));
                console.log("Error while registering.  " + JSON.stringify(errorInfo));
            }
            
            function unregCallback(msg) {
                alert("In unregCallback with params");
                console.log("Unregistered" + JSON.stringify(msg));
            }
            
            function unregCallback() {
                alert("In unregCallback with no params");
            }
            
            function proccessNotification(notification) {
                console.log("Received a notifcation: " +  JSON.stringify(notification));
            }
            
            function proccessMissedNotification(notification) {
                alert("In processMissedNotification");
                console.log("In processMissedNotification");
                console.log("Received a missed notifcation: " +  JSON.stringify(notification));
            }
            
            function checkForNotification(notification) {
                sap.Push.checkForNotification(proccessMissedNotification);
            }
            
            function showRegistrationInfo() {
                xmlhttp = new XMLHttpRequest();
                var url = smpURL + "/odata/applications/latest/" + appId + "/Connections('" + applicationContext.applicationConnectionId + "')";
                alert(url);
                xmlhttp.open("GET", url, false);
                xmlhttp.setRequestHeader("X-SMP-APPCID", applicationContext.applicationConnectionId);
                xmlhttp.send();
                var responseText = xmlhttp.responseText;
                alert(responseText);
                console.log(responseText);
            }
            
            function getBadgeCallback(data) {
                alert("The badge number is : "+ data["badgecount"]);
            }
            
            function getBadgeNum(){
                if(device.platform == "Android"){
                    alert("badge number is iOS only!");
                    return;
                }
                sap.Push.getBadgeNumber(getBadgeCallback);
                
                
            }
            
            function badgeCallBack(msg){
                alert("Set badget number : " + msg);
            }
            
            function setBadgeNum(){
                if(device.platform == "Android"){
                    alert("badge number is iOS only!");
                    return;
                }
                
                sap.Push.setBadgeNumber(10, badgeCallBack);
                
            }
            
            function resetBadgeCallback(msg){
                alert("Reset badge number : " + msg);
            }
            
            function resetBadgeNum(){
                
                if(device.platform == "Android"){
                    alert("badge number is iOS only!");
                    return;
                }
                sap.Push.resetBadge(resetBadgeCallback);
                
            }
            
            document.addEventListener("deviceready", init, false);
            </script>
    </head>
    <body>
        <h1>Push</h1>
        <button onclick="registerForPush()">Register For Push</button><br><br>
        <button onclick="unregisterForPush()">Unregister For Push</button><br><br>
        <button onclick="checkForNotification()">Check for Notification</button><br><br>
        <button onclick="showRegistrationInfo()">Registration Info</button><br><br>
        <button onclick="getBadgeNum()">Get Badge Number</button><br><br>
        <button onclick="setBadgeNum()">Set Badge Number</button><br><br>
        <button onclick="resetBadgeNum()">Reset Badge Number to 0</button><br>
    </body>
</html>