Notification Center for MoEngage SDK version 11.1.00
warning

Critical

This module is deprecated; integrate this module only if you are using 11.1.00 or below version of moe-android-sdk. Refer to Notification Center. If you are using a later version of moe-android-sdk.

Adding dependency

Add the following dependency in build.gradle.

Groovy
implementation("com.moengage:addon-inbox:$sdkVersion")

where $sdkVersion should be replaced by the latest version of the SDK.

Component Declaration

Add the following activity to the manifest file.

XML
<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

316d6e5-inbox1.png

Customizing Notification Center

You can override InboxAdapter to customize the look and feel of the notification Items.
Example:

First, Override the ViewHolder

Java
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
<?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

Java
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 {
  /**
   * 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

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

When done, it would look like

6dc568e-notification_center.png

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
XML
<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 the inbox then add the following meta tag in the activity declaration.

XML
<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.

XML
<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>

Filtering the Notification Center

info

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 an additional parameter in the 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.

    Java
    Bundle args = new Bundle();
    args.putString(InboxFragment.BUNDLE_EXTRA_FILTER, "offers");
    InboxFragment fragment = new InboxFragment();
    fragment.setArguments(args);
  2. 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.
    Java
    String msgTag = "offers";
    InboxFragment fragment = new InboxFragment();
    fragment.onQueryTextChanged(msgTag);

Deleting Notification from Notification Center

info

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 the Notification Center by calling the deleteItem(Context,Long) method.

Java
//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 the Notification Center, and the Notification Center will get updated.

Self Handled Inbox

The below APIs can be used to build a custom inbox of your own.
These APIs are available only from the SDK version 5.3.0 and above.

Get all messages

Kotlin Java
// synchronous API should not be called on the main thread
MoEInboxHelper.getInstance().fetchAllMessages(context)
// asynchronous API
MoEInboxHelper.getInstance().fetchAllMessagesAsync(applicationContext, listener)

Get Unread Notifications Count

Kotlin Java
MoEInboxHelper.getInstance().getUnClickedMessagesCount()

Track Inbox Notification Clicks

Kotlin Java
MoEInboxHelper.getInstance().trackMessageClicked(context, inboxMessage)

Delete Inbox Message

Kotlin Java
MoEInboxHelper.getInstance().deleteMessage(context, inboxMessage)

Version Compatibility

Core SDK Version  Inbox-Core Version

11.0.03 and above

6.0.1 and above

11.0.00-11.0.02

6.0.0

10.0.00 - 10.6.02

5.0.0 - 5.3.0

Previous

Next

Was this article helpful?
0 out of 0 found this helpful

How can we improve this article?