Implementing Server Notification for Hybrid App Clients

Configure matching rules for MBO-related data on SAP Mobile Server.

Prerequisites
Complete the procedure in Defining the Mobile Business Object so that you have an MBO with the required attributes.
Task
Any data changes matching these rules trigger a notification from SAP Mobile Server to the Hybrid App client. This section describes how to write HTML, JavaScript, and modify the WorkflowClient.xml to display the results of a server notification.
  1. Right-click on the mobile application project and choose Generate Hybrid App API.
  2. Select the Sales MBO, choose Generate to an external folder, and add \html to end of the folder name.
  3. Right-click on the generated html folder and select New > Other > General > File.
  4. Enter notification.html for the file name.
  5. Open the notification.html file and add this code:
     <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
            <meta name="HandheldFriendly" content="True" />
            <meta http-equiv="PRAGMA" content="NO-CACHE" />
            <meta name="viewport" content="initial-scale = 1.0 ,maximum-scale = 1.0" />
            <script src="js/PlatformIdentification.js"></script>
            <script src="js/hwc-api.js"></script>
            <script src="js/hwc-comms.js"></script>
            <script src="js/hwc-utils.js"></script>
            <script src="js/WorkflowMessage.js"></script>
            <script src="js/HybridApp.js"></script>
            <script>
            hwc.processDataMessage = function (incomingDataMessageValue) {
                if (incomingDataMessageValue.indexOf("<M>") != 0) {
                    alert("An error occurred!  " + incomingDataMessageValue);
                }
                var workflowMessage = new WorkflowMessage(incomingDataMessageValue);
                var values = workflowMessage.getValues();
                var salesOrderList = values.getData("Sales_order");
                var salesOrder = salesOrderList.value[0];
                var salesOrderId = salesOrder.getData("Sales_order_id_attribKey").value;
                var custId = salesOrder.getData("Sales_order_cust_id_attribKey").value;
                alert("The customer id for sales order  " + salesOrderId + " is " + custId);
            }
            </script>
        </head>
        <body onload="hwc.onHybridAppLoad_CONT()">
            <h3>Server Notification Sample</h3>
            <button id="closeHybridApp" onclick="hwc.close()">Close</button>
        </body>
    </html> 
    Five of the included files are from the <SMP_HOME>\MobileSDK23\HybridAp\API folder. The file named HybridApp.js is generated based on the operations and object queries of the MBOs selected in the Generate Hybrid App API wizard. In the onload event, the method hwc.onHybridAppLoad_CONT() is called. For server-initiated applications this returns the data message associated with this instance of the server-initiated application as a parameter to hwc.processDataMessage(). In processDataMessage, some of the data is extracted from the application message and displayed.
  6. Navigate to SMP_HOME\MobileSDK23\HybridApp\PackagingTool and double-click the packagingtool.bat file.
  7. Click Browse to enter the file path for your project and click OK.
  8. Select Project > New.
  9. Fill in Patterns_Notification and the location of where the generated files currently exist (the same location specified as the Generation folder above) for the Project name and Web root directory.
  10. Fill in the MBO package name and version to match the deployed package.
  11. Specify a matching rule for the subject:
    <![CDATA[find sales order \(.*]]>

    HWC2.2_notification_subject_rule
  12. Specify the files to include in the Hybrid App for each supported platform.
    Only the selected files will appear in the manifest.xml file.

    HWC2.2_notification_files
  13. Open the generated WorkflowClient.xml file and update the Notifications section: 
    <Notifications>
      <Notification type="onEmailTriggered" targetscreen="Salesorder" asyncRequestErrorScreen="" errorNotificationSubjectLine="" errorNotificationFromLine="" asyncRequestErrorlogs="" asyncRequestErrorLogMessage="" asyncRequestErrorLogMessageAsList="">
        <Transformation>
          <Rule type="regex-extract" source="subject" workflowKey="order_id" workflowType="number" beforeMatch="find sales order \(" afterMatch="\)" format=""/> 
        </Transformation>
        <Methods>
          <Method name="findByParameter" type="search" mbo="Sales_order" package="patterns_notification:1.0">
            <InputBinding opname="findByParameter" optype="none">
              <Value sourceType="Key" workflowKey="order_id" contextVariable="" paramName="order_id" attribName="id" mboType="int" convertToLocalTime="false"/>
            </InputBinding>
            <OutputBinding generateOld="true">			    
              <Mapping workflowKey="Sales_order" workflowType="list" mboType="list">
                <Mapping workflowKey="Sales_order_id_attribKey" workflowType="number" attribName="id" mboType="int"/>
                <Mapping workflowKey="Sales_order_cust_id_attribKey" workflowType="number" attribName="cust_id" mboType="int"/>
                <Mapping workflowKey="Sales_order_order_date_attribKey" workflowType="date" attribName="order_date" mboType="date"/>
                <Mapping workflowKey="Sales_order_fin_code_id_attribKey" workflowType="text" attribName="fin_code_id" mboType="string"/>
                <Mapping workflowKey="Sales_order_region_attribKey" workflowType="text" attribName="region" mboType="string"/>
              </Mapping>
            </OutputBinding>
          </Method>
        </Methods>
      </Notification>
    </Notifications>
  14. Save and close the file.
  15. In the Hybrid App Packaging Tool, click Generate to create a deployable package.
  16. Login in to SAP Control Center to deploy and assign the Hybrid App package.
  17. Send a notification to the device.
    Typically this is triggered by a database trigger or by the EIS sending a DCN. You can also use the Send a Notification wizard in the Hybrid App designer.
    1. In the Hybrid App designer, click Flow Design.
    2. Right-click in the Flow Design page and select Send a notification.

      send_notification_server_notification