CleverPush Developer Docs

CleverPush Developer Docs

  • SDK Docs
  • API Overview
  • API Reference

›Android SDK

JavaScript SDK

  • Setup
  • Methods

iOS SDK

  • Setup
  • Methods
  • Inbox View
  • Chat
  • Stories
  • Deep Links
  • Live Activities
  • Troubleshooting
  • Changelog

Android SDK

  • Setup
  • Methods
  • Notification Extender Service
  • Inbox View
  • Chat
  • Stories
  • Deep Links
  • Troubleshooting
  • Upgrading
  • Changelog

Cordova SDK

  • Capacitor Setup
  • Cordova Setup
  • Methods
  • Changelog

React Native SDK

  • Setup
  • Methods
  • Changelog

Xamarin SDK

  • Setup
  • Methods
  • Changelog

Flutter SDK

  • Setup
  • Methods
  • Chat
  • Troubleshooting
  • Changelog

Methods

Basic Usage

You can add a NotificationReceivedListener and a NotificationOpenedListener which fire when notifications have been received and/or opened:

JAVA

public class MainActivity extends Activity {
  public void onCreate(Bundle savedInstanceState) {
      CleverPush.getInstance(this).init("CLEVERPUSH_CHANNEL_ID", new NotificationReceivedListener(){
        @Override
        public void notificationReceived(NotificationOpenedResult result){
            System.out.println("Received CleverPush Notification: " +result.getNotification().getTitle());
        }
      }, new NotificationOpenedListener() {
        @Override
        public void notificationOpened(NotificationOpenedResult result) {
            System.out.println("Opened CleverPush Notification: " + result.getNotification().getTitle());
        }
      }, true); // autoRegister: You can set this to false to prevent automatic subscribing on the first launch
  }
}

KOTLIN

class MainActivity:Activity() {
fun onCreate(savedInstanceState:Bundle) {
   CleverPush.getInstance(this).init("CLEVERPUSH_CHANNEL_ID",
               NotificationReceivedListener { result -> println("ReceivedCleverPushNotification: " + result.notification.title) },
               NotificationOpenedListener { result -> println("Opened CleverPush Notification: " + result.notification.title) })
  }
}

Please note that autoRegister is turned to true in the above example. It means that the CleverPush SDK will automatically try to subscribe the user on the first launch of the app. If you later call unsubscribe() the SDK will not automatically try to subscribe again, instead you would have to call subscribe() yourself again.

Instead of a NotificationReceivedListener you could also use a NotificationReceivedCallbackListener. This way you can dynamically control if you want to show a notification when the app is running in foreground:

JAVA

CleverPush.getInstance(this).init("CLEVERPUSH_CHANNEL_ID", new NotificationReceivedCallbackListener() {
   @Override
   public boolean notificationReceivedCallback(NotificationOpenedResult notificationOpenedResult) {
      boolean showNotification = true;
      return showNotification;
   }
}, ...);

KOTLIN

CleverPush.getInstance(this).init("XXXXXXX", object:NotificationReceivedCallbackListener() {
fun notificationReceivedCallback(notificationOpenedResult:NotificationOpenedResult):Boolean {
   val showNotification = true
   return showNotification
}
}, ...)

You can add a SubscribedListener which fires when the user has successfully been subscribed:

JAVA

public class MainActivity extends Activity {
  public void onCreate(Bundle savedInstanceState) {
    CleverPush.getInstance(this).init("CLEVERPUSH_CHANNEL_ID", new NotificationReceivedListener() {
      @Override
      public void notificationReceived(NotificationOpenedResult result) {
        System.out.println("Received CleverPush Notification: " + result.getNotification().getTitle());
      }
    }, new NotificationOpenedListener() {
      @Override
      public void notificationOpened(NotificationOpenedResult result) {
        System.out.println("Opened CleverPush Notification: " + result.getNotification().getTitle());
      }
    }, new SubscribedListener() {
      @Override
      public void subscribed(String subscriptionId) {
        System.out.println("CleverPush Subscription ID: " + subscriptionId);
      }
    });
  }
}

KOTLIN

