Be aware that the Changer Android SDK requires both Background access to device Location and Physical Activity Recognition permissions to run.
To get started, you need to register your application by contacting Changers. This process is done manually after a request. You're going to receive from us:
- An application identifier;
- A client id;
- A client secret.
This guide includes Kotlin code examples only. You can see the complete Kotlin code in our GitHub repo.
No change at root build.gradle is required. Everything listed in this session can be done at module level.
- Java 8 support is required, so you must have set:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
//...
}- Setup your app name and credentials as environment variables, and then add them as build config fields:
android {
//...
defaultConfig {
buildConfigField "String", "APP_NAME", "\"$SDK_APP_NAME\""
buildConfigField "String", "CLIENT_ID", "\"$SDK_CLIENT_ID\""
buildConfigField "String", "CLIENT_SECRET", "\"$SDK_CLIENT_SECRET\""
}
}- Add the following maven repository at module level:
repositories {
//this should come before jitpack else MotionTag library will require authentication before download
maven { url "https://pkgs.dev.azure.com/motiontag/releases/_packaging/releases/maven/v1" }
maven { url 'https://jitpack.io' }
}- And finally add the SDK as an implementation dependency:
dependencies {
implementation 'com.github.Changers.Android-SDK:sdk:1.20.7'
// for testing on stage use this
//implementation "com.github.Changers.Android-SDK:sdkstaging:1.20.7"
}You need to initialize the SDK before launching the Web App. We recommend doing this in your Application class.
class App : android.app.Application() {
override fun onCreate() {
super.onCreate()
initializeSdk()
}
private fun initializeSdk() {
Changers.with(this,
Changers.Settings(
notification = createNotification(),
appName = BuildConfig.APP_NAME,
displayName = getString(R.string.app_name),
clientId = BuildConfig.CLIENT_ID,
clientSecret = BuildConfig.CLIENT_SECRET,
version = BuildConfig.VERSION_NAME
)
)
}
}You must create a notification so our foreground service can keep listening for location updates and tracking works properly.
- Make sure you've setup your App class
<application android:name=".App"
android:fullBackupContent="@xml/backup_rules"
android:dataExtractionRules="@xml/data_extraction_rules"
android:allowBackup="false"
Where the file contents are as follows:
backup_rules.xml
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<exclude domain="sharedpref" path="motiontag_tracker.xml"/>
</full-backup-content>
data_extraction_rules.xml
<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
<cloud-backup>
<exclude domain="sharedpref" path="motiontag_tracker.xml"/>
</cloud-backup>
<device-transfer>
<exclude domain="sharedpref" path="motiontag_tracker.xml"/>
</device-transfer>
</data-extraction-rules>
- Register WebActivity so it can be launched:
<activity
android:name="com.blacksquared.sdk.activity.WebActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
///Add this part to handle deep link
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="api.klima-taler.com"
android:scheme="https" />
<data
android:host="api.coin-stage.de"
android:scheme="https" />
</intent-filter>
</activity>
- Create a file provider so the Web Application can persist data in your internal memory. Under
<application>, add:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.blacksquared.sampleandroidsdk.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/changers_file_paths" />
</provider>
Under the desired interaction (in our example, it's the click of a button), launch the Web Application:
startActivity(WebActivity.newIntent(this))Changers.openLink hands a link to the SDK and opens the Changers web application on it. Use it
when your own app catches the link — on your own domain, or any link you want to route
yourself:
Changers.openLink(this, uri)From Java:
Changers.openLink(this, uri);Put the intent filter on your own activity in that case, rather than on WebActivity as shown
in section 3.2, and call openLink from it:
<activity android:name=".DeepLinkActivity" android:exported="true">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="your.domain.example" android:scheme="https" />
</intent-filter>
</activity> class DeepLinkActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
intent?.data?.let { Changers.openLink(this, it) }
finish()
}
}The intent filter on WebActivity in section 3.2 still works for links on the Changers hosts —
those open the web application directly and need no code from you. Use openLink for links that
reach your own activity first.
Changers.handleDeepLink takes the link without opening anything. Use it when your app has its
own screen to show first — the link is picked up the next time the web application opens:
Changers.handleDeepLink(uri)Changers.with(...)must already have run (section 2.1). A deep link can arrive at app startup, before any of your own screens exist, so initialize fromApplication.onCreate()and not from the activity that receives the link.WebActivitymust be registered (section 3.2).openLinkthrowsActivityNotFoundExceptionwithout it.
The SDK opens whatever link you give it. Make sure it is one of yours before calling — particularly on a custom URL scheme, since other SDKs in your app may use that scheme for their own callbacks.
openLink and handleDeepLink need 1.20.7 or later. They do not exist in earlier versions.
To fetch user uuid, use this function call
Changers.uuid
It will return the logged in user uuid or empty string if the uuid have not been created. Uuid will be created once the WebActivity have been launched at least once.
To log in returning user, use this function call
Changers.logUserIn("*****")
Passing in your uuid, The function can throw an exception if there is an error. It should be called in a background thread since it makes a network call. It should be called before launching the WebActivity.
You can use our helpdesk. We're friendly. ;)