Android In-App
Learn how to send and control in-app notifications.
Overview
CleverTap provides the capability to send in-app messages with images, GIFs, video, and audio. You can also do A/B testing on your in-apps.
Exclude in-app from Android activity
If your application has a splash screen (for example, logo screen or loading page) displayed on app launch, then the in-app triggered on App Launch would be attempted to be displayed on this screen. As soon as this screen is dismissed, the in-app will be dismissed too.
In these types of cases, the splash screen must be excluded. Mention the Activities on which you do not want in-app notifications to show in the android:value of the metadata tag in the AndroidManifest.xml file.
<meta-data
android:name="CLEVERTAP_INAPP_EXCLUDE"
android:value="YourSplashActivity1, YourSplashActivity2" />
Control In-App Notifications
With CleverTap Android SDK 4.2.0 and above, you can control the rendering of in-app notifications by calling the following ways:
Suspend
Suspends and saves in-app notifications until resumeInAppNotifications
method is called for the current session.
CleverTapSingleton.getDefaultInstance(ctx).suspendInAppNotifications();
CleverTapAPI.getDefaultInstance(ctx)!!.suspendInAppNotifications()
Discard
Suspends the display of in-app Notifications and discards the display of any new in-app notification. It also discards in-app notifications until resumeInAppNotifications
method is called for the current session.
CleverTapSingleton.getDefaultInstance(ctx).discardInAppNotifications();
CleverTapAPI.getDefaultInstance(ctx)!!.discardInAppNotifications()
Resume
The resumeInAppNotifications
method resumes displaying in-app notifications.
If you call this method after the discardInAppNotifications()
method, it resumes the in-app notifications for events raised after the call is performed.
However, if you call the resumeInAppNotifications
method after the suspendInAppNotifications()
method, then it displays all queued in-app notifications and also resumes in-app notifications for events raised after the call is performed.
CleverTapSingleton.getDefaultInstance(ctx).resumeInAppNotifications();
CleverTapAPI.getDefaultInstance(ctx)!!.resumeInAppNotifications()
Javascript Support in In-App Notifications
Android SDK v3.5.0 and above supports embedding JavaScript code inside custom in-apps. To ensure your JavaScript code works on the app while creating the in-app campaign, select the checkbox for Enabling JavaScript during the in-app campaign creation.
Below are all methods explained with examples:
//Recording a User Event called Product Viewed in JS enabled custom In-Apps
if (window.CleverTap) {
// Call Android interface
CleverTap.pushEvent("Product Viewed");
}
//Recording a User Event called Product Viewed with properties, in JS enabled custom In-Apps
var props = {foo: 'xyz', lang: 'French'};
if (window.CleverTap) {
// Call Android interface
CleverTap.pushEvent("Product Viewed",JSON.stringify(props));
}
//Updating profile properties of the User in JS enabled custom In-Apps
var props = {foo: 'xyz', lang: 'French'};
if (window.CleverTap) {
// Call Android interface
CleverTap.pushProfile(JSON.stringify(props));
}
//Adding user property for the User in JS enabled custom In-Apps
if (window.CleverTap) {
// Call Android interface
CleverTap.addMultiValueForKey('membership', 'gold');
}
//Adding multiple user properties for the User in JS enabled custom In-Apps
var cars = ['Saab', 'Volvo', 'BMW', 'Kia'];
if (window.CleverTap) {
// Call Android interface
CleverTap.addMultiValuesForKey('Cars', JSON.stringify(cars));
}
//removing a user property for a specific key in JS enabled custom In-Apps
if (window.CleverTap) {
// Call Android interface
CleverTap.removeMultiValueForKey(âCarsâ, 'Saab');
}
//Removing multiple user properties for a specific key in JS enabled custom In-Apps
var cars = ['BMW', 'Kia'];
if (window.CleverTap) {
// Call Android interface
CleverTap.removeMultiValuesForKey('Cars', JSON.stringify(cars));
}
//Removing a user property by specifying a key in JS enabled custom In-Apps
if (window.CleverTap) {
// Call Android interface
CleverTap.removeValueForKey('Cars');
}
//Setting a user property by specifying the key in JS enabled custom In-Apps
var values = ['Mercedes','Bentley']
if (window.CleverTap) {
// Call Android interface
CleverTap.setMultiValueForKey('Cars', JSON.stringify(values));
}
In-App Notification Button onClick Callbacks
Android SDK v3.6.1 and above supports callback on the click of in-app notification buttons by returning a map of key-value pairs. To use this, make sure your activity implements the InAppNotificationButtonListener
and override the following method:
@Override
public void onInAppButtonClick(HashMap<String, String> hashMap) {
if(hashMap != null){
//Read the values
}
}
override fun onInAppButtonClick(hashMap: HashMap<String, String>?) {
// Read the values
}
Set the InAppNotificationButtonListener
using the following code:
cleverTapInstance.setInAppNotificationButtonListener(this);
cleverTapDefaultInstance?.apply {
setInAppNotificationButtonListener([email protected])
}
Updated 5 months ago