Getting Started
Integrate the Smartech SDK
a. Adding Dependencies
To install the latest Smartech SDK in your project, add the following line to the dependencies section in the app-level build.gradle
implementation 'com.netcore.android:smartech-sdk:3.1.18'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61'
implementation 'androidx.work:work-runtime-ktx:2.3.3'
b. Perform gradle sync
Be sure to perform a Gradle Sync to build your project and incorporate the dependency additions noted above.
Initialize Smartech SDK
First, you need to put your Smartech panel app Id in the meta-data tag of your application's manifest file.
<meta-data
android:name="SMT_APP_ID"
android:value="YOUR_SMARTECH_APP_ID_HERE" />
In the onCreate()
method of your Application class includes the code below. This code will initialize the Smartech SDK.
@Override
public void onCreate() {
super.onCreate();
Smartech.getInstance(new WeakReference<>(context)).initializeSdk(this);
}
override fun onCreate() {
super.onCreate()
Smartech.getInstance(WeakReference(applicationContext)).initializeSdk(this)
}
Make sure you have registered your Application class in the Application tag inside the manifest.xml
file.
Setting existing FCM token to Smartech SDK
Note
You can skip this step if you don't want to send push notifications via Smartech
If your app already has FCM integrated into it or you are migrating from SDK v2.x to 3.x it is recommended to set existing FCM to Smartech SDK. You can pass FCM token to Smartech SDK using the setDevicePushToken() method and please make sure that you are calling this method only once, i.e. on the first app launch. You can also use Smartech's fetchAlreadyGeneratedTokenFromFCM() to fetch existing token from Firebase.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Smartech smartech = Smartech.getInstance(new WeakReference<Context>(this));
smartech.initializeSdk(this);
if(firstrun) {
smartech.setDevicePushToken(token);
}
// Or you can use below method of Smartech SDK to fetch existing FCM token.
try {
smartech.fetchAlreadyGeneratedTokenFromFCM();
} catch (Exception e) {
Log.e(TAG, "Fetching FCM token failed.");
}
}
}
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val smartech = Smartech.getInstance(WeakReference(this))
smartech.initializeSdk(this)
if(firstrun) {
smartech.setDevicePushToken(token)
}
// Or you can use below method of Smartech SDK to fetch existing FCM token.
try {
smartech.fetchAlreadyGeneratedTokenFromFCM()
} catch (e: Exception) {
Log.e(TAG, "Fetching FCM token failed.")
}
}
}
Check SDK logs
To check the logs of Smartech SDK, you need to implement a method named setDebugLevel(). By default, debug level will be None.
Below is the Log levels.
LOG_LEVEL_VERBOSE
LOG_LEVEL_DEBUG
LOG_LEVEL_INFO
LOG_LEVEL_WARN
LOG_LEVEL_ERROR
LOG_LEVEL_FATAL
LOG_LEVEL_NONE
Smartech smartech = Smartech.getInstance(new WeakReference<>(this.getApplicationContext()));
smartech.setDebugLevel(SMTDebugLevel.Level.VERBOSE);
val smartech = Smartech.getInstance(WeakReference(this.getApplicationContext()))
smartech.setDebugLevel(SMTDebugLevel.Level.LOG_LEVEL_DEBUG)
Track App Install
Smartech needs your help to track app installs correctly. You need to write the logic to identify if its first app launch to trigger the app INSTALL event.
Smartech.getInstance(new WeakReference<>(context)).trackAppInstall();
//Sample code
boolean isFirstLaunch=sharedPreferences.getBoolean("is_first_launch",true);
if(isFirstLaunch){
Smartech.getInstance(new WeakReference<>(context)).trackAppInstall();
sharedPreferences.putBoolean("is_first_launch",false);
}
Smartech.getInstance(WeakReference(context)).trackAppInstall()
//Sample code
val isFirstLaunch = sharedPreferences.getBoolean("is_first_launch", true)
if (isFirstLaunch) {
Smartech.getInstance(WeakReference(context)).trackAppInstall()
sharedPreferences.putBoolean("is_first_launch", false)
}
Track App Update
Whenever you update the app you need to inform Smartech, You need to write logic at the app side to trigger the app UPDATE event.
Smartech.getInstance(new WeakReference<>(context)).trackAppUpdate();
//Sample code
String currentVersion = BuildConfig.VERSION_NAME;
String preferenceVersion = sharedPreferences.getString("app_version", "");
if (!preferenceVersion.isEmpty()) {
if (!currentVersion.equals(preferenceVersion)) {
Smartech.getInstance(new WeakReference<>(context)).trackAppUpdate();
sharedPreferences.putString("app_version", currentVersion);
}
} else {
sharedPreferences.putString("app_version", currentVersion);
}
Smartech.getInstance(WeakReference(context)).trackAppUpdate()
//Sample code
val currentVersion = BuildConfig.VERSION_NAME
val preferenceVersion = sharedPreferences.getString("app_version", "")
if (!preferenceVersion.isEmpty()) {
if (currentVersion != preferenceVersion) {
Smartech.getInstance(WeakReference(context)).trackAppUpdate()
sharedPreferences.putString("app_version", currentVersion)
}
} else {
sharedPreferences.putString("app_version", currentVersion)
}
Track App Install and Update event by Netcore
To allow us to track the app install and update event, you just need to call the below method so we will track the app install and update event on behalf of you. We will consider the app is updated whenever the version code inside the app is greater than the previous version code.
Smartech.getInstance(new WeakReference<>(context)).trackAppInstallUpdateBySmartech();
Smartech.getInstance(WeakReference(context)).trackAppInstallUpdateBySmartech()
Tracking the Re-Installs
Step1: Creating the full backup content file.
Create an XML file in the xml directory of your resources (i.e. res/xml/my_backup_file). Copy the below snippet in the XML file.
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<include domain="sharedpref" path="smt_guid_preferences.xml"/>
<include domain="sharedpref" path="smt_preferences_guid.xml"/>
</full-backup-content>
Step2: Update the application to allow backup.
Add allowBackup
and fullBackupContent
tags in the application tag of your AndroidManifest.xml.
<application
android:name=".MyApplication"
android:allowBackup="true"
android:fullBackupContent="@xml/my_backup_file"
...
/>
NOTE:
- Re-Installs works for users with Android OS version 6.0 and higher.
- The user must be logged in to his Google account and must have enabled backup and restore in order to track re-install. (and have at least 25MB space available in Google drive)
Handling Custom In-App HTML
Note
You can skip this step if you don't want to send In-app messages via Smartech
You must implement the InAppCustomtion HTMLListener and call smartech.setInAppCustomHTMLListener() method in your application class if you are using custom HTML in-app messages.
public class MyApplication extends Application implements InAppCustomHTMLListener {
@Override
public void onCreate() {
super.onCreate();
Smartech smartech = Smartech.getInstance(new WeakReference<>(this));
smartech.initializeSdk(this);
smartech.setInAppCustomHTMLListener(this);
....
}
@Override
public void customHTMLCallback(@Nullable HashMap<String, Object> payload) {
// Handle In-App HTML callback.
}
}
class SampleApplication : Application(), InAppCustomHTMLListener {
override fun onCreate() {
super.onCreate()
val smartech: Smartech = Smartech.getInstance(WeakReference(this))
smartech.initializeSdk(this)
smartech.setInAppCustomHTMLListener(this)
...
}
override fun customHTMLCallback(payload: HashMap<String, Any>?) {
// Handle custom HTML callback.
}
}
Adding Permissions
Add below permissions in your AndroidManifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
....>
....
</application>
Updated almost 3 years ago