Integrate a third-party native SDK

Updated at:

This topic explains the Mobile Push configurations required for manually integrating a native SDK from a third-party channel.

Important

This document applies to Android SDK 3.x and 4.x. Before you proceed, make sure you have initialized the main SDK by following the instructions in Integrate the Android SDK.

Overview

Instead of using the third-party channel SDKs provided by EMAS, you can manually integrate SDKs directly from third-party vendors.

This approach is intended for the following scenarios:

  • The third-party channel SDK provided by EMAS has a dependency conflict with another SDK in your application.

  • EMAS does not provide a specific version of a third-party channel SDK that you need.

Prerequisites

Step 1: Add the SDK to your application

In your module-level (app-level) Gradle file, typically <project>/<app-module>/build.gradle, add the SDK dependency to the dependencies block.

Important
dependencies {
    implementation 'com.aliyun.ams:alicloud-android-third-push:x.x.x'
}
Note

The Mobile Push SDK for third-party channels supports only Maven dependencies.

Step 2: Configure and use the SDK

1. Initialize the SDK

Follow the integration documentation from your chosen third-party vendor to initialize their SDK.

2. Report the third-party device ID

After the third-party channel initializes successfully, call ThirdPushManager.reportToken() to report the third-party device ID:

/**
* Take HUAWEI as an example.
*
* @param context The context object. We recommend passing the ApplicationContext.
* @param thirdTokenKeyword An identifier for the third-party channel type, for example, HUAWEI or FCM.
* @param token The device token or registration ID from the third-party service. The term varies by vendor; `token` is used generically.
* @param thirdSdkVersion The version number of the third-party's native push SDK, not the EMAS SDK version. Do not add a vendor prefix.
*/
ThirdPushManager.reportToken(context, ThirdPushReportKeyword.HUAWEI.thirdTokenKeyword, token, thirdSdkVersion);

3. Register the third-party data decoder

To process push data when a user taps a notification from a third-party channel, you must register the corresponding data decoder.

// For HUAWEI devices
ThirdPushManager.registerImpl(new HuaweiMsgParseImpl());
// For Honor devices
ThirdPushManager.registerImpl(new HonorParserImpl());
// For Xiaomi devices
ThirdPushManager.registerImpl(new XiaoMiMsgParseImpl());
// For vivo devices
VivoMsgParseImpl listener = new VivoMsgParseImpl();
listener.setContext(getApplicationContext());
ThirdPushManager.registerImpl(listener);
// For OPPO devices
ThirdPushManager.registerImpl(new OppoMsgParseImpl());
// For Meizu devices
ThirdPushManager.registerImpl(new MeizuMsgParseImpl());
Note

Supported data decoders: HuaweiMsgParseImpl, HonorParserImpl, XiaoMiMsgParseImpl, OppoMsgParseImpl, VivoMsgParseImpl, and MeizuMsgParseImpl.

4. Process third-party pass-through messages

When your application receives a pass-through message from a third-party channel, call ThirdPushManager.onPushMsg to pass the message content to the EMAS Mobile Push SDK for processing.

/**
* Take HUAWEI as an example.
*
* @param context The context object. We recommend passing the ApplicationContext.
* @param thirdMsgKeyword The identifier for the third-party channel.
* @param msgContent The message content from the third-party channel.
*/
ThirdPushManager.onPushMsg(context, ThirdPushReportKeyword.HUAWEI.thirdMsgKeyword, msgContent);

FCM message receiving configuration

When manually integrating the native FCM SDK, you must implement FirebaseMessagingService to report the token and process incoming messages. In the onMessageReceived callback, pass messages to the Mobile Push SDK. You must also declare this service in your AndroidManifest.xml file.

1. Implement FirebaseMessagingService

Create a class named MyFirebaseMessagingService that extends FirebaseMessagingService. In the onNewToken callback, report the token. In the onMessageReceived callback, read the payload field from the data object. If the payload is not empty, pass the raw content to ThirdPushManager.onPushMsg. Set FCM_SDK_VERSION to the version of the firebase-messaging library you are using.

package com.example.push;

import android.text.TextUtils;

import com.alibaba.sdk.android.push.register.ThirdPushManager;
import com.alibaba.sdk.android.push.register.ThirdPushManager.ThirdPushReportKeyword;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    // Replace with the version of the firebase-messaging library in your application.
    private static final String FCM_SDK_VERSION = "25.0.2";

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        ThirdPushManager.reportToken(getApplicationContext(),
                ThirdPushReportKeyword.FCM.thirdTokenKeyword,
                token, FCM_SDK_VERSION);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getData() == null) {
            return;
        }
        String payload = remoteMessage.getData().get("payload");
        if (!TextUtils.isEmpty(payload)) {
            ThirdPushManager.onPushMsg(getApplicationContext(),
                    ThirdPushReportKeyword.FCM.thirdMsgKeyword, payload);
        }
    }
}

If your application already has a class that extends FirebaseMessagingService, add the preceding logic to the corresponding callbacks in your existing service. Make sure to keep your original business logic.

2. Declare the service in AndroidManifest.xml

In your application's AndroidManifest.xml file, declare the service within the application node. Replace the value of android:name with the fully qualified name of your service class.

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application>
        <service
            android:name="com.example.push.MyFirebaseMessagingService"
            android:process="@string/aliyun_accs_target_process"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>
</manifest>