class MainActivity:Activity() {
fun onCreate(savedInstanceState:Bundle) {
   CleverPush.getInstance(this).init("CLEVERPUSH_CHANNEL_ID",
               { result -> println("ReceivedCleverPushNotification: " + result.notification.title) },
               { result -> println("Opened CleverPush Notification: " + result.notification.title) },
               { subscriptionId -> System.out.println("CleverPush Subscription ID: $subscriptionId"); }
      )
  }
}

Subscribe / Unsubscribe:

JAVA

public class MainActivity extends Activity {
  public void onCreate(Bundle savedInstanceState) {
    // last parameter (autoRegister) is false
    CleverPush.getInstance(this).init(..., false);

    // subscribe
    CleverPush.getInstance(this).subscribe();

    // or unsubscribe
    CleverPush.getInstance(this).unsubscribe();

    // get subscription status (true or false)
    CleverPush.getInstance(this).isSubscribed();
  }
}

KOTLIN

class MainActivity:Activity() {
  fun onCreate(savedInstanceState:Bundle) {
    // last parameter (autoRegister) is false
    CleverPush.getInstance(this).init(..., false)

    // subscribe
    CleverPush.getInstance(this).subscribe()
    // or unsubscribe
    CleverPush.getInstance(this).unsubscribe()
    // get subscription status (true or false)
    CleverPush.getInstance(this).isSubscribed()
  }
}

Tags

JAVA

CleverPush.getInstance(this).getAvailableTags(tags -> {
   // returns Set<ChannelTag>
});

Set<String> subscribedTagIds = CleverPush.getInstance(this).getSubscriptionTags();

// add single tag
CleverPush.getInstance(this).addSubscriptionTag("TAG_ID")

// add multiple tags
CleverPush.getInstance(this).addSubscriptionTags(new String[] {"TAG_ID_1", "TAG_ID_2"});

// remove single tag
CleverPush.getInstance(this).removeSubscriptionTag("TAG_ID")

// remove multiple tags
CleverPush.getInstance(this).removeSubscriptionTags(new String[] {"TAG_ID_1", "TAG_ID_2"});

boolean hasTag = CleverPush.getInstance(this).hasSubscriptionTag(channelTags.get(0).getId());

KOTLIN

CleverPush.getInstance(this).getAvailableTags({ tags->
                                             // returns Set<ChannelTag>
                                             })
val subscribedTagIds = CleverPush.getInstance(this).getSubscriptionTags()

// add single tag
CleverPush.getInstance(this).addSubscriptionTag("TAG_ID")

// add multiple tags
CleverPush.getInstance(this).addSubscriptionTags(arrayOf<String>("TAG_ID_1", "TAG_ID_2"))

// remove single tag
CleverPush.getInstance(this).removeSubscriptionTag("TAG_ID")

// remove multiple tags
CleverPush.getInstance(this).removeSubscriptionTags(arrayOf<String>("TAG_ID_1", "TAG_ID_2"))

val hasTag = CleverPush.getInstance(this).hasSubscriptionTag(channelTags.get(0).getId())

Automatic Tag Assignment

The SDK can also automatically assign tags by using the trackPageView method. In simple cases you can just give the method a URL. In the CleverPush backoffice you can then set trigger the tags by matching URL Pathname RegExes. You can optionally also set combinations of min. visits, seconds or sessions for this tag.

Let's say you have created a tag with the URL pathname regex "/sports". This would trigger the tag for a subscriber:

CleverPush.getInstance(this).trackPageView("https://example.com/sports/article-123123");

We can also have more advanced use cases here by using Javascript functions for matching. For example you created a tag with the following function in the CleverPush backend: params.category === "sports". This would then trigger the tag for a subscriber:

CleverPush.getInstance(this).trackPageView("https://example.com/anything", new HashMap<String, String>() {{
   put("category", "sports");
}}););

Once the trackPageView method has been implemented you can set up all the tags dynamically in the CleverPush backend without touching your code.

Automatic View Tracking

The SDK can also automatically track view by using the autoTrackWebViewPages and setWebViewClientListener methods.

autoTrackWebViewPages You just need to add autoTrackWebViewPages to your webview clients doUpdateVisitedHistory method like below

new WebViewClient(){
            @Override
            public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
                cleverPush.autoTrackWebViewPages(url);
                super.doUpdateVisitedHistory(view, url, isReload);
            }
        }

setWebViewClientListener

