项目作者: diagnal

项目描述 :
ENGAGE Android SDK
高级语言:
项目地址: git://github.com/diagnal/engage-android-sdk.git
创建时间: 2017-07-05T13:57:21Z
项目社区:https://github.com/diagnal/engage-android-sdk

开源协议:

下载


N|Solid

Engage Android SDK

Engage is real-time targeted marketing campaigns that drive user acquisition and conversion.
It promotes content to users and access customer sentiment.
Engage Android SDK allows easy integration of Engage to your android application in a few steps.

Engage Android SDK class reference documents and release notes.
Engage Android SDK requires API 17 (Android 4.2) or higher.

Getting Started

Step 1: Install the Library

Method 1

Engage Android SDK library is in the Maven central repository. The easiest way to integrate Engage Android SDK library is by simply declaring it as dependency in your build.gradle file.

  1. dependencies {
  2. compile 'com.diagnal.engage:engage:1.3.6'
  3. }
Method 2

The Engage Android SDK is also distributed as aar package. You can obtain the latest Engage Android SDK aar file from https://github.com/diagnal/engage-android-sdk/

Include aar file as a dependency in you project.
The steps for adding an aar dependency can be found at https://developer.android.com/studio/projects/android-library.html#AddDependency

Step 2: Initialize the Client

The entry point of the SDK is through the Engage class. We recommend initializing the client in onCreate() your Application subclass.

  1. Engage.initialize(applicationContext, ENGAGE_ACCOUNT_ID, ENGAGE_PROJECT_ID);

ENGAGE_ACCOUNT_ID and ENGAGE_PROJECT_ID can be obtained from your Enage Dashboard

Usage

Identify User

The identify method is how you associate your users and their actions to a recognizable userId and Traits.

  1. Engage.identify(new Traits(userId)
  2. .setName(name)
  3. .setAge(age)
  4. .setEmail(email)
  5. .setGender(gender)
  6. .setRegistrationStatus(registrationStatus)
  7. );

If Traits is initialized with the default constructor or null is passed in Engage.identify, user will be treated as anonymous.

Example identify call:

  1. Engage.identify(new Traits("4981498")
  2. .setName("Jithin")
  3. .setAge(27)
  4. .setGender(Traits.GENDER.MALE)
  5. .setRegistrationStatus(Traits.STATUS.REGISTERED)
  6. );

Track

The track method lets you record any actions your users perform.

  1. Engage.track("eventName", properties);

The track method has the following parameters:

Parameter Type Description
eventName String The name of the event you’re tracking.
properties Bundle A dictionary of options.

Example track call:

  1. Bundle properties = new Bundle();
  2. properties.putString("content_title", "How to Create a Tracking Plan");
  3. properties.putString("content_id", "1234");
  4. Engage.track("play_content", properties);

Using built-in events

Engage Android SDK pre-built Events gives you the ability to track the specific actions and events in your app that are most commonly used.
For example, the content view event can be logged as follows:

  1. ContentEventCreator eventCreator = new ContentEventCreator("series_2332");
  2. eventCreator.putTitle("Game of thrones")
  3. .putSource("Details page")
  4. .putType("series");
  5. Engage.track(eventCreator.onContentView());

Engage Android SDK support the following event creators:

Event Creator Description
AdvertisementEventCreator Event creator for Advertisement playback events
ContentEventCreator Event creator for Media Content events
DownloadEventCreator Event creator for Content Download events
PlayerEventCreator Event creator for Media Playback events
PurchaseEventCreator Event creator for Purchase events
SearchEventCreator Event creator for Search events
UserEventCreator Event creator for User events

Engage allows reporting your Deep Links from external campaigns like SMS/Email/Facebook.

Follow https://developer.android.com/training/app-links/deep-linking.html to capture deep links in your app.

Once the deep link URL is available, simply call Engage.onDeepLinkReceived(url); for conversion of your deep link campaign url.

Campaigns

Campaigns are triggered by the SDK on events that are defined in the campaign page.
You can register a CampaignTriggerListener to know when a campaign is triggered and obtain an action-string based on user interaction with the campaign.

  1. Engage.setCampaignTriggerListener(new CampaignTriggerListener() {
  2. @Override
  3. public boolean onCampaignTriggered(EngageCampaign engageCampaign) {
  4. //Return false if you don't want to show the campaign dialog
  5. return true;
  6. }
  7. @Override
  8. public void onCampaignAction(EngageCampaign engageCampaign) {
  9. //Do post actions based on the actionString
  10. String actionString = engageCampaign.getActionString();
  11. }
  12. });

Messaging

Engage wraps the Firebase and Huawei messaging services to provide a simple and robust method to implement remote messaging.
To listen to push notifications or remote data messages, you just need to register with the Engage Messenger in a few simple steps.

