Skip to main content

Handle Push Notification Click

This document describes the SDK's default behavior when a user clicks a remote push notification, and how your application can handle click events for remote push notifications.

tip

This document covers remote push notification click handling only. For local notification click handling, refer to the relevant documentation. To distinguish between local notifications and remote push notifications, see the related knowledge base articles.

Implement the SDK default navigation behavior

The SDK handles remote push notification click events by default. When a user clicks a notification, the SDK fires the corresponding implicit Intent. You need to configure an intent-filter in your app's manifest file (AndroidManifest.xml) to receive the Intent and complete the default navigation.

  • Notification from a single contact: When a user clicks one or more notifications from a single contact, the SDK navigates to the channel page by default.
  • Collapsed notification from multiple contacts: When multiple contacts send multiple messages, their notifications are collapsed. When a user clicks this collapsed notification, the SDK navigates to the channel list page by default.
  • It is recommended to always navigate to the channel list page, and call the connect method before opening the list page to ensure the latest pushed messages are visible.

Navigate to the channel matching the message

When a user clicks one or more notifications from a single contact, the SDK fires an Intent to navigate to the channel page.

Add the following intent-filter to the Activity that opens a single conversation in your app. The Android Chat SDK does not ship a built-in conversation UI Activity, so replace com.example.app.chat.ChannelActivity with your own class.

xml
<activity
android:name="com.example.app.chat.ChannelActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="${applicationId}"
android:pathPrefix="/conversation"
android:scheme="rong" />
</intent-filter>
</activity>

When multiple contacts send multiple messages, their notifications are collapsed. When a user clicks this collapsed notification, the SDK fires an Intent to navigate to the channel list page.

Add the following intent-filter to the Activity that opens your conversation list. The Android Chat SDK does not ship a built-in conversation list UI Activity, so replace com.example.app.chat.ChannelListActivity with your own class.

xml
<activity
android:name="com.example.app.chat.ChannelListActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="${applicationId}"
android:pathPrefix="/conversationlist"
android:scheme="rong" />
</intent-filter>
</activity>

Customize notification click navigation

If the default navigation behavior does not meet your needs, extend PushMessageReceiver and override onNotificationMessageClicked.

Using PushMessageReceiver

You can customize the click event through the onNotificationMessageClicked method of PushMessageReceiver.

tip
  • Huawei push click events cannot be customized via PushMessageReceiver.
  • Due to Android 12 notification trampoline restrictions, if your app's targetVersion ≥ 31, launch the Activity directly inside the PushMessageReceiver callback. Do not dispatch the message through a broadcast or service before launching the Activity.
  1. Create CustomPushMessageReceiver extending the PushMessageReceiver class.

    Java
    class CustomPushMessageReceiver extends PushMessageReceiver {
    ...
    }

  2. Register the push broadcast receiver in the main project's AndroidManifest.xml. Replace the existing PushMessageReceiver registration entry.

    xml
      <receiver
    android:name="xxx.CustomPushMessageReceiver"
    android:exported="true">
    <intent-filter>
    <action android:name="io.rong.push.intent.MESSAGE_ARRIVED" />
    <action android:name="io.rong.push.intent.MESSAGE_CLICKED" />
    <action android:name="io.rong.push.intent.THIRD_PARTY_PUSH_STATE" />
    </intent-filter>
    </receiver>
  3. After registering the broadcast receiver, override the onNotificationMessageClicked method in your CustomPushMessageReceiver class. Intercept the event there and implement your navigation logic.

    Parameters

    ParameterTypeRequiredDescription
    contextContextYesContext object
    pushTypePushTypeYesPush type
    notificationMessagePushNotificationMessageYesPush message
    Java
    class CustomPushMessageReceiver extends PushMessageReceiver {
    @Override
    public boolean onNotificationMessageClicked(Context context, PushType pushType, PushNotificationMessage notificationMessage) {
    if (pushType == PushType.HUAWEI) {
    return true;
    } else if (pushType == PushType.GOOGLE_FCM) {
    return true;
    }
    return false;
    }
    }