Week 5 Downloads

download Week 5 Downloads

of 14

Transcript of Week 5 Downloads

  • 8/12/2019 Week 5 Downloads

    1/14

    openSAP 1 Copyright SAP

    Week 5 Download Overview

    INTRODUCTION

    The downloads for this week aim to illustrate the following: How to register against SMP and persist the registration details across application restarts

    The use of resource bundles for enhancing an application

    The use of Google Cloud Messaging to illustrate how to use native Push mechanisms

    The first download will show the steps involved in registering against SMP. Since registration is onlyrequired once, the application will persist (or save) the registration details so that on subsequent applicationrestarts the same registration id can be used again, avoiding the need to register again.

    The second download builds on this by doing two things:1. Using resource bundles uploaded to SAP Mobile Platform server in the cloud2. Adding a registration against Googles Cloud Messaging (GCM) system.

    This registration against GCM is a one-time event, and so the application will again save the registrationdetails for subsequent use. GCM will be used to notify the application of changes to the resource bundlesuploaded to SMP. This notification will be handled by the SMP admin initiating a push via GCM.

    PREREQUISITES

    These applications require the installation of Android APIs level 18 via the Android SDK manager:

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    2/14

    openSAP 2 Copyright SAP

    You will also need to create an AVD that uses Google APIs level 18 (notjust Android):

    The two applications need to be installed into Eclipse. Use the Import feature of Eclipse and selectGeneralArchive file. Browse to the downloaded project files and import them one at a time.

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    3/14

    openSAP 3 Copyright SAP

    NATIVE APPLICATION THAT PERSISTS APPCID

    The first download (OPENSAP_ANDROID_UIMOD) contains some additional code that illustrates how topersist (save) the APPCID value used by the application.

    Remember that this value is returned when the application registers with SMP. It is included on all Odatarequests (as a custom header, X-SUP-APPCID), or alternatively as a cookie. If this value is not sent with theOdata requests, SMP will reject the requests.

    In this example, we should look in the following files:

    Page1Activity.java

    In this file, we can see that when the Activity is launched (after the user has provided his or her user ID andpassword), the application checks to see if the APPCID is available (that is, it has been saved from aprevious execution). If not, the application assumes it needs to register with SMP; else it re-uses the value.

    // In order to work with SMP cloud correctly, we need to register first.// This will generate something called an APPCID which we need to provide on all subsequent// OData requests. This APPCID should be generated just once and persisted on the device so// that on subsequent executions we can reuse it and avoid the registration step.//// So we first check to see if we have we already got an APPCID value.

    // We we use a small Utility method to see if we can find a persisted copy of the APPCID value

    String appCid = Util.getProperty(application, "APPCID");

    if(appCid == null) {// APPCID is not stored, so we must register ...// The registration URL is extracted from the properties file, and accesses through the

    service instance// The service instance is populated with the properties at app startupString regUrl = application.getService().getSmpOdataUrl() + "Connections";try{

    // We now create a new query with the url data and execute the request// This is going to be a POST to the SMP Odata Endpoint// It requires an XML body template// We also set the X-SUP-SC header to define the security context. The

    SecurityConfig is defined in the// property file which is read at app startup.ODataQuery query = newODataQuery(regUrl);String body = "\n\n\n\n\n\n";

    Map headers = newHashMap();headers.put("X-SUP-SC", application.getService().getSecurityConfig());

    // execute the Create (POST) requestapplication.getConnectivityHelper().executeAsyncCreateRequestWithHeaders(query,

    body, headers, this);}catch(MalformedURLException e){

    ZGWSAMPLE_SRVApplication.LOGGER.e(ZGWSAMPLE_SRVApplication.TAG, e.getMessage());}

    // When this registration is successful, we will do the actual Collection query

    return;}

    // If we get here we have a persisted APPCID// Let's save it for access elsewhereapplication.getService().setAppCid(appCid);

    Registration involves POSTing to a URL in SMP; and receiving the response, which will include the X-SUP-APPCID value.

    Note the asynchronous handling of HTTP calls: when a HTTP call is made, either onSuccess or onError iscalled. If onSuccess is called, the code decides if it needs to start a follow on request (that is, afterregistering successfully, it will start the actual product query request).

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    4/14

    openSAP 4 Copyright SAP

    NB: this code is really for illustrative purposes only. Also note that with the latest SMP SDK release, theactual registration is done with different APIs which hide some of this complexity. This code illustrates whatis actually happening under the hood and helps the student understand what steps are being executed.

    Util.java

    This just contains a couple of useful methods to save and retrieve values so that on application restart, thevalues can be restored.

    zgwsample_srvservice.properties

    This property file (under the res->raw folder) contains the URLs to be used to connect to the SMP server. Italso defines the security config you used when creating the application in SMP. Please change theseproperty values as appropriate.

    smp_odata_url=https\://smp-P123456trial.hanatrial.ondemand.com\:443/odata/applications/latest/com.opensap.myfirst/base_url=https\://smp-P123456trial.hanatrial.ondemand.com\:443/com.opensap.myfirst/security_config=Sapes1SC

    You will need to use the URLs for your instance of SMP Cloud Platform. Please ensure that you also usethe correct application ID and security config.

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    5/14

    openSAP 5 Copyright SAP

    NATIVE APPLICATION THAT USES GOOGLE CLOUD MESSAGING

    The second download (OPENSAP_ANDROID_UIMOD_GCM) builds on the previous one, and addsadditional features.

    In addition to registering with SMP, it also downloads a resource bundle. In a real application, the resourcebundle could hold useful information (such as images or customization data). In this example, the bundle isnot actually used for anything.

    The app also registers with the Google Cloud Messaging service, and using the SMP Admin console,Admins can push notifications to the app that indicate that a new resource bundle is ready for download.The app will recieve these notifications and re-load the resource bundle.

    To obtain a resource bundle, it is necessary that the student uploads a resource bundle into SMP. For thisexercise, the student should ensure that at least 2 bundles are uploaded. Because the app does not actuallydo ANYTHING with the bundle, the two versions can be the same. You should ensure that the bundle isnamed in a way that is consistent with that expected by the application in the example property file, it issimply called MyApp.

    To upload resource bundles, use the SMP Admin UI and go to Client Resources. There enter the bundlename (MyApp) and the version id. Ensure that the first bundle has a version 1.0; subsequent bundle

    uploads can use different versions.

    Also ensure that Enable push notifications for resource bundle is set.

    You will also then need to enable GCM push in the UI. This is done from the Push tab. To do this, you willneed to register with Google Cloud Messaging. See this URL for more details:

    http://developer.android.com/google/gcm/gs.html

    Broadly, you need to

    Enable Google Cloud Messaging for Android

    Create a new GCM project

    Create a Server API key

    Enter the API key and project ID in SMP Admin

    These are some screen shots for this process.

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://developer.android.com/google/gcm/gs.htmlhttp://developer.android.com/google/gcm/gs.htmlhttp://developer.android.com/google/gcm/gs.htmlhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    6/14

    openSAP 6 Copyright SAP

    First, the developer page (mentioned above):

    From the main Google APIs console page create a new project. You will end up with a screen like this:

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    7/14

    openSAP 7 Copyright SAP

    After creating the project, the URL shown in the browser will change to show the project number you areassigned this becomes the SENDER_ID (see below). Make a note of this number.

    Now you need to enable Google Messaging for Android. Click on Services on the left

    Then look for Google Messaging for Android and ensure it is On:

    Now we can get the API access keys. You need to click on API access

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    8/14

    openSAP 8 Copyright SAP

    Google console will automatically create a server key for you:

    Under the Simple API Access section you will see the API key take a note of this, as you will need it soon.

    The GCM project ID MUST now be set in the application project code. In the example download, this isdone in LoginActivity.java by replacing the SENDER_ID value found in the source code with the project ID

    created in Google.

    String SENDER_ID= "542388214422"; // change this value

    The project ID value and API key then need to be entered into the SMP Admin UI.

    Its very important these values get entered correctly into SMP, and the correct SENDER_ID value is set inthe project source code.

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    9/14

    openSAP 9 Copyright SAP

    To demonstrate the push, you should go to the Admin UI, then select the relevant bundle, then click SendNotification.

    All being well, the app should receive the notification, and then make a request to SMP to upload anewbundle, which it saves on disk.

    To verify that the push message was sent, you can check the SMP server logs (under Logs and then clickthe PUSH tab). You should be able to see if the push was successfully sent to the GCM service.

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    10/14

    openSAP 10 Copyright SAP

    For this app, the bundle doesnt contain anything. In reality you would use the bundle for somethinginteresting (e.g. it might contain images or configuration details).

    The following files contain the necessary code changes required to handle this process flow:

    LoginActivity.java

    This contains the code to initialize the GCM service on the client. It will save the registration id for future use the application should only register once.

    This code also contains the SENDER_ID setting that needs to be modified.

    GCM Registration will take place in the onCreate delegate method. First, we check to see if Google PlayServices are enabled:

    // Check device for Play Services APK. If check succeeds, proceed with GCM registration.if(checkPlayServices()) {

    gcm= GoogleCloudMessaging.getInstance(this);regid= Util.getRegistrationId(context);

    if(regid.isEmpty()) {registerInBackground();

    }} else{

    ZGWSAMPLE_SRVApplication.LOGGER.i(ZGWSAMPLE_SRVApplication.TAG, "No valid Google PlayServices APK found.");

    }

    Then registration takes place (in the registerInBackground method):

    protectedString doInBackground(Void... params) {String msg = "";try{

    if(gcm== null) {gcm= GoogleCloudMessaging.getInstance(context);

    }regid= gcm.register(SENDER_ID);msg = "Device registered, registration ID="+ regid;

    ZGWSAMPLE_SRVApplication.LOGGER.i(ZGWSAMPLE_SRVApplication.TAG, msg);

    // Persist the regID - no need to register again.Util.storeRegistrationId(context, regid);

    } catch(IOException ex) {msg = "Error :"+ ex.getMessage();// If there is an error, don't just keep trying to register.// Require the user to click a button again, or perform// exponential back-off.

    }returnmsg;

    }

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    11/14

    openSAP 11 Copyright SAP

    We store the returned registration id through the storeRegistrationId method call. This will be used later.

    Page1Activity.java

    As with the previous application download, this class will register the app against SMP.

    In this version, it also handles the logic necessary to tell SMP that the application has registered with GCM,and also that the device is an Android device. This is done by updating certain connection settings in theXML document that is pushed up to SMP.

    This class also downloads the resource bundle from SMP, which it will save locally.

    // Extract the resource bundle and save it locally// then do the actual product querytry{

    InputStream is = response.getEntity().getContent();Util.writeResourceFile(application, is);is.close();

    }catch(IOException e1){

    // TODOAuto-generated catch blocke1.printStackTrace();

    }

    Util.setProperty(application, "RESOURCE_BUNDLE", bundleId + ":1.0");

    Note that it also saves the resource bundle identifier so that when a notification is received about a new one,the app can check to see if the new version is changed, and hence whether to upload a new one or not.

    GcmIntentService.java

    This handles the push from GCM. It checks the message and uploads the relevant resource bundle. Themessage sent from SMP via GM will contain the name of the new resource bundle to be uploaded.

    When the request is received, the application will upload the new bundle by making the appropriate HTTPcall to SMP. When the bundle is received, it will save the bundle, using logic similar to that in

    Page1Activity.java.

    For more details on GCM handling, see the Google documentation.

    Util.java

    As with the previous app, this contains useful support routines for persisting values so that they can be re-read on application re-start.

    zgwsample_srvservice.properties

    As with the first app, this file defines the URLs used to connect to SMP. It also now contains the resourcebundle name that should be used and defines the URL used to query for the bundle.

    You must change these values to suit your project needs.

    Resourcebundle.zip

    This is the dummy bundle that needs to be uploaded to SMP. Its contents are ignored.

    Google-play-services_lib

    This is a project that contains the necessary library code to enable GCM in the application. Import it intoEclipse.

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    12/14

    openSAP 12 Copyright SAP

    NB: This project should be marked as a library. To check that this is the case, after import, right-click on theproject properties, select Android from the list, and check that isLibrary is checked:

    This library should now be referenced from your main App project. This is done by right-clicking the mainapplication project, choosing properties, then selecting Android. In the references section at the bottom, adda reference to this library.

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    13/14

    openSAP 13 Copyright SAP

    You should be able to build your main project successfully after having done this.

    When deploying, the target AVD musthave Google Play APIs level 18 installed (see the prerequisites at thestart of the document).

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epx
  • 8/12/2019 Week 5 Downloads

    14/14

    openSAP 14 Copyright SAP

    APPENDIX

    With SMP 2.3 SP2 and also SUP 2.2 SP4 revised APIs are available that provide enhanced support forapplication registration and resource bundle access. For more details, see the SMP documentation at:

    http://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc01708.0223/doc/html/title.html

    These APIs will perform the same HTTP requests as the examples downloaded during this weeks course,

    but will use a simpler API from the developers perspective.

    http://www.sap.com/corporate-en/legal/copyright/index.epxhttp://www.sap.com/corporate-en/legal/copyright/index.epxhttp://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc01708.0223/doc/html/title.htmlhttp://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc01708.0223/doc/html/title.htmlhttp://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc01708.0223/doc/html/title.htmlhttp://www.sap.com/corporate-en/legal/copyright/index.epx