For applications that are integrated with a third-party push channel and run on that vendor's device, the server-side sends messages through the third-party channel by default. For all other applications, messages are sent through the self-built channel.
When the self-built channel receives a message, the push SDK automatically sends a notification. When a user clicks the notification, a web page opens automatically.
ImportantThe push SDK uses notification IDs that start from 10000. Ensure that other notification IDs you use do not conflict with this range.
To redirect to a page within the application, see Redirect to an in-app page.
To process received messages, see Customize message handling.
When a third-party channel receives a message, the mobile OS automatically sends a notification. Neither the push SDK nor developers can control this process. The push SDK receives the message only after the user clicks the notification, at which point a web page opens automatically.
To redirect to a page within the application, see Redirect to an in-app page.
To handle the redirection after a notification click, see Customize message handling.
Prerequisites
The
MPPushMsgServiceAdaptermethods described in this topic apply only to baseline 10.1.68.32 and later. If you use a baseline version earlier than 10.1.68.32, you must upgrade your baseline. For more information, see the mPaaS upgrade guide.The
AliPushRcvServicemethod from older versions can still be used. To view the documentation for these versions, click here to download.
Redirect to an in-app page
To redirect to a specific page in your application, enter a custom deep link, such as mpaas://navigate, in the redirect address of the push message. You also need to set up a routing Activity in your application to receive the deep link and dispatch it to other pages.
Add a corresponding intent-filter for the routing Activity in the AndroidManifest.xml file. For example:
<activity android:name=".push.LauncherActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="mpaas" />
</intent-filter>
</activity>Retrieve the URI and message in the routing Activity:
Uri uri = intent.getData();
MPPushMsg msg = intent.getParcelableExtra("mp_push_msg");Customize message handling
To process messages, override the onMessageReceive and onChannelMessageClick methods of MPPushMsgServiceAdapter:
public class MyPushMsgService extends MPPushMsgServiceAdapter {
/**
* Callback for messages received from the self-built channel.
*
* @param msg The received message.
* @return A boolean that indicates whether the message was processed.
* - true: The SDK does not process the message. You must handle the message, including sending a notification and handling the subsequent click.
* - false: The SDK automatically sends a notification and handles the click action.
*/
@Override
protected boolean onMessageReceive(MPPushMsg msg) {
Log.d("Message received from self-built channel: " + msg.toString());
// Process the message, for example, send a custom notification.
return true;
}
/**
* Callback for clicks on notifications from a third-party channel.
*
* @param msg The received message.
* @return A boolean that indicates whether the click was processed.
* - true: The SDK does not handle the click. You must handle the redirect action.
* - false: The SDK automatically handles the redirect action.
*/
@Override
protected boolean onChannelMessageClick(MPPushMsg msg) {
Log.d("Message from third-party channel was clicked: " + msg.toString());
// Handle the logic after the click.
return true;
}
}The MPPushMsg object encapsulates all message parameters:
String id = msg.getId(); // Message ID
boolean isSilent = msg.isSilent(); // Indicates if it is a silent message
String title = msg.getTitle(); // Message title
String content = msg.getContent(); // Message content
String action = msg.getAction(); // Redirect type. 0: URL. 1: Custom DeepLink.
String url = msg.getUrl(); // Redirect address, which can be a URL or a DeepLink.
int pushStyle = msg.getPushStyle(); // Message type. 0: Normal message. 1: Large text message. 2: Rich media message.
String iconUrl = msg.getIconUrl(); // Small icon for a rich media message.
String imageUrl = msg.getImageUrl(); // Image for a rich media message.
String customId = msg.getCustomId(); // Custom message ID.
String params = msg.getParams(); // Extended parameters.After processing a message, you must also report message instrumentation data. Otherwise, the push usage analysis in the console cannot collect the correct data:
MPPush.reportPushOpen(msg); // Reports that the message was opened.
MPPush.reportPushIgnored(msg); // Reports that the message was ignored.For messages from the self-built channel:
Silent messages do not need to be reported.
For non-silent messages, report when they are opened or ignored. Use the
setContentIntentandsetDeleteIntentmethods ofNotification.Builderor other valid methods to listen for these user actions.
Messages from third-party channels do not need to be reported.