{"__v":100,"_id":"55a9222dc8bd450d000dd1a8","category":{"__v":6,"_id":"55a589e30f354f0d00fd0312","pages":["55a590d080c8a30d00b32402","55a591c20f354f0d00fd0347","55a591c780c8a30d00b3240e","55a9172a27a17d210052524e","55a9222dc8bd450d000dd1a8","56d5d72da0b5600b000b1f7b"],"project":"559a61d2dbcfd20d00710b39","version":"559a61d2dbcfd20d00710b3c","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-07-14T22:14:59.849Z","from_sync":false,"order":3,"slug":"android-sdk","title":"Android SDK"},"parentDoc":null,"project":"559a61d2dbcfd20d00710b39","user":"559a618bdbcfd20d00710b38","version":{"__v":23,"_id":"559a61d2dbcfd20d00710b3c","project":"559a61d2dbcfd20d00710b39","createdAt":"2015-07-06T11:09:06.510Z","releaseDate":"2015-07-06T11:09:06.510Z","categories":["559a61d3dbcfd20d00710b3d","55a589ddaaf9cf1900114dd0","55a589e30f354f0d00fd0312","55a589ea80c8a30d00b323cc","55a589f6aaf9cf1900114dd1","55a58d4e80c8a30d00b323e6","55a8e7a227a17d21005251a2","55a93098cf45e1390093f351","55afb085f202b12100cd9e83","55afb22e902fd51700f5f8bf","55afb260902fd51700f5f8c2","55afb28ec8a85321007a5462","55afb294f202b12100cd9e95","55afb29b902fd51700f5f8c5","55afb2a1c8a85321007a5463","55afb2a7902fd51700f5f8c7","55afb2ad902fd51700f5f8c8","55afb2b5902fd51700f5f8ca","55b74b2131bccb190081bedc","55bb441b54f9640d006e6cf2","565711085cb2420d00d70071","5681681330018c0d006bf7ff","588f38b5923d610f00c72dad"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"Version 1.0","version_clean":"1.0.0","version":"1"},"updates":["5716177840ef9c2000cac8d2","57602f88c811102000cef2fe"],"next":{"pages":[],"description":""},"createdAt":"2015-07-17T15:41:33.472Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Gradle\"\n}\n[/block]\nConfigure the library and its dependencies through `Gradle`\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Gradle path\",\n  \"body\": \"Make sure changes are made to the build.gradle file of your `Module:app`, not of your `Project:project name`.\\n\\nDon't forget to add the `repositories` configuration in your `build.gradle`, as shown below.\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"repositories {\\n \\tflatDir {\\n        dirs 'libs'\\n    }\\n}\\n\\nandroid {\\n  ...\\n    ...\\n    ...\\n    \\n  dependencies {\\n    //AppCompat\\n    compile 'com.android.support:support-v4:23.4.0'\\n    compile 'com.android.support:appcompat-v7:23.4.0'\\n\\n    //MOCA SDK\\n    compile(name: 'moca-sdk-android-1.9.+', ext: 'aar')\\n\\n    compile 'org.msgpack:msgpack:0.6.11'\\n\\n    //Google Cloud Messaging\\n    compile 'com.google.android.gms:play-services-gcm:8.3.0'\\n\\n    //Google Location, Activity Recognition, and Places\\n    compile 'com.google.android.gms:play-services-location:8.3.0'\\n\\n    //Google Mobile Ads (Optional)\\n    compile 'com.google.android.gms:play-services-ads:8.3.0'\\t\\n  }\\n}\",\n      \"language\": \"groovy\",\n      \"name\": \"build.gradle\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Installing Google Play Services.\"\n}\n[/block]\nMOCA SDK requires Google Play Services 8.3.0+ as you've seen in the dependencies list above.\nIf your project is not using Google Play Services, please ensure the libraries are installed in your Android Studio before continuing.\nYou can see how to install Google Play Services by visiting this [Google guide](https://developer.android.com/studio/intro/update.html).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Grab your AppKey and AppSecret\"\n}\n[/block]\n1 - Go to https://console.mocaplatform.com and sign into your MOCA account.  \n2 - Select Apps item at left sidebar, and then click + New App in the content panel. Fill in the form and complete the app creation.  \n3 - Open the newly created app and navigate to Settings item. Select API keys tab.\n4 - Get `App Key` and `App Secret`. \n\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/jvi2iXdScWdM4W428eiX_consolekeys.png\",\n        \"consolekeys.png\",\n        \"704\",\n        \"458\",\n        \"#b02428\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYou can configure the MOCA SDK from the `AndroidManifest.xml` file, or alternatively from a `MOCA.properties` file\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Option A: Configuration through MOCA.properties file\"\n}\n[/block]\nStarting from MOCA Android SDK 1.9.0, you can configure the SDK using a `MOCA.properties` file. \n\n1 - Create an `assets` folder, just inside your App's main folder.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/1k7sOod6QFGfMreUtwCl_assets-folder.png\",\n        \"assets-folder.png\",\n        \"700\",\n        \"598\",\n        \"#5a8fce\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYou should be able to see a new folder in your project.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/9IgnTA5oSDqOMtqk5Xw0_assets.png\",\n        \"assets.png\",\n        \"272\",\n        \"272\",\n        \"#4d87d1\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n2 - Create a file called `MOCA.properties` by right clicking the `assets` folder and selecting New > File.\n\n3 - Configure the SDK with the necessary keys. Please bear in mind that ***the only mandatory keys are AppKey, and AppSecret***. \nThe ***gcmSender*** is mandatory only if you want to use MOCA Remote Push Notifications service.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Get your GCM Sender from Google\",\n  \"body\": \"Check [this guide](./gcm-fcm-cloud-messaging-setup) in order to get your Cloud Messaging Sender Id.\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"\",\n  \"body\": \"Don't forget to paste the AppKey and AppSecret with the ones corresponding to your App. Otherwise the SDK won't be able to connect to MOCA Cloud.\"\n}\n[/block]\nContents of the `MOCA.properties` file:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#MOCA App key and app secret. You can get this keys from the MOCA Console.\\nappKey = YOUR-MOCA-APP-KEY\\nappSecret = YOUR-MOCA-APP-SECRET\\n\\n#MOCA SDK Log level. Possible values [Off, Error,  Warning, Info, Debug, Verbose]\\nlogLevel = Debug\\n\\n# MOCA Remote Push Notifications\\n# disable it if you have your own/ other remote push implementation\\nremotePushNotificationsService = true\\n\\n# MOCA Proximity Service includes beacon and geofence detection\\nproximityService = true\\n\\n# MOCA GeoTracking Service generates geo points periodically for analytics\\ngeoTrackingService = true\\n\\n# MOCA Event Tracking Service. If enabled, activates event tracking (Sessions, custom events etc.)\\neventTrackingService = true\\n\\n# MOCA driver. This is required for Bluetooth Beacon detection. Set it to false if a custom\\n# beacon driver is being used\\nmocaDriver = true\\n\\n# Background location. Allows geoTracking in background\\nbackgroundLocation = true\\n\\n# GCM Sender ID. Only needed if you are using MOCA Remote Push Notifications.\\ngcmSender = YOUR-GCM-SENDER-ID\\n\\n# Beacon detection engine profile [LowLatency, Balanced, BatterySaver, Custom]\\n# LowLatency = Notifications will be delivered almost instantly in foreground and background.\\n# but will have an important impact on the user's device battery.\\n# Balanced = Notifications are delivered almost instantly in foreground. In background it can\\n# take up to one minute. In Android Lollipop, Marshmallow, and N there are operating system\\n# optimizations that reduce the notification delay (Default setting).\\n# BatterySaver = Detections in foreground can take up to one minute, and in background up to\\n#5 minutes. this is the recommended setting for analytics-only usages.\\n# \\nbeaconProfile = Balanced\\n\\n# If beaconProfile is \\\"Custom\\\", the following parameters are mandatory\\n# foregroundTimeBetweenScans: Time between each scan in milliseconds, close scans means faster response, but\\n#  more battery consumption.\\n# foregroundScanDuration: Time while device will be listening bluetooth signals in milliseconds.\\n#  We recommend at least 1100 ms, so if your beacon broadcasts a signal each 350 Hz,~3 packets\\n#  will be received.\\n# backgroundTimeBetweenScans: Same than foregroundTimeBetweenScans, but when device is in the background.\\n#  this is the most important setting in terms of battery consumption.\\n# backgroundScanDuration: Same than foregroundScanDuration but in background.\\n\\n# Uncomment these parameters in case you want to use a Custom profile\\n\\n# foregroundTimeBetweenScans = 3300\\n# foregroundScanDuration = 3300\\n# backgroundTimeBetweenScans = 60000\\n# backgroundScanDuration = 5001\\n\\n# MOCA Beacon detection settings (approximate distance in meters)\\n# distance detection is strongly dependent on hardware, and device calibration.\\n# Parameter indicates the threshold for the next zone, starting in 0\\n# Default config: 0-1m: immediate, 1m-5m: immediate, >5m far\\n# Uncomment the following lines if you want to override this settings.\\n\\n# proximityImmediate= 1\\n# proximityNear = 5\\n\\n#Other settings\\n\\n#Cache in MB\\ncacheSize = 100\\n\\n#Data fetch interval in milliseconds. Defaults to 1 hour\\ndataFetchInterval = 3600000\\n\\n#Event upload interval. Defaults to 15 minutes.\\neventUploadInterval = 900000\\n\\n# Custom settings\\n# Save custom settings by adding \\\"custom.\\\" prefix\\n# example custom.myCustomSetting = 1000\\ncustom.myCustomSetting = 1000\\n# Legacy notation\\nmoca.CUSTOM_PUSH_ICON = :::at:::drawable/ic_moca_logo\\n# moca.CUSTOM_NAOMICRO_APP_KEY = xxxxxx\\n# moca.CUSTOM_NAOMICRO_ENABLED = false\\n\",\n      \"language\": \"ruby\",\n      \"name\": \"MOCA.properties\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Option B: Configuration through `AndroidManifest.xml` file\"\n}\n[/block]\n- SDK Settings: you don't need to use this keys if you have used a `MOCA.properties` file.\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"Remember to paste the `moca.APP_KEY` and `moca.APP_SECRET` with the ones of your App. Otherwise the SDK won't be able to connect to MOCA Cloud.\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<application\\n             ...>\\n      <!-- MOCA SDK configuration -->\\n    <meta-data android:name=\\\"moca.APP_KEY\\\"\\n               android:value=\\\"YOUR-MOCA-APP-KEY\\\"/>\\n    <meta-data android:name=\\\"moca.APP_SECRET\\\"\\n               android:value=\\\"YOUR-MOCA-APP-SECRET\\\"/>\\n    <meta-data android:name=\\\"moca.LOG_LEVEL\\\"\\n               android:value=\\\"debug\\\"/>\\n    <meta-data android:name=\\\"moca.AUTOMATIC_PUSH_SETUP_ENABLED\\\"\\n               android:value=\\\"true\\\"/>\\n    <meta-data android:name=\\\"moca.PROXIMITY_SERVICE_ENABLED\\\"\\n               android:value=\\\"true\\\" />\\n    <meta-data android:name=\\\"moca.GEOLOCATION_SERVICE_ENABLED\\\"\\n               android:value=\\\"true\\\" />\\n    <meta-data android:name=\\\"moca.BACKGROUND_LOCATION_ENABLED\\\"\\n               android:value=\\\"true\\\" />\\n    <meta-data android:name=\\\"moca.DATA_FETCH_INTERVAL_MS\\\"\\n               android:value=\\\"auto\\\" />\\n    <meta-data android:name=\\\"moca.EVENT_UPLOAD_INTERVAL_MS\\\"\\n               android:value=\\\"auto\\\" />\\n  \\t<meta-data android:name=\\\"moca.MOCA_DRIVER_ENABLED\\\"\\n               android:value=\\\"true\\\" />\\n    <!--Optional: Include it for remote push notifications-->\\n    <!--Be careful with the space before the number-->\\n    <meta-data android:name=\\\"moca.GCM_SENDER\\\"\\n               android:value=\\\"\\\\ YOUR_GCM_SENDER_ID\\\" />\\n</application>\",\n      \"language\": \"xml\"\n    }\n  ]\n}\n[/block]\n- Be sure to replace `moca.APP_KEY` and `moca.APP_SECRET` values with the real values for your app which you found in the MOCA console. \n\nChange any setting that you need for your app:\n\n- `moca.PROXIMITY_SERVICE_ENABLED`, will enable or disable Bluetooth Beacon detection & geofence detection.\n- `moca.GEOLOCATION_SERVICE_ENABLED` – enables/disabled tracking of significant changes in user location by GPS/Cellular Trilateration/Wifi.\n - YES – user location will be tracked when the app is in foreground. Additionally, background tracking is controlled by BACKGROUND_LOCATION_ENABLED flag. This is the default.\n - NO – user location will not be tracked at all (either foreground nor background). Bluetooth beacons and geofences will still be detected if this flag is set to NO. \n- `moca.BACKGROUND_LOCATION_ENABLED` – enables/disables location tracking when the app is in background or suspended mode.\n - This flag has no effect if GEOLOCATION_SERVICE_ENABLED is set to NO.\n - By default background location tracking is enabled.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Services & Receivers\"\n}\n[/block]\n- Please declare these services and receivers no matter what option did you use in the previous steps.\n[block:callout]\n{\n  \"type\": \"danger\",\n  \"body\": \"You will need to override the **Application** class to initialize MOCA, so don't forget to add the tag **android:name=\\\"*pathToYourApplicationClass*\\\"** to **application**.\\n[Learn more about the Application class.](#initializing-moca-in-the-application-class)\",\n  \"title\": \"Please Note\"\n}\n[/block]\n\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/F3KObXES3CnfPRbMGWbE_android-name-red.gif\",\n        \"android-name-red.gif\",\n        \"630\",\n        \"243\",\n        \"#0f344c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<application \\n        ...     \\n        android:name=\\\"com.myCompany.MyApplication\\\">\\n\\n    <!-- User Google Play Services -->\\n    <meta-data android:name=\\\"com.google.android.gms.version\\\"\\n               android:value=\\\"@integer/google_play_services_version\\\" />\\n  \\n\\t\\t<!-- REQUIRED by MOCA to handle clicked notifications -->\\n\\t\\t<receiver android:name=\\\"com.innoquant.moca.push.MOCAPushReceiver\\\"\\n  \\t          android:exported=\\\"true\\\"\\n    \\t        android:permission=\\\"com.google.android.c2dm.permission.SEND\\\" >\\n\\t \\t\\t<intent-filter>\\n  \\t  \\t<action android:name=\\\"com.innoquant.moca.push.ACTION_PUSH_RECEIVE\\\" />\\n    \\t  <action android:name=\\\"com.innoquant.moca.push.ACTION_PUSH_DISMISSED\\\" />\\n\\t\\t\\t</intent-filter>\\n\\t\\t</receiver>\\n  \\n\\n    <!-- REQUIRED by MOCA to handle push notifications -->\\n    <receiver android:name=\\\"com.innoquant.moca.push.GcmPushReceiver\\\"\\n              android:permission=\\\"com.google.android.c2dm.permission.SEND\\\">\\n        <intent-filter>\\n            <action android:name=\\\"com.google.android.c2dm.intent.RECEIVE\\\" />\\n            <action android:name=\\\"com.google.android.c2dm.intent.REGISTRATION\\\" />\\n            <category android:name=\\\"YOUR-APP-PACKAGE-NAME\\\" />\\n        </intent-filter>\\n    </receiver>\\n  \\n\\t\\t<!-- MOCA GcmTokenListener listens to token registrations/updates -->\\n    <service android:name=\\\"com.innoquant.moca.push.GcmTokenListener\\\"\\n   \\t\\t\\t\\t\\tandroid:exported=\\\"false\\\">\\n\\t\\t\\t<intent-filter>\\n\\t\\t\\t\\t<action android:name=\\\"com.google.android.gms.iid.InstanceID\\\"/>\\n\\t\\t\\t</intent-filter>\\n\\t\\t</service>\\n  \\n</application>\",\n      \"language\": \"xml\",\n      \"name\": \"AndroidManifest.xml\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Setting app permissions\"\n}\n[/block]\nMOCA SDK archive (*.aar) file comes with proper AndroidManifest.xml file that gets merged with your app's Manifest. Internally, the SDK uses the following permissions:\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"There is no need to include these permissions in your AndroidManifest.xml\",\n  \"title\": \"Please note\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<!-- Required for MOCA SDK -->\\n<uses-permission android:name=\\\"android.permission.INTERNET\\\" />\\n<uses-permission android:name=\\\"android.permission.ACCESS_NETWORK_STATE\\\" />  <!– Required for BLE and Beacons -->\\n<uses-permission android:name=\\\"android.permission.BLUETOOTH\\\" /> \\n<!– Required to monitor reachability --> \\n<uses-permission android:name=\\\"android.permission.ACCESS_WIFI_STATE\\\" />     \\n<!-- Keeps the processor from sleeping when a message is received. -->     <uses-permission android:name=\\\"android.permission.WAKE_LOCK\\\" />\\n<!-- Required for Push --> \\n<uses-permission android:name=\\\"android.permission.VIBRATE\\\" /> \\n<!-- Used to collect location data -->\\n<uses-permission android:name=\\\"android.permission.ACCESS_FINE_LOCATION\\\" />  <!-- Used to collect battery stats -->\\n<uses-permission android:name=\\\"android.permission.BATTERY_STATS\\\" />     <!-- Used to collect carrier -->\\n<uses-permission android:name=\\\"android.permission.READ_PHONE_STATE\\\"/> \\n<!–- Push notifications -->\\n<uses-permission android:name=\\\"com.google.android.c2dm.permission.RECEIVE\\\"/>\\n\\n\\n\",\n      \"language\": \"xml\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Android Marshmallow (6.0) permissions\"\n}\n[/block]\nRequest the `ACCESS_FINE_LOCATION` permission at Run Time, an example of how to do it is [available here.](doc:android-60-permissions).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"MOCA SDK included Services & Receivers\"\n}\n[/block]\nMOCA SDK archive (*.aar) file comes with proper AndroidManifest.xml file that gets merged with your app manifest. Internally, the SDK uses the following services & receivers:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<!– In App AndroidManifest.xml -->\\n<meta-data android:name=\\\"moca.CUSTOM_PUSH_ICON\\\" android:resource=\\\"@drawable/YOUR_PUSH_ICON\\\" />\\n\\n<!-- REQUIRED by MOCA to handle push notifications -->\\n<receiver android:name=\\\"com.innoquant.moca.push.MOCAPushReceiver\\\">\\n\\t<intent-filter>\\n\\t       <action android:name=\\\"com.innoquant.moca.push.ACTION_PUSH_RECEIVE\\\" />\\n            <category android:name=\\\"YOUR_APP_PACKAGE\\\" />\\n     </intent-filter>\\n</receiver>       \\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n####Remote Push Notifications\n\nIf your plan includes Remote Push Notifications, please ensure the SDK is properly configured.\nCheck the [Push Notifications](/docs/push-notifications) doc.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Initializing MOCA in the Application class\"\n}\n[/block]\nFinally, to start using SDK, you’ll need to initialize MOCA library. \n\n1 - If you don't have one, create a new class that inherits from Application. You will need to declare the name of this class in the `AndroidManifest.xml` file. As you can see in the AndroidManifest.xml section above.\n2 - Import `com.innoquant.moca.*` package into your main **Application** class.\n3 - Initialize MOCA SDK in the `onCreate` method of this class.\n[block:callout]\n{\n  \"type\": \"danger\",\n  \"title\": \"Do not initialize MOCA within an Activity\",\n  \"body\": \"By doing so, MOCA won't be able to manage proximity events when the `Activity` is not running (App not in foreground, for instance).\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import com.innoquant.moca.MOCA;\\nimport com.innoquant.moca.MOCAInstance;\\n\\npublic class MocaApplication extends Application {\\n\\n    @Override\\n    public void onCreate() {\\n        super.onCreate();\\n\\n        // Init MOCA library\\n        MOCA.initializeSDK (this);\\n    }\\n}\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n4 - You must initialize the SDK before calling any other method.\n5 - On initialization, MOCA SDK will load configuration from the `AndroidManifest.xml` file or the `MOCA.properties` file and perform all necessary library setup.","excerpt":"","slug":"moca-android-sdk-configuration","type":"basic","title":"Configuration"}
[block:api-header] { "type": "basic", "title": "Gradle" } [/block] Configure the library and its dependencies through `Gradle` [block:callout] { "type": "warning", "title": "Gradle path", "body": "Make sure changes are made to the build.gradle file of your `Module:app`, not of your `Project:project name`.\n\nDon't forget to add the `repositories` configuration in your `build.gradle`, as shown below." } [/block] [block:code] { "codes": [ { "code": "repositories {\n \tflatDir {\n dirs 'libs'\n }\n}\n\nandroid {\n ...\n ...\n ...\n \n dependencies {\n //AppCompat\n compile 'com.android.support:support-v4:23.4.0'\n compile 'com.android.support:appcompat-v7:23.4.0'\n\n //MOCA SDK\n compile(name: 'moca-sdk-android-1.9.+', ext: 'aar')\n\n compile 'org.msgpack:msgpack:0.6.11'\n\n //Google Cloud Messaging\n compile 'com.google.android.gms:play-services-gcm:8.3.0'\n\n //Google Location, Activity Recognition, and Places\n compile 'com.google.android.gms:play-services-location:8.3.0'\n\n //Google Mobile Ads (Optional)\n compile 'com.google.android.gms:play-services-ads:8.3.0'\t\n }\n}", "language": "groovy", "name": "build.gradle" } ] } [/block] [block:api-header] { "type": "basic", "title": "Installing Google Play Services." } [/block] MOCA SDK requires Google Play Services 8.3.0+ as you've seen in the dependencies list above. If your project is not using Google Play Services, please ensure the libraries are installed in your Android Studio before continuing. You can see how to install Google Play Services by visiting this [Google guide](https://developer.android.com/studio/intro/update.html). [block:api-header] { "type": "basic", "title": "Grab your AppKey and AppSecret" } [/block] 1 - Go to https://console.mocaplatform.com and sign into your MOCA account. 2 - Select Apps item at left sidebar, and then click + New App in the content panel. Fill in the form and complete the app creation. 3 - Open the newly created app and navigate to Settings item. Select API keys tab. 4 - Get `App Key` and `App Secret`. [block:image] { "images": [ { "image": [ "https://files.readme.io/jvi2iXdScWdM4W428eiX_consolekeys.png", "consolekeys.png", "704", "458", "#b02428", "" ] } ] } [/block] You can configure the MOCA SDK from the `AndroidManifest.xml` file, or alternatively from a `MOCA.properties` file [block:api-header] { "type": "basic", "title": "Option A: Configuration through MOCA.properties file" } [/block] Starting from MOCA Android SDK 1.9.0, you can configure the SDK using a `MOCA.properties` file. 1 - Create an `assets` folder, just inside your App's main folder. [block:image] { "images": [ { "image": [ "https://files.readme.io/1k7sOod6QFGfMreUtwCl_assets-folder.png", "assets-folder.png", "700", "598", "#5a8fce", "" ] } ] } [/block] You should be able to see a new folder in your project. [block:image] { "images": [ { "image": [ "https://files.readme.io/9IgnTA5oSDqOMtqk5Xw0_assets.png", "assets.png", "272", "272", "#4d87d1", "" ] } ] } [/block] 2 - Create a file called `MOCA.properties` by right clicking the `assets` folder and selecting New > File. 3 - Configure the SDK with the necessary keys. Please bear in mind that ***the only mandatory keys are AppKey, and AppSecret***. The ***gcmSender*** is mandatory only if you want to use MOCA Remote Push Notifications service. [block:callout] { "type": "info", "title": "Get your GCM Sender from Google", "body": "Check [this guide](./gcm-fcm-cloud-messaging-setup) in order to get your Cloud Messaging Sender Id." } [/block] [block:callout] { "type": "warning", "title": "", "body": "Don't forget to paste the AppKey and AppSecret with the ones corresponding to your App. Otherwise the SDK won't be able to connect to MOCA Cloud." } [/block] Contents of the `MOCA.properties` file: [block:code] { "codes": [ { "code": "#MOCA App key and app secret. You can get this keys from the MOCA Console.\nappKey = YOUR-MOCA-APP-KEY\nappSecret = YOUR-MOCA-APP-SECRET\n\n#MOCA SDK Log level. Possible values [Off, Error, Warning, Info, Debug, Verbose]\nlogLevel = Debug\n\n# MOCA Remote Push Notifications\n# disable it if you have your own/ other remote push implementation\nremotePushNotificationsService = true\n\n# MOCA Proximity Service includes beacon and geofence detection\nproximityService = true\n\n# MOCA GeoTracking Service generates geo points periodically for analytics\ngeoTrackingService = true\n\n# MOCA Event Tracking Service. If enabled, activates event tracking (Sessions, custom events etc.)\neventTrackingService = true\n\n# MOCA driver. This is required for Bluetooth Beacon detection. Set it to false if a custom\n# beacon driver is being used\nmocaDriver = true\n\n# Background location. Allows geoTracking in background\nbackgroundLocation = true\n\n# GCM Sender ID. Only needed if you are using MOCA Remote Push Notifications.\ngcmSender = YOUR-GCM-SENDER-ID\n\n# Beacon detection engine profile [LowLatency, Balanced, BatterySaver, Custom]\n# LowLatency = Notifications will be delivered almost instantly in foreground and background.\n# but will have an important impact on the user's device battery.\n# Balanced = Notifications are delivered almost instantly in foreground. In background it can\n# take up to one minute. In Android Lollipop, Marshmallow, and N there are operating system\n# optimizations that reduce the notification delay (Default setting).\n# BatterySaver = Detections in foreground can take up to one minute, and in background up to\n#5 minutes. this is the recommended setting for analytics-only usages.\n# \nbeaconProfile = Balanced\n\n# If beaconProfile is \"Custom\", the following parameters are mandatory\n# foregroundTimeBetweenScans: Time between each scan in milliseconds, close scans means faster response, but\n# more battery consumption.\n# foregroundScanDuration: Time while device will be listening bluetooth signals in milliseconds.\n# We recommend at least 1100 ms, so if your beacon broadcasts a signal each 350 Hz,~3 packets\n# will be received.\n# backgroundTimeBetweenScans: Same than foregroundTimeBetweenScans, but when device is in the background.\n# this is the most important setting in terms of battery consumption.\n# backgroundScanDuration: Same than foregroundScanDuration but in background.\n\n# Uncomment these parameters in case you want to use a Custom profile\n\n# foregroundTimeBetweenScans = 3300\n# foregroundScanDuration = 3300\n# backgroundTimeBetweenScans = 60000\n# backgroundScanDuration = 5001\n\n# MOCA Beacon detection settings (approximate distance in meters)\n# distance detection is strongly dependent on hardware, and device calibration.\n# Parameter indicates the threshold for the next zone, starting in 0\n# Default config: 0-1m: immediate, 1m-5m: immediate, >5m far\n# Uncomment the following lines if you want to override this settings.\n\n# proximityImmediate= 1\n# proximityNear = 5\n\n#Other settings\n\n#Cache in MB\ncacheSize = 100\n\n#Data fetch interval in milliseconds. Defaults to 1 hour\ndataFetchInterval = 3600000\n\n#Event upload interval. Defaults to 15 minutes.\neventUploadInterval = 900000\n\n# Custom settings\n# Save custom settings by adding \"custom.\" prefix\n# example custom.myCustomSetting = 1000\ncustom.myCustomSetting = 1000\n# Legacy notation\nmoca.CUSTOM_PUSH_ICON = @drawable/ic_moca_logo\n# moca.CUSTOM_NAOMICRO_APP_KEY = xxxxxx\n# moca.CUSTOM_NAOMICRO_ENABLED = false\n", "language": "ruby", "name": "MOCA.properties" } ] } [/block] [block:api-header] { "type": "basic", "title": "Option B: Configuration through `AndroidManifest.xml` file" } [/block] - SDK Settings: you don't need to use this keys if you have used a `MOCA.properties` file. [block:callout] { "type": "warning", "body": "Remember to paste the `moca.APP_KEY` and `moca.APP_SECRET` with the ones of your App. Otherwise the SDK won't be able to connect to MOCA Cloud." } [/block] [block:code] { "codes": [ { "code": "<application\n ...>\n <!-- MOCA SDK configuration -->\n <meta-data android:name=\"moca.APP_KEY\"\n android:value=\"YOUR-MOCA-APP-KEY\"/>\n <meta-data android:name=\"moca.APP_SECRET\"\n android:value=\"YOUR-MOCA-APP-SECRET\"/>\n <meta-data android:name=\"moca.LOG_LEVEL\"\n android:value=\"debug\"/>\n <meta-data android:name=\"moca.AUTOMATIC_PUSH_SETUP_ENABLED\"\n android:value=\"true\"/>\n <meta-data android:name=\"moca.PROXIMITY_SERVICE_ENABLED\"\n android:value=\"true\" />\n <meta-data android:name=\"moca.GEOLOCATION_SERVICE_ENABLED\"\n android:value=\"true\" />\n <meta-data android:name=\"moca.BACKGROUND_LOCATION_ENABLED\"\n android:value=\"true\" />\n <meta-data android:name=\"moca.DATA_FETCH_INTERVAL_MS\"\n android:value=\"auto\" />\n <meta-data android:name=\"moca.EVENT_UPLOAD_INTERVAL_MS\"\n android:value=\"auto\" />\n \t<meta-data android:name=\"moca.MOCA_DRIVER_ENABLED\"\n android:value=\"true\" />\n <!--Optional: Include it for remote push notifications-->\n <!--Be careful with the space before the number-->\n <meta-data android:name=\"moca.GCM_SENDER\"\n android:value=\"\\ YOUR_GCM_SENDER_ID\" />\n</application>", "language": "xml" } ] } [/block] - Be sure to replace `moca.APP_KEY` and `moca.APP_SECRET` values with the real values for your app which you found in the MOCA console. Change any setting that you need for your app: - `moca.PROXIMITY_SERVICE_ENABLED`, will enable or disable Bluetooth Beacon detection & geofence detection. - `moca.GEOLOCATION_SERVICE_ENABLED` – enables/disabled tracking of significant changes in user location by GPS/Cellular Trilateration/Wifi. - YES – user location will be tracked when the app is in foreground. Additionally, background tracking is controlled by BACKGROUND_LOCATION_ENABLED flag. This is the default. - NO – user location will not be tracked at all (either foreground nor background). Bluetooth beacons and geofences will still be detected if this flag is set to NO. - `moca.BACKGROUND_LOCATION_ENABLED` – enables/disables location tracking when the app is in background or suspended mode. - This flag has no effect if GEOLOCATION_SERVICE_ENABLED is set to NO. - By default background location tracking is enabled. [block:api-header] { "type": "basic", "title": "Services & Receivers" } [/block] - Please declare these services and receivers no matter what option did you use in the previous steps. [block:callout] { "type": "danger", "body": "You will need to override the **Application** class to initialize MOCA, so don't forget to add the tag **android:name=\"*pathToYourApplicationClass*\"** to **application**.\n[Learn more about the Application class.](#initializing-moca-in-the-application-class)", "title": "Please Note" } [/block] [block:image] { "images": [ { "image": [ "https://files.readme.io/F3KObXES3CnfPRbMGWbE_android-name-red.gif", "android-name-red.gif", "630", "243", "#0f344c", "" ] } ] } [/block] [block:code] { "codes": [ { "code": "<application \n ... \n android:name=\"com.myCompany.MyApplication\">\n\n <!-- User Google Play Services -->\n <meta-data android:name=\"com.google.android.gms.version\"\n android:value=\"@integer/google_play_services_version\" />\n \n\t\t<!-- REQUIRED by MOCA to handle clicked notifications -->\n\t\t<receiver android:name=\"com.innoquant.moca.push.MOCAPushReceiver\"\n \t android:exported=\"true\"\n \t android:permission=\"com.google.android.c2dm.permission.SEND\" >\n\t \t\t<intent-filter>\n \t \t<action android:name=\"com.innoquant.moca.push.ACTION_PUSH_RECEIVE\" />\n \t <action android:name=\"com.innoquant.moca.push.ACTION_PUSH_DISMISSED\" />\n\t\t\t</intent-filter>\n\t\t</receiver>\n \n\n <!-- REQUIRED by MOCA to handle push notifications -->\n <receiver android:name=\"com.innoquant.moca.push.GcmPushReceiver\"\n android:permission=\"com.google.android.c2dm.permission.SEND\">\n <intent-filter>\n <action android:name=\"com.google.android.c2dm.intent.RECEIVE\" />\n <action android:name=\"com.google.android.c2dm.intent.REGISTRATION\" />\n <category android:name=\"YOUR-APP-PACKAGE-NAME\" />\n </intent-filter>\n </receiver>\n \n\t\t<!-- MOCA GcmTokenListener listens to token registrations/updates -->\n <service android:name=\"com.innoquant.moca.push.GcmTokenListener\"\n \t\t\t\t\tandroid:exported=\"false\">\n\t\t\t<intent-filter>\n\t\t\t\t<action android:name=\"com.google.android.gms.iid.InstanceID\"/>\n\t\t\t</intent-filter>\n\t\t</service>\n \n</application>", "language": "xml", "name": "AndroidManifest.xml" } ] } [/block] [block:api-header] { "type": "basic", "title": "Setting app permissions" } [/block] MOCA SDK archive (*.aar) file comes with proper AndroidManifest.xml file that gets merged with your app's Manifest. Internally, the SDK uses the following permissions: [block:callout] { "type": "info", "body": "There is no need to include these permissions in your AndroidManifest.xml", "title": "Please note" } [/block] [block:code] { "codes": [ { "code": "<!-- Required for MOCA SDK -->\n<uses-permission android:name=\"android.permission.INTERNET\" />\n<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" /> <!– Required for BLE and Beacons -->\n<uses-permission android:name=\"android.permission.BLUETOOTH\" /> \n<!– Required to monitor reachability --> \n<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\" /> \n<!-- Keeps the processor from sleeping when a message is received. --> <uses-permission android:name=\"android.permission.WAKE_LOCK\" />\n<!-- Required for Push --> \n<uses-permission android:name=\"android.permission.VIBRATE\" /> \n<!-- Used to collect location data -->\n<uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\" /> <!-- Used to collect battery stats -->\n<uses-permission android:name=\"android.permission.BATTERY_STATS\" /> <!-- Used to collect carrier -->\n<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/> \n<!–- Push notifications -->\n<uses-permission android:name=\"com.google.android.c2dm.permission.RECEIVE\"/>\n\n\n", "language": "xml" } ] } [/block] [block:api-header] { "type": "basic", "title": "Android Marshmallow (6.0) permissions" } [/block] Request the `ACCESS_FINE_LOCATION` permission at Run Time, an example of how to do it is [available here.](doc:android-60-permissions). [block:api-header] { "type": "basic", "title": "MOCA SDK included Services & Receivers" } [/block] MOCA SDK archive (*.aar) file comes with proper AndroidManifest.xml file that gets merged with your app manifest. Internally, the SDK uses the following services & receivers: [block:code] { "codes": [ { "code": "<!– In App AndroidManifest.xml -->\n<meta-data android:name=\"moca.CUSTOM_PUSH_ICON\" android:resource=\"@drawable/YOUR_PUSH_ICON\" />\n\n<!-- REQUIRED by MOCA to handle push notifications -->\n<receiver android:name=\"com.innoquant.moca.push.MOCAPushReceiver\">\n\t<intent-filter>\n\t <action android:name=\"com.innoquant.moca.push.ACTION_PUSH_RECEIVE\" />\n <category android:name=\"YOUR_APP_PACKAGE\" />\n </intent-filter>\n</receiver> \n", "language": "java" } ] } [/block] ####Remote Push Notifications If your plan includes Remote Push Notifications, please ensure the SDK is properly configured. Check the [Push Notifications](/docs/push-notifications) doc. [block:api-header] { "type": "basic", "title": "Initializing MOCA in the Application class" } [/block] Finally, to start using SDK, you’ll need to initialize MOCA library. 1 - If you don't have one, create a new class that inherits from Application. You will need to declare the name of this class in the `AndroidManifest.xml` file. As you can see in the AndroidManifest.xml section above. 2 - Import `com.innoquant.moca.*` package into your main **Application** class. 3 - Initialize MOCA SDK in the `onCreate` method of this class. [block:callout] { "type": "danger", "title": "Do not initialize MOCA within an Activity", "body": "By doing so, MOCA won't be able to manage proximity events when the `Activity` is not running (App not in foreground, for instance)." } [/block] [block:code] { "codes": [ { "code": "import com.innoquant.moca.MOCA;\nimport com.innoquant.moca.MOCAInstance;\n\npublic class MocaApplication extends Application {\n\n @Override\n public void onCreate() {\n super.onCreate();\n\n // Init MOCA library\n MOCA.initializeSDK (this);\n }\n}\n", "language": "java" } ] } [/block] 4 - You must initialize the SDK before calling any other method. 5 - On initialization, MOCA SDK will load configuration from the `AndroidManifest.xml` file or the `MOCA.properties` file and perform all necessary library setup.