Step 1: Add firebase in your project

To add Firebase to your app you’ll need a Firebase project and a Firebase configuration file for your app.

  1. Create a Firebase project in the Firebase console, if you don’t already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
  2. Click Add Firebase to your Android app and follow the setup steps. If you’re importing an existing Google project, this may happen automatically and you can just download the config file.
  3. When prompted, enter your app’s package name. It’s important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
  4. At the end, you’ll download a google-services.json file. You can download this file again at any time.
  5. If you haven’t done so already, copy this into your project’s module folder, typically app/.
  6. Add rules to your root-level build.gradle file, to include the google-services plugin and the Google’s Maven repository:
    1. buildscript {
    2. dependencies {
    3. classpath 'com.google.gms:google-services:3.1.0' // google-services plugin
    4. }
    5. }
    6. allprojects {
    7. repositories {
    8. maven {
    9. url "https://maven.google.com" // Google's Maven repository
    10. }
    11. }
    12. }
  7. Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:

    1. apply plugin: 'com.android.application'
    2. // .....
    3. // ADD THIS AT THE BOTTOM
    4. apply plugin: 'com.google.gms.google-services'

    Getting a “Could not find” error? Make sure you have the latest Google Repository in the Android SDK manager

    Step 2: Add Huawei push in your project

  8. Follow the Huawei HMS guide to add your application in the Huawei AppGallery. And enable the push notification service
    https://developer.huawei.com/consumer/en/service/hms/catalog/huaweipush_v3.html
  9. Once the app is created, download the agconnect-services.json file as put it in your app folder
  10. Add the following repository in allprojects section of your main gradle file:
    1. maven {url 'http://developer.huawei.com/repo/'}
  11. Add the Huawei App id in your manifest file:
    1. <meta-data
    2. android:name="com.huawei.hms.client.appid"
    3. android:value="appid=xxxxxxxx" />
  12. Add the following rules in your Proguard/R8 file:
    1. -ignorewarnings
    2. -keepattributes *Annotation*
    3. -keepattributes Exceptions
    4. -keepattributes InnerClasses
    5. -keepattributes Signature
    6. -keepattributes SourceFile,LineNumberTable
    7. -keep class com.hianalytics.android.**{*;}
    8. -keep class com.huawei.updatesdk.**{*;}
    9. -keep class com.huawei.hms.**{*;}
  13. Set Messenger.getInstance().setForceFirebaseMesssaging to true if you want to prioritize Firebase push over HMS on supported Huawei devices

NOTE: If you don’t need Huawei push support you need to perform only steps 3 and 5

Step 3: Get an instance of Messenger

  1. Messenger messenger = Messenger.getInstance();

Step 4: Register for remote messages

  1. messenger.setRemoteMessageListener(new RemoteMessageListener() {
  2. @Override
  3. public void onMessageReceived(RemoteMessage remoteMessage) {
  4. // Get HMS message data. https://developer.huawei.com/consumer/en/doc/development/HMS-References/push-RemoteMessage-cls
  5. remoteMessage.getHmsRemoteMessage();
  6. // Get Firebase message data.https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage
  7. remoteMessage.getFirebaseRemoteMessage()
  8. }
  9. @Override
  10. public void onPushNotificationReceived(NotificationData data) {
  11. //Engage push notification campaign
  12. }
  13. @Override
  14. public void onTokenRefresh(Messenger.Type type, String token) {
  15. //Firebase instance id token or HMS push token
  16. }
  17. });

Step 5: Show the notification

The simplest way to build a notification from NotificationData is to call NotificationData.getSimpleNotificationBuilder(context).

  1. NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  2. //MainActivity is the one to be opened when user clicks the notification
  3. Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  4. NotificationCompat.Builder builder = notificationData.getSimpleNotificationBuilder(getApplicationContext(), intent);
  5. notificationManager.notify(id, builder.build());
  6. messenger.onNotificationReceived(notificationData);

Don’t forget to call messenger.onNotificationReceived(notificationData) to let the Engage SDK know that the notification is shown.

Step 6: Notification open event

Once the component(Activity/Service/Receiver) set in the Notification is opened, you should call messenger.onNotificationOpened(notificationData) to let the Engage SDK know that user interacted with the notification.
You can obtain NotificationData from the Intent within the component using EXTRA_NOTIFICATION_DATA

  1. NotificationData data = getIntent().getParcelableExtra(NotificationData.EXTRA_NOTIFICATION_DATA);
  2. if (data != null) {
  3. if (Engage.hasInitailized()) {
  4. Messenger.getInstance().onNotificationOpened(data);
  5. //You'll get the appropriate `NotificationAction` based on the user interaction with the notification.
  6. String action = data.getNotificationAction().getAction()
  7. }
  8. }