Android SDK
PushEngage Android SDK allows you to integrate Push Notification service into your Android applications. It can be used with both Kotlin and Java based native Android applications to integrate push notification using FCM.
This document shows the usage of the Android SDK and the ways to use the methods of the library.
Get SDK Version
Retrieves the current version of the PushEngage Android SDK, returning a string that represents the SDK's current version.
Syntax
getSdkVersion()
Returns
A String representing the current version of the PushEngage Android SDK.
Usage
- Java
- Kotlin
PushEngage.getSdkVersion();
PushEngage.getSdkVersion()
Subscribe
For Android 13 and above, the client should call this method once notification permission is granted.
Syntax
subscribe()
Usage
- Java
- Kotlin
PushEngage.subscribe();
PushEngage.subscribe()
Set Notification Small Icon
Sets the resource name of the small icon used for notifications. The small icon appears in the status bar when a notification is displayed.
Syntax
setSmallIconResource(String resourceName)
Parameters
resourceName
: A string representing the resource name of the small icon.
Usage
- Java
- Kotlin
String resourceName = "your_small_icon_name";
PushEngage.setSmallIconResource(resourceName);
val resourceName = "your_small_icon_name"
PushEngage.setSmallIconResource(resourceName)
It is recommended to set a valid resource name to ensure proper display of notifications. If an invalid resource name is provided, the default bell icon specified by the PushEngage library will be used.
Retrieve Subscriber ID
Retrieves the device hash generated based on the device token associated with the client app.
This method retrieves the unique subscriber ID for a user. PushEngage generates this ID for every user based on their subscription data. Sometimes, this ID is referred to as the 'subscriber_hash'. It will return a string value.
Syntax
getDeviceTokenHash()
Usage
- Java
- Kotlin
PushEngage.getDeviceTokenHash();
PushEngage.getDeviceTokenHash()
Retrieve Subscriber Details
This method retrieves subscriber details based on a provided list of strings, which can include city, state, country, device, device type, segments, etc.
Syntax
getSubscriberDetails(List<String> values, PushEngageResponseCallback callback)
Parameters
values
: A list of strings that can contain any values from PushEngage.SubscriberFields
.
callback
: An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
List<String> subscriberDetailsList = new ArrayList<>();
subscriberDetailsList.add(PushEngage.SubscriberFields.City);
subscriberDetailsList.add(PushEngage.SubscriberFields.State);
PushEngage.getSubscriberDetails(subscriberDetailsList, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//Handle the failure event.
}
});
val subscriberDetailsList: MutableList<String> = ArrayList()
subscriberDetailsList.add(PushEngage.SubscriberFields.City)
subscriberDetailsList.add(PushEngage.SubscriberFields.State)
PushEngage.getSubscriberDetails(
subscriberDetailsList,
object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})
Profile ID to Subscriber
Profile IDs serve as unique identifiers for your subscribers, enabling you to recognize them across multiple devices. Each subscriber can be assigned just one profile ID. This ID should be a string, and you have the flexibility to use any value, such as an email or phone number.
This method allows you to set a profile ID for the current subscriber. If a profile ID already exists, it will be replaced with the new value.
Syntax
addProfileId(String profileId, PushEngageResponseCallback callback)
Parameters
profileId
: A String representing the profile ID to be associated with the subscriber.
callback
(optional): An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
PushEngage.addProfileId("your_id", new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the Success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//Handle the Failure event.
}
});
PushEngage.addProfileId("your_id", object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})
Automated/Personalized Notifications
Automated notifications include all types of triggered campaigns, such as cart abandonment, price drop, back in stock, and browse abandonment. By default, automated notifications are enabled for all subscribers.
Enable/Disable Automated Notifications for Subscriber
This method allows you to enable/disable automated notifications for the current subscriber.
Syntax
automatedNotification(TriggerStatusType status, PushEngageResponseCallback callback)
Parameters
status
: The trigger status type indicating the status of the trigger campaign.
callback
: An interface designed as a callback to handle API responses asynchronously.
Usage
Enable Automated Notifications
- Java
- Kotlin
PushEngage.automatedNotification(PushEngage.TriggerStatusType.enabled, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
Toast.makeText(TriggerCampaignActivity.this, "Trigger Enabled Successfully", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
Toast.makeText(TriggerCampaignActivity.this, "Trigger Enabled Failed", Toast.LENGTH_LONG).show();
}
});
PushEngage.automatedNotification(PushEngage.TriggerStatusType.enabled, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
Toast.makeText(this@TriggerCampaignActivity, "Trigger Enabled Successfully", Toast.LENGTH_LONG).show()
}
override fun onFailure(errorCode: Int?, errorMessage: String?) {
Toast.makeText(this@TriggerCampaignActivity, "Trigger Enabled Failed", Toast.LENGTH_LONG).show()
}
})
Disable Automated Notifications
- Java
- Kotlin
PushEngage.automatedNotification(PushEngage.TriggerStatusType.disabled, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
Toast.makeText(TriggerCampaignActivity.this, "Trigger Enabled Successfully", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
Toast.makeText(TriggerCampaignActivity.this, "Trigger Enabled Failed", Toast.LENGTH_LONG).show();
}
});
PushEngage.automatedNotification(PushEngage.TriggerStatusType.disabled, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
Toast.makeText(this@TriggerCampaignActivity, "Trigger Enabled Successfully", Toast.LENGTH_LONG).show()
}
override fun onFailure(errorCode: Int?, errorMessage: String?) {
Toast.makeText(this@TriggerCampaignActivity, "Trigger Enabled Failed", Toast.LENGTH_LONG).show()
}
})
Goal Tracking
Goal Tracking will help you assign conversion goals & value to your notification campaigns. You can set up a default goal and have it integrated for all your campaigns.
Syntax
sendGoal(Goal goal, PushEngageResponseCallback callback)
Parameters
goal
: Goal object representing the goal to be tracked.
callback
: An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
Goal goal = new Goal("name_of_goal", 1, 10.0);
PushEngage.sendGoal(goal, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
Toast.makeText(GoalActivity.this, "Goal Added Successfully", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
Toast.makeText(GoalActivity.this, "Failure", Toast.LENGTH_LONG).show();
}
});
val goal = Goal("name_of_goal", 1, 10.0)
PushEngage.sendGoal(goal, object: PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
Toast.makeText(this@GoalActivity, "Goal Added Successfully", Toast.LENGTH_LONG).show()
}
override fun onFailure(errorCode: Int?, errorMessage: String?) {
Toast.makeText(this@GoalActivity, "Failure", Toast.LENGTH_LONG).show()
}
})
Triggered Campaigns
Custom Trigger Campaign
Detect your visitor’s behavior to send automated push notifications to the right person at the right time.
Syntax
sendTriggerEvent(TriggerCampaign trigger, PushEngageResponseCallback callback)
Parameters
triggerCampaign
: The TriggerCampaign object representing the campaign event to be triggered.
callback
: An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
Map<String, String> dataMap = new HashMap<>();
dataMap.put("custom_key", "custom_value");
TriggerCampaign triggerCampaign = new TriggerCampaign(
"name_of_campaign",
"name_of_event",
"your_reference_id", // optional
dataMap // optional
);
PushEngage.sendTriggerEvent(triggerCampaign, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
Toast.makeText(TriggerEntryActivity.this, "Send Trigger Alert Successfully", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
Toast.makeText(TriggerEntryActivity.this, errorMessage != null ? errorMessage : "Error occurred", Toast.LENGTH_LONG).show();
}
});
val dataMap: MutableMap<String, String> = mutableMapOf()
dataMap["custom_key"] = "custom_value"
val triggerCampaign = TriggerCampaign(
campaignName = "name_of_campaign",
eventName = "name_of_event",
referenceId = "your_reference_id", // optional
data = dataMap // optional
)
PushEngage.sendTriggerEvent(triggerCampaign, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
Toast.makeText(this@TriggerEntryActivity, "Send Trigger Alert Successfully", Toast.LENGTH_LONG).show()
}
override fun onFailure(errorCode: Int?, errorMessage: String?) {
Toast.makeText(this@TriggerEntryActivity, errorMessage.toString(), Toast.LENGTH_LONG).show()
}
})
Trigger Alert
Re-engage your customers and increase conversion using Price Drop Alert Campaigns and Inventory Alert Campaigns.
addAlert(TriggerAlert alert, PushEngageResponseCallback callback)
Parameters
alert
: The TriggerAlert object representing the alert to be added.
callback
: An interface designed as a callback to handle API responses asynchronously.
Usage
Price Drop
- Java
- Kotlin
Map<String, String> dataMap = new HashMap<>();
dataMap.put("custom_key", "custom_value");
TriggerAlert triggerAlert = new TriggerAlert(
TriggerAlertType.priceDrop,
"your_product_id",
"your_link",
100.0,
"your_variant_id", // optional
"your_expiry_timestamp", // optional
102.0, // optional
.inStock, // optional, possible values: [.inStock, .outOfStock]
dataMap // optional
);
PushEngage.addAlert(triggerAlert, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
Toast.makeText(AddAlertActivity.this, "Add Alert Successfully", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
Toast.makeText(AddAlertActivity.this, errorMessage, Toast.LENGTH_LONG).show();
}
});
val dataMap: MutableMap<String, String> = mutableMapOf()
dataMap["custom_key"] = "custom_value"
val triggerAlert = TriggerAlert(
type = TriggerAlertType.priceDrop,
productId = "your_product_id",
link = "your_link",
price = 100.0,
variantId = "your_variant_id", // optional
expiryTimestamp = "your_expiry_timestamp", // optional
alertPrice = 102.0, // optional
availability = .inStock, // optional, possible values: [.inStock, .outOfStock]
data = dataMap // optional
)
PushEngage.addAlert(triggerAlert, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
Toast.makeText(this@AddAlertActivity, "Add Alert Successfully", Toast.LENGTH_LONG).show()
}
override fun onFailure(errorCode: Int?, errorMessage: String?) {
Toast.makeText(this@AddAlertActivity, errorMessage, Toast.LENGTH_LONG).show()
}
})
Back in Stock Alert
- Java
- Kotlin
Map<String, String> dataMap = new HashMap<>();
dataMap.put("custom_key", "custom_value");
TriggerAlert triggerAlert = new TriggerAlert(
TriggerAlertType.inventory,
"your_product_id",
"your_link",
100.0,
"your_variant_id", // optional
"your_expiry_timestamp", // optional
.inStock, // optional, possible values: [.inStock, .outOfStock]
dataMap // optional
);
PushEngage.addAlert(triggerAlert, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
Toast.makeText(AddAlertActivity.this, "Add Alert Successfully", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
Toast.makeText(AddAlertActivity.this, errorMessage, Toast.LENGTH_LONG).show();
}
});
val dataMap: MutableMap<String, String> = mutableMapOf()
dataMap["custom_key"] = "custom_value"
val triggerAlert = TriggerAlert(
type = TriggerAlertType.inventory,
productId = "your_product_id",
link = "your_link",
price = 100.0,
variantId = "your_variant_id", // optional
expiryTimestamp = "your_expiry_timestamp", // optional
availability = .inStock, // optional, possible values: [.inStock, .outOfStock]
data = dataMap // optional
)
PushEngage.addAlert(triggerAlert, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
Toast.makeText(this@AddAlertActivity, "Add Alert Successfully", Toast.LENGTH_LONG).show()
}
override fun onFailure(errorCode: Int?, errorMessage: String?) {
Toast.makeText(this@AddAlertActivity, errorMessage, Toast.LENGTH_LONG).show()
}
})
Segments
Segments are used to group subscribers so that you can send personalized notifications. Segments can be created based on attributes, categories, and more.
Add a Subscriber to Segments
This method enables you to add the current subscriber to segments.
Syntax
addSegment(List<String> segmentId, PushEngageResponseCallback callback)
Parameters
segmentId
: A list of segment IDs to be added.
callback
(optional): An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
List<String> segmentList = new ArrayList<String>();
segmentList.add("sports");
PushEngage.addSegment(segmentList, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the Success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//Handle the Failure event.
}
});
val segmentList: MutableList<String> = ArrayList()
segmentList.add("sports")
PushEngage.addSegment(segmentList, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})
Add a Subscriber to a Segment with Duration
This method enables you to add the current subscriber to a segment for a specified duration, measured in days. After this period, the segment will be automatically removed from the subscriber.
Syntax
addDynamicSegment(List<AddDynamicSegmentRequest.Segment> segments, PushEngageResponseCallback callback)
Parameter
segments
: A list of segment objects representing dynamic segments to be added.
callback
(optional): An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
AddDynamicSegmentRequest addDynamicSegmentRequest = new AddDynamicSegmentRequest();
List<AddDynamicSegmentRequest.Segment> segments = new ArrayList<>();
AddDynamicSegmentRequest.Segment segment = addDynamicSegmentRequest.new Segment("sports", 5);
segments.add(segment);
PushEngage.addDynamicSegment(segments, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the Success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//Handle the Failure event.
}
});
val addDynamicSegmentRequest = AddDynamicSegmentRequest()
val segments: MutableList<AddDynamicSegmentRequest.Segment> = ArrayList()
val segment = addDynamicSegmentRequest.Segment("sports", 5)
segments.add(segment)
PushEngage.addDynamicSegment(segments, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})
Remove a Subscriber from Segments
This method allows you to remove the current subscriber from segments.
Syntax
removeSegment(List<String> segmentId, PushEngageResponseCallback callback)
Parameters
segmentId
: A list of segment IDs to be removed.
callback
(optional): An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
List<String> segmentList = new ArrayList<String>();
segmentList.add("sports");
PushEngage.removeSegment(segmentList, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the Success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//Handle the Failure event.
}
});
val segmentList: MutableList<String> = ArrayList()
segmentList.add("sports")
PushEngage.removeSegment(segmentList, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})
Attributes
Attributes are key-value pairs that allow you to store additional information about your subscribers. You can utilize attributes to segment your subscribers and send personalized notifications.
Add Attributes to Subscriber
Use this method to add or update attributes for a subscriber. If an attribute with the specified key already exists, the existing value will be replaced.
Syntax
addSubscriberAttributes(JSONObject obj, PushEngageResponseCallback callback)
Parameters
obj
: A JSONObject
containing the subscriber attributes to be added.
callback
(optional): An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("age", "25");
jsonObject.put("height", "6.1");
PushEngage.addSubscriberAttributes(jsonObject, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the Success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//Handle the Failure event.
}
});
} catch (JSONException e) {
e.printStackTrace();
}
val jsonObject = JSONObject()
jsonObject.put("age", "25")
jsonObject.put("height", "6.1")
PushEngage.addSubscriberAttributes(
jsonObject,
object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})
Set Attributes for Subscriber
This method allows you to set attributes for a subscriber, replacing any previously associated attributes. Use this method when you need to entirely reset the attributes with new values.
Syntax
setSubscriberAttributes(JSONObject obj, PushEngageResponseCallback callback)
Parameters
obj
: A JSONObject
containing the updated subscriber attributes.
callback
(optional): An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("age", "25");
jsonObject.put("height", "6.1");
PushEngage.setSubscriberAttributes(jsonObject, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the Success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//Handle the Failure event.
}
});
} catch (JSONException e) {
e.printStackTrace();
}
val jsonObject = JSONObject()
jsonObject.put("age", "25")
jsonObject.put("height", "6.1")
PushEngage.setSubscriberAttributes(
jsonObject,
object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})
Get Attributes for Subscriber
Retrieve the attributes associated with the current subscriber using this method.
Syntax
getSubscriberAttributes(PushEngageResponseCallback callback)
Parameter
callback
: An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
PushEngage.getSubscriberAttributes(new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the Success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//TODO Handle the Failure event.
}
});
PushEngage.getSubscriberAttributes(object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})
Remove Attributes for Subscriber
This method allows you to remove one or more attributes from the current subscriber. Provide an array of attribute names you wish to remove. Passing an empty array will result in the removal of all the subscriber's attributes.
Syntax
deleteSubscriberAttributes(List<String> values, PushEngageResponseCallback callback)
Parameters
values
: A List<String>
containing attribute names to be deleted.
callback
(optional): An interface designed as a callback to handle API responses asynchronously.
Usage
- Java
- Kotlin
List<String> attributeList = new ArrayList<>();
attributeList.add("age");
attributeList.add("height");
PushEngage.deleteSubscriberAttributes(attributeList, new PushEngageResponseCallback() {
@Override
public void onSuccess(Object responseObject) {
//Handle the Success event.
}
@Override
public void onFailure(Integer errorCode, String errorMessage) {
//Handle the Failure event.
}
});
val attributeList: MutableList<String> = ArrayList()
attributeList.add("age")
attributeList.add("height")
PushEngage.deleteSubscriberAttributes(attributeList, object : PushEngageResponseCallback {
override fun onSuccess(responseObject: Any?) {
//Handle the Success event.
}
override fun onFailure(errorCode: Int, errorMessage: String) {
//Handle the Failure event.
}
})