Handle push notification clicks
This guide explains how the SDK handles push notification tap events by default and how your app can customize that behavior.
This guide only covers remote push notification click events. For local notification click handling, see the related documentation.
Implement the default navigation behavior
The SDK handles remote push notification tap events by default. When a user taps a notification, the SDK sends an implicit Intent. Configure an Intent filter in your app's AndroidManifest.xml to receive the Intent and complete the default navigation.
- Notification from a single sender: When the user taps one or more notifications from the same sender, the SDK navigates to the channel Activity.
- Collapsed notifications from multiple senders: When multiple senders send notifications, the notifications are collapsed. Tapping a collapsed notification navigates to the channel list Activity.
- We recommend navigating to the channel list page by default. Call the connect method before opening the channel list to ensure the latest messages are visible.
Navigate to the channel page
When the user taps notifications from a single sender, the SDK sends an Intent to navigate to the channel Activity.
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.
<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>
Navigate to the channel list page
When the user taps collapsed notifications from multiple senders, the SDK sends an Intent to navigate to the channel list Activity.
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.
<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 behavior
If the default navigation behavior does not meet your needs, extend PushMessageReceiver and override onNotificationMessageClicked.
Use PushMessageReceiver
You can customize click behavior through PushMessageReceiver's onNotificationMessageClicked method.
- Huawei Push click events cannot be customized through
PushMessageReceiver. - Due to Android 12 notification trampoline restrictions, if your app targets API level 31 or higher, start the Activity directly in the
PushMessageReceivercallback. Do not route through a broadcast or service before launching the Activity.
-
Create a
CustomPushMessageReceiverextendingPushMessageReceiver.- Kotlin
- Java
kotlinclass CustomPushMessageReceiver : PushMessageReceiver() {
// ...
}Javaclass CustomPushMessageReceiver extends PushMessageReceiver {
...
} -
Register the custom receiver in your app's
AndroidManifest.xml. Replace any existingPushMessageReceiverregistration.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> -
Override
onNotificationMessageClickedin yourCustomPushMessageReceiverclass. Intercept the event and implement your navigation logic.Parameters
Parameter Type Required Description context Context Yes The context object. pushType PushType Yes The push provider type. notificationMessage PushNotificationMessage Yes The push notification message. - Kotlin
- Java
kotlinclass CustomPushMessageReceiver : PushMessageReceiver() {
override fun onNotificationMessageClicked(context: Context, pushType: PushType, notificationMessage: PushNotificationMessage): Boolean {
return when (pushType) {
PushType.HUAWEI -> true
PushType.GOOGLE_FCM -> true
else -> false
}
}
}Javaclass 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;
}
}