MoEngage Docs

Notification Center

MoEngage Add On Inbox library provides an out-of-box inbox support

Adding Dependency

Add the following dependency in build.gradle

compile 'com.moengage:addon-inbox:4.2.0'

Component Declaration

Add the following activity to the manifest file.

<activity
    android:name="com.moengage.addon.inbox.MoEInboxActivity"
    android:label="[ACTIVITY_NAME]" >
</activity>

Replace [ACTIVITY_NAME] with the name of the activity which you want to show for Inbox

Customizing Notification Center

You can override InboxAdapter to customise the look and feel of the notification Items.
Refer to the API documentation for helper methods to parse the payload.
Example:

First, Override the ViewHolder

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.moengage.addon.inbox.InboxManager;

public class CustomHolder extends InboxManager.ViewHolder {

  public TextView title;
  public TextView message;
  public boolean hasCouponCode;
  public String code;
  public TextView timeStamp;
  public TextView couponCode;
  public Button copyCode;
  public View couponAction;

}

Then define your own XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/moe_card_background"
    android:descendantFocusability="blocksDescendants"
    android:padding="5dp">

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:gravity="left"
        android:layout_weight=".7"
        android:layout_width="0dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="@color/label_color"
        />

    <TextView
        android:id="@+id/date"
        android:layout_marginRight="15dp"
        android:gravity="right"
        android:layout_weight=".3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        />
  </LinearLayout>

  <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
      android:layout_marginTop="5dp"
      android:layout_marginBottom="5dp"
      android:id="@+id/message"/>

  <RelativeLayout android:layout_width="match_parent"
      android:id="@+id/couponAction"
      android:layout_height="wrap_content">
    <TextView
        android:id="@+id/label_code"
        android:text="@string/labelCode"
        android:textStyle="bold"
        android:textColor="@color/label_color"
        android:gravity="left"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"/>

    <TextView
        android:id="@+id/code"
        android:gravity="left"
        android:textStyle="bold"
        android:textColor="@color/blue"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/label_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button android:layout_width="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textColor="@android:color/black"
        android:layout_height="wrap_content" android:id="@+id/btnCopy"
        android:text="@string/btnLabelCopy"/>
  </RelativeLayout>

</LinearLayout>

Then override the InboxAdapter

import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.moe.pushlibrary.providers.MoEDataContract;
import com.moengage.addon.inbox.InboxManager;
import com.moengage.addon.inbox.InboxUtils;

public class CustomAdapter extends InboxManager.InboxAdapter<CustomHolder> {
  /**
   * Makes a new view to hold the data pointed to by cursor.
   *
   * @param context Interface to application's global information
   * @param cursor The cursor from which to get the data. The cursor is already moved to the
   * correct position.
   * @param parent The parent to which the new view is attached to
   * @return the new inflated view which will be used by the adapter
   */
  @Override public View newView(Context context, Cursor cursor, ViewGroup parent,
      LayoutInflater layoutInflater) {
    return layoutInflater.inflate(R.layout.item_custom, parent, false);
  }

  /**
   * Bind an existing view to the data pointed to by cursor
   *
   * @param holder The ViewHolder which should be updated to represent the contents of the
   * item at the given position in the data set.
   * @param context Interface to application's global information
   * @param cursor The cursor from which to get the data. The cursor is already moved to the
   * correct position.
   * @return The binded View which is ready to be shown
   */
  @Override public void bindData(CustomHolder holder, Context context, Cursor cursor) {
    String details = cursor.getString(MoEDataContract.MessageEntity.COLUMN_INDEX_MSG_DETAILS);
    //validity long millis = cursor.getLong(MoEDataContract.MessageEntity.COLUMN_INDEX_GTIME)
    holder.timeStamp.setText(InboxUtils.getTimeStamp(details, "dd MMM"));
    holder.hasCouponCode = InboxUtils.hasCouponCode(details);
    holder.code = InboxUtils.getCouponCode(details);
    holder.couponCode.setText(holder.code);
    holder.title.setText(InboxUtils.getTitle(details));
    holder.message.setText(InboxUtils.getMessage(details));
    holder.copyCode.setTag(holder.code);
    if(holder.hasCouponCode){
      holder.couponAction.setVisibility(View.VISIBLE);
    }else{
      holder.couponAction.setVisibility(View.GONE);
    }

  }

  /**
   * Callback method to be invoked when an item in this AdapterView has been clicked.
   *
   * @param view The view within the AdapterView that was clicked (this will be a view provided
   * by the adapter)
   * @param context Application Context
   * @return true if Click is being overriden, false otherwise
   */
  @Override public boolean onItemClick(View view, Context context) {
    return false;
  }

