Quick Start
Introduction
Here we will walk you thru the steps to quickly set up the Haptik SDK.
Get your gear
Few details are required to ensure notifications can be sent to the user, while chatting with the bot. Please fill the required pre-requisite information here
For sample integration of Haptik SDK please visit here
Before delving into integrating the SDK, please go through the checklist and make sure you have all the items mentioned in the checklist
- Artifactory Username and Password
- Client ID
- Business viaName for your Bot
- Staging URL
- Production URL
Minimum and Target SDK versions of the Haptik SDK
minSdkVersion 21
targetSdkVersion 28
build.gradle
files
1. Changes to The first thing we are going to do here is add the following lines of code to your projects root build.gradle
allprojects {
repositories {
.
.
.
maven {
url "https://artifactory.hellohaptik.com/artifactory/libs-release-local"
credentials {
username "<ASK HAPTIK FOR USERNAME>"
password "<ASK HAPTIK FOR PASSWORD>"
}
}
google()
jcenter()
.
.
.
}
}
Haptik will provide required username and password while providing access to the SDK.
Once this is done you can move over to your app's build.gradle
file and add the following lines of code:
implementation 'ai.haptik.android.sdk:haptiklib-core:7.0.1-70284'
Add a dependency for image loading library
implementation 'ai.haptik.android.sdk:haptiklib-picasso-helper:7.0.1-70284'
For further details please refer to the "Image Loading Modules" section of the documentation
2. Manifest Entries
1. Add meta-data
We will now start adding MetaData to the apps Manifest file. This MetaData corresponds to the SDK's:
<application>
.
.
.
// Client ID (provided by Haptik)
<meta-data android:name="ai.haptik.android.sdk.ClientId"
android:resource="@string/haptik_sdk_client_id"/>
// Drawable to be used for showing the Large Notification Icon
<meta-data android:name="ai.haptik.android.sdk.NotificationIconLarge"
android:resource="@drawable/ic_notification_large"/>
// Drawable to be used for showing the Small Notification Icon
<meta-data android:name="ai.haptik.android.sdk.NotificationIconSmall"
android:resource="@drawable/ic_notification_small"/>
<!-- Geo API key needs to be put only if you require to enable maps in the SDK -->
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR GEO API KEY HERE" />
<!-- Pass the Geo API key here, if you do not have one, pass empty string -->
<meta-data android:name="ai.haptik.places.sdk.api.key"
android:value="YOUR PLACES SDK API KEY HERE" />
.
.
.
</application>
NOTE:
- Use the value of
com.google.android.geo.API_KEY
for the value ofai.haptik.places.sdk.api.key
- If you do not use
com.google.android.geo.API_KEY
pass any non empty string as the value of the key
2. Add ChatActivity
The following Activity needs to be added to your Manifest. Adding a parent will make sure that the UP button in the ChatActivity will take the user to the correct activity. If you implement an Activity which will host the InboxView then that would be the ideal candidate to be the parent of the ChatActivity.
<application>
.
.
.
<activity android:name="ai.haptik.android.sdk.messaging.ChatActivity" android:parentActivityName="YOUR_PARENT_ACTIVITY">
<meta-data android:name="ai.haptik.android.sdk.messaging.backAsUp" android:value="true"/>
</activity>
.
.
.</application>
The meta-data tag which is part of the ChatActivity is used to define the behaviour of the back button when the user is in the ChatActivity. There are two possible behaviours that can be assigned to the back button:
- The back button will behave the same way as the up button
- The back button will follow the default Android behaviour of a back button.
For the first option the value field needs to be set to true and for the second option it needs to be set to false. Incase you don’t add this meta-data tag the value is set to true.
3. Targeting Android SDK 28
If you are targeting Android Version 28, please create an xml resource file network_security_config.xml
(if not created already) and add the below lines. This is needed so that Haptik specific images which are hosted in non-secure connection can be loaded properly.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
.
.
.
<!--Haptik Specific configuration-->
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">amazonaws.com</domain>
<domain includeSubdomains="true">haptikapi.com</domain>
<domain includeSubdomains="true">hellohaptik.com</domain>
</domain-config>
.
.
.
</network-security-config>
Now add the following line in your app's AndroidManifest.xml
file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
.
.
.
<application android:networkSecurityConfig="@xml/network_security_config">
.
.
.
</manifest>
You can find more details about it here in Google's Blog.
3. Init the Haptik SDK
Haptik SDK MUST be initialized before using any of Haptik's functionality.
If you are pointing the SDK to the Staging Environment please add the following line.
HaptikLib.setRunEnvironment(HaptikLib.RUN_ENVIRONMENT_STAGING);
By default the SDK is pointing to the Production Environment.
To initialize the SDK you will need to create Haptik's InitData object. You can create that by coping the below code.
InitData initData = new InitData.Builder(application)
.baseUrl("https://staging.hellohaptik.com")
.debugEnabled(true)
.notificationSound(R.raw.notification_sound)
.imageLoadingService(`PicassoApiFactory.getPicassoApi()`)
.build();
With this initData
object you can pass it into the
HaptikLib.init(initData)
method to initialize the Haptik SDK.
Implementing InitDataCallback Interface
- Implement the
InitDataCallback
interface in theApplication
class of your app. - Override the
getClientSetupData()
method of the interface and provide the data required to initialize Haptik SDK in the method
Eg:
@Override
public InitData getClientSetupData() {
HaptikLib.setRunEnvironment(HaptikLib.RUN_ENVIRONMENT_STAGING);
InitData initData = new InitData.Builder(this)
.baseUrl(DemoPrefUtils.getHaptikBaseUrl(this))
.debugEnabled(BuildConfig.DEBUG)
.notificationSound(R.raw.notification_sound)
.build();
return initData;
}
4. Basic Sign Up on the Haptik SDK
Haptik has different types of authentication methods. To get started quickly you can try the Basic Authentication type. But for your app please check out the complete Integration guide to see what Authentication method will suit your app.
You need to pass a SignUpData object to the SDK for performing sign up. Below is the code which perform sign up and will take user to the Inbox screen.
You can also take a user directly to a channel by using following code :
if(!HaptikLib.isInitialized()){
//This mean HaptikLib is not initialised. Please initialise the HaptikLib here
HaptikLib.init(initData)
}
if (!HaptikLib.isUserLoggedIn()) {
SignUpData signUpData = new SignUpData
.Builder(SignUpData.AUTH_TYPE_BASIC)
.build();
Router.signUpAndLaunchChannel(Context context, SignUpData signUpData, String businessViaName, String source);
} else {
Router.launchChannel(Context context, String businessViaName, String source);
}
Here businessViaName
is the unique business name of the channel that
you wish to open and source
is the screen from where you're launching
this channel.
5: Logging out
If your application supports logout functionality, please call the logout method of the Haptik SDK to ensure that chats of the user logging out are cleared
HaptikLib.logout(new Callback<Boolean>() {
@Override
public void success(Boolean result) {
//Perform actions on success, or can be left blank
}
@Override
public void failure(HaptikException exception) {
//Perform actions on failure, or can be left blank
}
});
Class duplication resolution
When you'll run gradle sync it's possible that the SDK and the Host app specify common dependencies, which might result in compile failure on the grounds of class duplication. In such cases specify resolutionStrategy on your App's build.gradle as follows:
configurations . all {
resolutionStrategy {
force < path to your version of conflicting library - 1>
force < path to your version of conflicting library - 2>
}
}