You can set a setWebViewClientListener it will do Automatic View Tracking and you will get all the callback of WebViewClient in Listener :

cleverPush.setWebViewClientListner(webView, new WebViewClientListener() {
   ...
}

Attributes

JAVA

CleverPush.getInstance(this).getAvailableAttributes(attributes -> {
   // returns Set<CustomAttribute>
});

Map<String, String> subscriptionAttributes = CleverPush.getInstance(this).getSubscriptionAttributes();

String attributeValue = CleverPush.getInstance(this).getSubscriptionAttribute("user_id");

// You can set string values like this
CleverPush.getInstance(this).setSubscriptionAttribute("user_id", "1");

// Please provide dates in the following format: YYYY-MM-DD
CleverPush.getInstance(this).setSubscriptionAttribute("birthdate", "YYYY-MM-DD");

// You can also push/pull values to special array attributes (e.g. "categories")
CleverPush.getInstance(this).pushSubscriptionAttributeValue("categories", "category_1");
CleverPush.getInstance(this).pullSubscriptionAttributeValue("categories", "category_1");

KOTLIN

CleverPush.getInstance(this).getAvailableAttributes({ attributes->
                                                   // returns Set<CustomAttribute>
                                                   })
val subscriptionAttributes = CleverPush.getInstance(this).getSubscriptionAttributes()

val attributeValue = CleverPush.getInstance(this).getSubscriptionAttribute("user_id")

// You can set string values like this
CleverPush.getInstance(this).setSubscriptionAttribute("user_id", "1")

// Please provide dates in the following format: YYYY-MM-DD
CleverPush.getInstance(this).setSubscriptionAttribute("birthdate", "YYYY-MM-DD")

// You can also push/pull values to special array attributes (e.g. "categories")
CleverPush.getInstance(this).pushSubscriptionAttributeValue("categories", "category_1")
CleverPush.getInstance(this).pullSubscriptionAttributeValue("categories", "category_1")

Country & Language

You can optionally override the country & language which is automatically detected from the system and can be used for targeting / translations.

JAVA

CleverPush.getInstance(this).setSubscriptionLanguage("en");
CleverPush.getInstance(this).setSubscriptionCountry("US");

Topics

JAVA

CleverPush.getInstance(this).getAvailableTopics(topics -> {
   // returns Set<ChannelTopic>
});

Set<String> subscribedTopicIds = CleverPush.getInstance(this).getSubscriptionTopics();

CleverPush.getInstance(this).setSubscriptionTopics(new String[]{"ID_1", "ID_2"});

boolean hasTopic = CleverPush.getInstance(this).hasSubscriptionTopic("TOPIC_ID");

// let the user choose his topics
CleverPush.getInstance(this).showTopicsDialog();

CleverPush.getInstance(this).setTopicsChangedListener(new TopicsChangedListener() {
    @Override
    public void topicsChanged(Set<String> topicIds) {

    }
});

KOTLIN

CleverPush.getInstance(this).getAvailableTopics({ topics->
                                             // returns Set<ChannelTopic>
                                             })

val subscribedTopicIds = CleverPush.getInstance(this).getSubscriptionTopics()
CleverPush.getInstance(this).setSubscriptionTopics(arrayOf<String>("ID_1", "ID_2"))

val hasTopic = CleverPush.getInstance(this).hasSubscriptionTopic("TOPIC_ID");

// let the user choose his topics
CleverPush.getInstance(this).showTopicsDialog()

Here is how the topics dialog looks like:

Topics Dialog Android

Received Notifications

JAVA

//get the locally stored notification.
Set<Notification> = CleverPush.getInstance(this).getNotifications();

KOTLIN

//get the locally stored notification.
CleverPush.getInstance(this).getNotifications()

JAVA

// get remote notification and local notification based on the boolean argument.
// - if you pass boolean argument YES you will get the list of remote notification else you will get the locally stored notification.
CleverPush.getInstance(this).getNotifications(true, new NotificationsCallbackListener() {
    @Override
    public void ready(Set<Notification> notifications) {

    }
});

KOTLIN

 // get remote notification and local notification based on the boolean argument.
// - if you pass boolean argument YES you will get the list of remote notification else you will get the locally stored notification.
CleverPush.getInstance(this).getNotifications(true) { }

Remove Notification

You can remove notification stored locally using Notification ID

JAVA


CleverPush.getInstance(this).removeNotification("Notification ID");

KOTLIN


CleverPush.getInstance(this).removeNotification("Notification ID")

Notification Styles

CleverPush automatically chooses the fitting Notification Style for you (e.g. BigImageStyle or BigTextStyle). We also provide a way that you can choose the displayed Notification style:

JAVA

// Available Notification styles:
CleverPush.NotificationStyle.AUTO // default style
CleverPush.NotificationStyle.BIG_TEXT // big text style
CleverPush.NotificationStyle.BIG_PICTURE // big picture style
CleverPush.NotificationStyle.TEXT_WITH_IMAGE // custom style with big image and text in expanded view

CleverPush.getInstance(this).setNotificationStyle(CleverPush.NotificationStyle.AUTO);

App Banners

(Available from version 1.8.0)

JAVA

// Will be called, once a user presses a button in the banner
CleverPush.getInstance(this).setAppBannerOpenedListener(action -> {
   System.out.println("App Banner Opened");
});

// You can emit custom events and use them as a trigger for your banners
CleverPush.getInstance(this).triggerAppBannerEvent("key", "value");

// You can also show one banner by its ID (we recommend app banner events for production usage)
CleverPush.getInstance(this).showAppBanner("BANNER_ID");

KOTLIN

// Will be called, once a user presses a button in the banner
CleverPush.getInstance(this).setAppBannerOpenedListener({ action-> println("App Banner Opened") })

// You can emit custom events and use them as a trigger for your banners
CleverPush.getInstance(this).triggerAppBannerEvent("key", "value")

// You can also show one banner by its ID (we recommend app banner events for production usage)
CleverPush.getInstance(this).showAppBanner("BANNER_ID")

Conversion Event Tracking

Events can be used to track conversions.

JAVA

CleverPush.getInstance(this).trackEvent("EVENT NAME");

// track a conversion with a specified amount
CleverPush.getInstance(this).trackEvent("EVENT NAME", 37.50f);

KOTLIN

CleverPush.getInstance(this).trackEvent("EVENT NAME")

// track a conversion with a specified amount
CleverPush.getInstance(this).trackEvent("EVENT NAME", 37.50f)

Follow up Events

Events can be used to trigger follow-up campaigns.

JAVA

CleverPush.getInstance(this).triggerFollowUpEvent("EVENT NAME");

// add custom parameters
CleverPush.getInstance(this).triggerFollowUpEvent("EVENT NAME", new HashMap<String, String>() {{
  put("id", "123456");
}});

KOTLIN

CleverPush.getInstance(this).triggerFollowUpEvent("EVENT NAME")

// add custom parameters
CleverPush.getInstance(this).triggerFollowUpEvent("EVENT NAME", hashMapOf("id" to "123456"))

Tracking Consent

You can optionally require a tracking consent from the user (e.g. you get this consent from a CMP). If you tell our SDK to wait for the tracking consent, it will not call any tracking-related features until the consent is available. Calls will be queued and automatically executed until the consent is available.

Step 1: Call this before initializing the SDK:

CleverPush.getInstance(this).setTrackingConsentRequired(true);

Step 2: Call this when the user gave his consent (needs to be called on every launch):

CleverPush.getInstance(this).setTrackingConsent(true);

Geo Fencing

For using Geo Fencing you need to request the location permission from the user.

CleverPush.getInstance(this).requestLocationPermission();

You can also check at any time if the user has already granted the permission:

boolean hasPermission = CleverPush.getInstance(this).hasLocationPermission();
← SetupNotification Extender Service →
  • Basic Usage
  • Tags
  • Automatic Tag Assignment
  • Automatic View Tracking
  • Attributes
  • Country & Language
  • Topics
  • Received Notifications
  • Remove Notification
  • Notification Styles
  • App Banners
  • Conversion Event Tracking
  • Follow up Events
  • Tracking Consent
  • Geo Fencing
SDKs
JavaScriptiOSAndroidCordovaCapacitorReact NativeXamarinFlutter
Community
TwitterFacebookGitHub
More
API ReferenceAPI OverviewBlogImprintPrivacy PolicyTerms of serviceGDPR
Copyright © 2023 CleverPush