  /**
   * Return the ViewHolder from this method which will be used to reduce Hierarchy lookup and also
   * be used to piggy back data required when view is clicked
   *
   * @param convertView The view which is used by the adapter
   * @return The ViewHolder which should be updated to represent the contents of the
   * item at the given position in the data set.
   */
  @Override public CustomHolder getViewHolder(View convertView) {
    CustomHolder holder = (CustomHolder) convertView.getTag();
    if( null == holder ){
      holder = new CustomHolder();
      holder.title = (TextView) convertView.findViewById(R.id.title);
      holder.couponCode = (TextView) convertView.findViewById(R.id.code);
      holder.copyCode = (Button) convertView.findViewById(R.id.btnCopy);
      holder.copyCode.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          String code = (String) v.getTag();
          copyCouponCodeToClipboard(v.getContext(), code);
        }
      });
      holder.message = (TextView) convertView.findViewById(R.id.message);
      holder.timeStamp = (TextView) convertView.findViewById(R.id.date);
      holder.couponAction = convertView.findViewById(R.id.couponAction);
      convertView.setTag(holder);
    }
    return holder;
  }

  /**
   * Copies the specified coupon code to the Clipboard.
   *
   * @param mContext
   *            An instance of the application Context
   * @param couponcode
   *            The coupon code to be added to the Clipboard
   */
  public static void copyCouponCodeToClipboard(Context mContext,
      String couponcode) {
    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
      copytoClipboardHoneyLess(mContext, couponcode);
    } else {
      copytoClipboardHoney(mContext, couponcode);
    }
    showToast("Coupon code copied to clipboard", mContext);
  }

  /**
   * Shows a Toast message
   *
   * @param message
   *            The message which needs to be shown
   * @param mContext
   *            An instance of the application Context
   */
  public static void showToast(String message, Context mContext) {
    Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private static void copytoClipboardHoney(Context mContext,
      String coupon_code) {
    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) mContext
        .getSystemService(Context.CLIPBOARD_SERVICE);
    android.content.ClipData clip = android.content.ClipData.newPlainText(
        "coupon code", coupon_code);
    clipboard.setPrimaryClip(clip);
  }

  @SuppressWarnings("deprecation")
  private static void copytoClipboardHoneyLess(Context mContext,
      String coupon_code) {
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) mContext
        .getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(coupon_code);
  }
}

Now set the custom Adapter to the InboxManager in your Application Class

InboxManager.getInstance().setInboxAdapter(new CustomAdapter());

When done, it would look like

Customizing the Inbox Activity

  • One way is to customize the theme declaration
  • Another way is to embed the InboxFragment in your activity. It can be done by using the following code
<fragment android:layout_width="match_parent"
      android:id="@+id/fragInbox"
      android:layout_height="match_parent"
      android:name="com.moengage.addon.inbox.InboxFragment" tools:layout="@layout/moe_inbox"/>

Disable Inbox Item Click

In case you want to disable clicks for the notification items stored in inbox then add the following meta tag in the activity declaration.

<activity
    android:name="com.moengage.addon.inbox.MoEInboxActivity"
    android:label="[ACTIVITY_NAME]"
    android:theme="@android:style/Theme.Light" >
    <meta-data
        android:name="CLICK_DISABLED"
        android:value="true" />
</activity>

Adding a parent activity to Inbox

In case you want to define the parent activity for the Inbox then use the below code. Replace [PARENT_ACTIVITY_AME] with the name of the parent activity.

<activity
   android:name="com.moengage.addon.inbox.MoEInboxActivity"
   android:label="[ACTIVITY_NAME]"
   android:parentActivityName="[PARENT_ACTIVITY_AME]" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
      android:name="android.support.PARENT_ACTIVITY"
      android:value="[PARENT_ACTIVITY_AME]" />
</activity>

Getting the unread message count

Use the following code to get the unread count.

MoEHelper.getInstance(mContext).getUnreadMessagesCount();

Filtering the Notification Center

Note:This feature is available from add-on inbox 2.6 onwards along with main SDK version 5.3.21

You can filter the items displayed in Notification Center based on gcm_msg_tag passed as additional parameter in push payload. If no value for key: 'gcm_msg_tag' is passed then it defaults to 'general'.

There are two ways of doing it:

  1. Passing Bundle Object to InboxFragment.java. Bundle object should contain key: 'InboxFragment.BUNDLE_EXTRA_FILTER' and value based on which you want to filter the Notification center. It will be one of the values passed for key 'gcm_msg_tag' in Push payload.
Bundle args = new Bundle();
args.putString(InboxFragment.BUNDLE_EXTRA_FILTER, "offers");
InboxFragment fragment = new InboxFragment();
fragment.setArguments(args);
  1. Invoking onQueryTextChanged(String msgTag) of InboxFragment. Where value for msgTag
    will be one of the values passed for key 'gcm_msg_tag' in Push payload.
String msgTag = "offers";
InboxFragment fragment = new InboxFragment();
fragment.onQueryTextChanged(msgTag);

Deleting Notification from Notification Center

Note:This feature is available from add-on inbox 2.7 onwards along with main SDK version 5.3.27

You can delete a particular notification from Notification Center by calling deleteItem(Context,Long) method.

//Get reference to adapter
InboxManager.InboxAdapter adapter = InboxManager.getInstance().getInboxAdapter();

//Get the id of Notification Item which got clicked
@Override public void onClick(View v) {
  InboxManager.ViewHolder holder = (InboxManager.ViewHolder) v.getTag();
  long id =holder.inboxMessage._id
//Call deleteItem method to remove Notification with above ID
	adapter.deleteItem(context,id);
}

Now, your Notification will get deleted from Notification Center and Notification Center will get updated.

Skipping Notification from Storing in Notification Center

Note:This feature is available from main SDK version 6.0.18

You can choose to not to display push notification in Notification Center by passing key "gcm_message_ignore" and value "true" in your push payload while creating campaign.
Your notification will appear on Notification tray but will not be stored in Notification Center.

Get all Inbox Messages

MoEngage SDK provides an API to fetch all the messages received by the user on the device.
Use the below API to get the messages.

InboxUtils.getInboxMessages(context);

Refer to the API documentation for more details.

Notification Center


MoEngage Add On Inbox library provides an out-of-box inbox support

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.