使用行動應用程式設定Oracle Mobile Hub SDK

您必須先設定Oracle Mobile Hub SDK 與行動應用程式,才能撰寫任何程式碼。

新增 SDK

在基本應用程式設定中 (若未介入中間的架構),您會將 Android 從屬端 SDK 新增至應用程式。

  1. 解壓縮 Android 從屬端 SDK 壓縮檔案。

  2. 將 SDK jar 複製到應用程式專案中的 libs 資料夾內。如果此資料夾不存在,請在階層中將它建立為與 srcbuild 資料夾相同的層次。

  3. 在 Android Studio 中,依序選取檔案新建,然後選取新建模組以啟動精靈,將 IDMMobileSDK.jar 匯入專案中。

  4. 按一下匯入。JAR/。AAR 套件,然後依照精靈將 IDMMobileSDK.jar 匯入專案。
  5. 在應用程式的來源樹狀結構中,建立名為 assets 的資料夾 (與 javares 資料夾位於相同層次)。

  6. 在 SDK 組合中,尋找 oracle_mobile_cloud_config.xml 檔案並將它複製到 assets 資料夾。

  7. 在您應用程式的 build.gradle 檔案中,請確定下列是註冊的相依性,以便讓應用程式可以使用 SDK 程式庫。

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.squareup.okhttp3:okhttp:3.9.0'
        compile 'org.slf4j:slf4j-jdk14:1.7.13'
        //to enable the app to receive notifications, include the following:
        compile 'com.google.firebase:firebase-messaging:11.0.2'
    		compile 'com.facebook.android:facebook-android-sdk:4.25.+'
    }
  8. 如果應用程式的設計目的是要在分析行銷活動中接收通知,請在 build.gradle 的最下方新增下列行。

    apply plugin: 'com.google.gms.google-services'
  9. 開啟 assets/oracle_mobile_cloud_config.xml,並填寫應用程式將使用之行動後端的環境詳細資訊。

設定 SDK 特性

若要在 Android 應用程式中使用從屬端 SDK,您必須新增 oracle_mobile_cloud_config.xml 組態檔至應用程式,然後在Oracle Mobile Hub中填入後端的環境詳細資訊。接著,SDK 類別會使用這項資訊建構對Oracle Mobile Hub進行之 REST 呼叫的 HTTP 標頭。

您可以在 assets 資料夾中,將應用程式主要組合的組態檔封裝在與 javares 資料夾相同的層次。例如,在範例 GettingStarted 應用程式中,它位於/GettingStarted/src/main/assets

檔案基本上分為下列部分:

  • mobileBackend 元素及其內容。

    如果您使用含有應用程式的後端,則會包括此部分。SDK 類別會使用您指定的環境和認證詳細資訊來存取後端,並建構對 api 進行 REST 呼叫的 HTTP 標頭。

  • 適用於整體組態的元素,例如 logLeveloAuthTokenEndpoint。這些元素通常會出現在檔案的頂端,但不一定要出現。

下列程式碼範例顯示 oracle_mobile_cloud_config.xml 檔案的結構。

<config>

  <!--This value is required if you are using OAuth to authenticate against the mobile backend-->
  <oAuthTokenEndPoint>YOUR_OAUTH_TOKEN_END_POINT<oAuthTokenEndPoint>
  <!--Set to true if you want to get logging information-->
  <enableLogger>true</enableLogger>
  <logLevel>DEBUG</logLevel>
  <!--Whether to log HTTP call request and response bodies and headers-->
  <logHTTP>true</logHTTP>

  <!-- Include the mobileBackend element and its sub-elements if you are going 
       to be using a backend to access custom and platform APIs.-->
  <mobileBackend>
    <name>MBE_NAME</name>
    <baseUrl>BASE_URL</baseUrl>
    <enableAnalytics>true</enableAnalytics>
    <authentication>
      <!--possible values for type are [oauth, basic, facebook, tokenExchange ]-->
      <type>AUTH_TYPE</type>
      <oauth>
        <clientId>CLIENT_ID</clientId>
        <clientSecret>CLIENT_SECRET</clientSecret>
        <enableOffline>true</enableOffline>
      </oauth>
      <basic>
        <mobileBackendId>MOBILE_BACKEND_ID</mobileBackendID>
        <anonymousKey>ANONYMOUS_KEY</anonymousKey>
        <enableOffline></enableOffline>
      </basic>
      <facebook>
        <appId>FACEBOOK_APP_ID</appId>
        <scopes>public_profile,user_friends,email,user_location,user_birthday</scopes>
        <basic>
          <mobileBackendId>MOBILE_BACKEND_ID</mobileBackendID>
          <anonymousKey>ANONYMOUS_KEY</anonymousKey>
        </basic>
      </facebook>
      <tokenExchange>
        <! tokenExchange can contain an 'oauth' sub-element or a 'basic' sub-element.
        <oauth>
          <clientId>CLIENT_ID</clientId>
          <clientSecret>CLIENT_SECRET</clientSecret>
        </oauth>
        <basic>
          <mobileBackendId>MOBILE_BACKEND_ID</mobileBackendID>
          <anonymousKey>ANONYMOUS_KEY</anonymousKey>
        </basic>
      <tokenExchange>
    </authentication>
    <!-- additional properties go here -->
  </mobileBackend>

