Introduction
By integrating MessageReceiver or AliyunMessageIntentService, you can block notifications, receive messages, access extended fields in push payloads, or perform follow-up actions when a notification is opened or deleted.
MessageReceiver and AliyunMessageIntentService are two implementation options for handling push data. They offer identical extension interfaces. Choose only one approach.
Integrate MessageReceiver (choose one of two)
1 Implement a custom MessageReceiver
Inherit from com.alibaba.sdk.android.push.MessageReceiver.
2 Register your custom MessageReceiver
In your AndroidManifest.xml file (typically <project>/<app-module>/src/main/AndroidManifest.xml), register your custom MessageReceiver under the application node.
<!-- Message reception listener -->
<receiver android:name="com.alibaba.sdk.android.push.MessageReceiver"
android:exported="false"> <-- Replace this with your own receiver -->
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
</intent-filter>
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
</intent-filter>
<intent-filter>
<action android:name="com.alibaba.sdk.android.push.RECEIVE" />
</intent-filter>
</receiver>
3 Handle push notifications and push messages
Within MessageReceiver, handle push notifications, push messages, and notification click events. For specific APIs, see Push message types and Push notification types.
You can also customize push messages, customize push notification styles, and block push notifications.
Integrate AliyunMessageIntentService (choose one of two)
1 Implement a custom MessageIntentService
Inherit from com.alibaba.sdk.android.push.AliyunMessageIntentService.
2 Register your custom MessageIntentService
In your AndroidManifest.xml file (typically <project>/<app-module>/src/main/AndroidManifest.xml), register your custom MessageIntentService under the application node.
<service android:name="com.alibaba.sdk.android.push.AliyunMessageIntentService"
android:exported="false"> <-- Replace this with your own service -->
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_OPENED" />
</intent-filter>
<intent-filter>
<action android:name="com.alibaba.push2.action.NOTIFICATION_REMOVED" />
</intent-filter>
<intent-filter>
<action android:name="com.alibaba.sdk.android.push.RECEIVE" />
</intent-filter>
</service>
3 Enable MessageIntentService
By default, the SDK uses MessageReceiver to handle messages. To use MessageIntentService, call setPushIntentService on CloudPushService.
Use CloudPushService's setPushIntentService method.
Call setPushIntentService in your application’s Application#onCreate() method as soon as possible after initializing the push SDK. Do not delay this call in an Activity, Service, or other business callbacks. Otherwise, the SDK might already dispatch messages and notifications using the default MessageReceiver, preventing your custom MessageIntentService from working as expected.
val service = PushServiceFactory.getCloudPushService()
service.setPushIntentService(MyMessageIntentService::class.java) // Replace this with your own servicefinal CloudPushService service = PushServiceFactory.getCloudPushService();
service.setPushIntentService(MyMessageIntentService.class); // Replace this with your own service4 Handle push notifications and push messages
Within MessageIntentService, handle push notifications, push messages, and notification click events. For specific APIs, see Push message types and Push notification types.
You can also customize push messages, customize push notification styles, and block push notifications.
Push message types
Use this callback to receive messages pushed from the server. Messages do not display as pop-ups. Instead, the system invokes this callback method.
If you send message-type pushes via OpenAPI or the console, handle the message data in this callback. The SDK does not process message-type data by default.
onMessage
This callback runs when a push message arrives. Handle the push message here.
Interface definition
void onMessage(Context context, CPushMessage message)
Class
MessageReceiver/AliyunMessageIntentService
Parameter description
|
Parameter |
Type |
Description |
|
context |
Context |
Android component context. |
|
message |
CPushMessage |
Data structure of the pushed message. |
Push notification types
Use these callbacks to receive notifications pushed from the server. The SDK displays notifications by default and provides callbacks for receiving, clicking, and other notification events.
By default, the SDK handles push notification types and displays them as system notifications. You can also block notifications or customize their appearance by extending the relevant APIs.
showNotificationNow
Control whether to show the notification immediately. Use this method to decide whether the SDK handles the notification by default or you handle it yourself. If you return false, handle the push notification in onNotificationReceivedInApp.
Interface definition
boolean showNotificationNow(Context context, Map<String, String> map)
Class
MessageReceiver/AliyunMessageIntentService
Parameter description
|
Parameter |
Type |
Description |
|
context |
Context |
Android component context. |
|
map |
Map<String, String> |
Push message data. |
Return Description
|
Type |
Description |
|
boolean |
|
onNotification
This callback runs when a push notification arrives. After the SDK receives the notification, it calls this method so you can access and process notification parameters.
Interface definition
void onNotification(Context context, String title, String summary, Map<String, String> extraMap)
Class
MessageReceiver/AliyunMessageIntentService
Parameter description
|
Parameter |
Type |
Description |
|
context |
Context |
Android component context. |
|
title |
String |
Notification title. |
|
summary |
String |
Notification content. |
|
extraMap |
Map<String, String> |
Extra notification parameters, including these built-in system parameters:
|
onNotificationOpened
This callback runs when a user taps a notification.
Interface definition
void onNotificationOpened(Context context, String title, String summary, String extraMap)
Class
MessageReceiver/AliyunMessageIntentService
Parameter description
|
Parameter |
Type |
Description |
|
context |
Context |
Android component context. |
|
title |
String |
Notification title. |
|
summary |
String |
Notification content. |
|
extraMap |
String |
Extra notification parameters in JSON format, including these built-in system parameters:
|
onNotificationClickedWithNoAction
This callback runs when a user taps a notification that has no redirect logic (open=4). Supported in V2.3.2 and later.
Interface definition
void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap)
Class
MessageReceiver/AliyunMessageIntentService
Parameter description
|
Parameter |
Type |
Description |
|
context |
Context |
Android component context. |
|
title |
String |
Notification title. |
|
summary |
String |
Notification content. |
|
extraMap |
String |
Extra notification parameters in JSON format, including these built-in system parameters:
|
onNotificationRemoved
This callback runs when a user deletes a notification.
Interface definition
void onNotificationRemoved(Context context, String messageId)
Class
MessageReceiver/AliyunMessageIntentService
Parameter description
|
Parameter |
Type |
Required |
Description |
|
context |
Context |
Yes |
Android component context. |
|
messageId |
String |
Yes |
ID of the deleted notification. |
onNotificationReceivedInApp
This callback runs only when showNotificationNow returns false. In this case, onNotification is not called, and you must handle the notification logic yourself.
This callback runs when you create a custom notification style and configure the push to arrive in-app without displaying a notification pop-up.
Interface definition
void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl)
Class
MessageReceiver/AliyunMessageIntentService
Parameter description
|
Parameter |
Type |
Description |
|
context |
Context |
Android component context. |
|
title |
String |
Notification title. |
|
summary |
String |
Notification content. |
|
extraMap |
Map<String, String> |
Extra notification parameters. |
|
openType |
int |
Original notification open behavior: 1 = open app, 2 = open activity, 3 = open URL, 4 = no redirect logic. |
|
openActivity |
String |
Name of the activity to open. Valid only when the server sets openType=2. Null otherwise. |
|
openUrl |
String |
URL to open. Valid only when the server sets openType=3. Null otherwise. |