Android Message Center

The default Message Center is available for iOS and Android. Minimal integration is required, and basic theming options are supported.

Installation

Message Center requires adding the urbanairship-message-center module:


dependencies {
    val airshipVersion = "17.8.0"
    
    implementation("com.urbanairship.android:urbanairship-message-center:$airshipVersion")
}

dependencies {
  def airshipVersion = "17.8.0"

  implementation "com.urbanairship.android:urbanairship-message-center:$airshipVersion"
}

Listening for Inbox Updates

To listen for inbox changes, add a InboxListener the inbox to be notified whenever the message listing changes:

MessageCenter.shared().inbox.addListener {
    // inbox updated
}
MessageCenter.shared().getInbox().addListener(() -> {
    // inbox updated
});

Styling the Message Center

The Message Center can be styled by providing a custom style in the application’s theme with the attribute messageCenterStyle. The Message Center can be customized with the following attributes:

messageCenterItemBackground
A drawable for the message item’s background
messageCenterItemTitleTextAppearance
Text appearance style for the message’s title
messageCenterItemDateTextAppearance
Text appearance style for the message’s date
messageCenterItemIconEnabled
Flag to enable message list icons instead of a checkbox
messageCenterItemIconPlaceholder
The default placeholder when using message list icons
messageNotSelectedTextAppearance
Text appearance style for the no message selected text when displaying the Message Center in a tablet view
messageNotSelectedText
String to use for the “no message selected” text when displaying the Message Center in a tablet view.
messageCenterEmptyMessageTextAppearance
Text appearance style for the empty message list text
messageCenterEmptyMessageText
String to use for the empty message list text
messageCenterDividerColor
The message list divider color
Example
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Set the custom Message Center style -->
    <item name="messageCenterStyle">@style/AppTheme.MessageCenter</item>
</style>

<style name="AppTheme.MessageCenter" parent="MessageCenter">
    <item name="messageCenterDividerColor">#44000000</item>
    <item name="messageCenterItemIconEnabled">true</item>
    <item name="messageCenterItemTitleTextAppearance">@style/AppTheme.MessageCenter.TitleTextAppearance</item>
    <item name="messageCenterItemDateTextAppearance">@style/AppTheme.MessageCenter.DateTextAppearance</item>
</style>

<!-- Custom message title text style -->
<style name="AppTheme.MessageCenter.TitleTextAppearance" parent="MessageCenter.Item.Title.TextAppearance">
    <item name="android:textSize">30sp</item>
    <item name="android:textStyle">italic</item>
    <item name="android:textColor">@android:color/black</item>
</style>

<!-- Custom message date text style -->
<style name="AppTheme.MessageCenter.DateTextAppearance" parent="MessageCenter.Item.Date.TextAppearance">
    <item name="android:textSize">16sp</item>
    <item name="urbanAirshipFontPath">"fonts/CustomFont.ttf"</item>
</style>

Message Center Filtering

Sometimes it can be useful to filter the contents of the Message Center according to some predetermined pattern. To facilitate this, use the shared MessageCenter instance to set a predicate. Once set, only messages that match the predicate will be displayed.

Filtering messages

MessageCenter.shared().predicate = Predicate { message ->
    message.title.contains("Cool")
}
Predicate<Message> predicate = new Predicate<>() {
    @Override
    public boolean apply(Message message) {
        return message.getTitle().contains("Cool");
    }
};
MessageCenter.shared().setPredicate(predicate);

Embedding the Message Center

The Message Center can be embedded in any FragmentActivity using MessageCenterFragment .

Embedding the MessageCenterFragment in a layout xml
<fragment
    android:name="com.urbanairship.messagecenter.MessageCenterFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Embedding the MessageCenterFragment using a fragment transaction

val fragment = MessageCenterFragment()
getSupportFragmentManager().beginTransaction()
        .replace(R.id.content_frame, fragment, "message_center")
        .commit()
MessageCenterFragment fragment = new MessageCenterFragment();
getSupportFragmentManager().beginTransaction()
    .replace(R.id.content_frame, fragment, "message_center")
    .commit();