</config>

您需要填入指定後端的值可以在該行動後端的「定值」和「應用程式設定檔」頁面找到。

以下是檔案元素的一些更多附註。

  • oAuthTokenEndPoint—您應用程式從中取得其認證記號的 OAuth 伺服器 URL。必須為依賴 OAuth 認證的所有應用程式提供此金鑰。您可以從後端的「設定值」頁面取得此頁面。

  • logLevel—決定應用程式的主控台中顯示的 SDK 記錄日誌數量。預設值為 ERROR。其他可能值 (在詳細資訊的增加層次中) 為 WARNINGINFO 以及 DEBUG。您也可以指定 NONE

  • enableLogger—設為 true 時,應用程式會包含記錄日誌。

  • logHTTP—設為 true 時,SDK 會在要求和回應中記錄 HTTP 和 HTTPS 標頭。

  • mobileBackend—包含您後端之認證詳細資訊以及其他選擇性詳細資訊 (例如同步化特性) 的元素。

    您可以從後端的「定值」頁面取得認證詳細資訊 (例如 OAuth 和 HTTP 證明資料)。

  • mobileBackend/baseUrl—您透過後端呼叫之所有 api 的基礎 URL。您可以從後端的「設定值」頁面取得此頁面。

  • mobileBackend/authentication—包含下列子元素:

    • type 子元素,可能的值為 oauthbasicfacebook 以及 tokenExchange

    • 認證類型的一或多個子元素,每個子元素都包含認證證明資料。

      您也可以新增 offlineEnabled 索引鍵,並將其值設為 true

  • enableOffline—如果設為 true,則允許離線登入。這只適用於基本和 OAuth 登入類型。若要執行此作業,您還需要將下列項目新增至應用程式的 AndroidManifest.xml 檔案:
    <receiver android:name="oracle.cloud.mobile.network.NetworkHelper"
      <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
      </intent-filter>
    </receiver>

設定您的 Android 資訊清單檔案

存取網路以及尋找網路狀態等作業的權限,是透過您應用程式資訊清單檔案 AndroidManifest.xml 中的權限設定值來控制。這些權限為必要:

  • permission。INTERNET—允許您的應用程式存取開啟的網路通訊埠。

  • permission。ACCESS_NETWORK_STATE—允許您的應用程式存取網路的相關資訊。

其他權限為選擇性。例如,應用程式能夠接收通知所需的許可權。如需可用權限的執行者,請參閱 Google 文件中的 Android 資訊清單權限。

AndroidManifest.xml 檔案的頂端新增權限,如下列範例所示:

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="oracle.cloud.mobile.sample" >
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

  <application>
<provider
	android:name="com.facebook.FacebookContentProvider"
	android:authorities="com.facebook.app.FacebookContentProvider430660953811847"
	android:exported="true" />    
<receiver 
	android:name="oracle.cloud.mobile.network.NetworkHelper"
      <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
      </intent-filter>
    </receiver>

    (.....)
  </application>
</manifest>

新增從屬端 SDK 至您的應用程式可能需要您設定 AndroidManifest.xml 檔案,以新增權限或活動。例如,如果您新增「通知」個別 SDK 程式庫,您可能也需要新增廣播接收者。