CleverTap Baidu Push Integration

Learn how to integrate and send push notifications from Baidu.

China has the worldโ€™s largest number of smartphone users. However, Android users in China can only receive push notifications powered by Baidu Cloud Push. Baidu has an Android app marketplace with over 195M daily users and is the largest source of app downloads for Android in China. In the rest of the world, Googleโ€™s Firebase Cloud Messaging (FCM) is used to send push notifications to Android smartphones. For global businesses with apps that want to target users in China, it is critical for them to enable the Baidu push.

Follow to steps to enable push notifications for Baidu:

Register as a Baidu Developer

The first step to accessing the Baidu cloud push is to register as a developer.

Enter console and create an application

Log in to the Baidu account that has been registered as a developer on the homepage, and then click Get Started or My Console in the upper right corner to enter the push console.

Entering the push console will open the application list page. The application list page shows all the applications that you have created in the Baidu Developer Center. If you have configured applications in the old management console, the new management console can be used directly. For newly created or never-configured applications, you must perform an application configuration before you can send messages and other operations.

To create a new application, click the Create Application button on the application list page and then name your app.

135

Create Application Button

The application name can be a combination of Chinese, numbers, or English letters, with a maximum of 32 characters. Check that the application name complies with the relevant laws and regulations and complies with the cloud push developer service agreement.
After successful creation, you can configure the application immediately, or you can enter it later through the link in the application list.

623

Configure the Application

Application configuration - Android

If you are configuring an application for the first time, select the application platform (Android).

โ—๏ธ

Application Platform

The application platform cannot be changed after saving the configuration. Proceed with caution.

Android applications require the package name of the application. The package name requirements are as follows

  • Can only contain uppercase letters (A to Z), lowercase letters (a to z), numbers and underscores, Chinese, and can be separated by periods.
  • Each logo must start with a letter, underscore, or Chinese.
  • Contains at least two identifiers or at least one period.
  • Cannot contain Java reserved words.
  • There can be only one period between two identifiers.

Get the app's ApiKey / SecretKey

ApiKey is the application identifier. During the SDK invocation process, an application is uniquely identified. SecretKey is the Token when calling the API. It is used to verify the validity of the request. Always keep it confidential. ApiKey / SecretKey can be found on the application details page after the application is created.

Integrate Baidu Push SDK

Download the latest Android SDK archive and unpack, increase Baidu cloud push functionality in an existing or new project.

Import cloud push jar package and .SO file

Copy all files in the unzipped libs folder to the libs folder of your project. If there are no other .so files in your project, it is recommended to copy only the armeabi folder. If other .so files are used in your project, only copy the .so files in the corresponding directory. If your Android development environment is Android Studio, create a new directory named jniLibs in the src / main _directory of the project, and copy the files in the libs folder to the _jniLibs directory. As shown in the following image:

364

Import Push Jar Package

Configure the AndroidManifest file

Add permissions and declaration information in the AndroidManifest.xml file of the current project.

<!-- Push service ่ฟ่กŒ้œ€่ฆ็š„ๆƒ้™ -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<!-- ้€‚้…Android N็ณป็ปŸๅฟ…้œ€็š„ContentProviderๅ†™ๆƒ้™ๅฃฐๆ˜Ž -->
<uses-permission android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName" />

<permission
    android:name="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName"
    android:protectionLevel="signature" />

<!-- push service start -->
<!-- ็”จไบŽๆŽฅๆ”ถ็ณป็ปŸๆถˆๆฏไปฅไฟ่ฏPushServiceๆญฃๅธธ่ฟ่กŒ -->
<receiver android:name="com.baidu.android.pushservice.PushServiceReceiver"
    android:exported="false"
    android:process=":bdservice_v1" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <action android:name="com.baidu.android.pushservice.action.notification.SHOW" />
        <action android:name="android.intent.action.USER_PRESENT" />
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
    </intent-filter>
</receiver>

<!-- PushๆœๅŠกๆŽฅๆ”ถๅฎขๆˆท็ซฏๅ‘้€็š„ๅ„็ง่ฏทๆฑ‚ -->
<receiver android:name="com.baidu.android.pushservice.RegistrationReceiver"
    android:exported="false"
    android:process=":bdservice_v1" >
    <intent-filter>
        <action android:name="com.baidu.android.pushservice.action.METHOD" />
        <action android:name="com.baidu.android.pushservice.action.BIND_SYNC" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
    </intent-filter>
</receiver>

<!-- Baidu Push Service -->
<service android:name="com.baidu.android.pushservice.PushService"
    android:exported="false"
    android:process=":bdservice_v1">
    <intent-filter>
        <action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" />
    </intent-filter>
</service>

<!-- ๆๅ‡ๅฐ็ฑณๅ’Œ้ญ…ๆ—ๆ‰‹ๆœบไธŠ็š„ๆŽจ้€ๅˆฐ่พพ็އ -->
<service android:name="com.baidu.android.pushservice.CommandService"
    android:exported="false" />

<!-- ้€‚้…Android N็ณป็ปŸ็š„ContentProvider -->
<provider
    android:name="com.baidu.android.pushservice.PushInfoProvider"
    android:authorities="YourPackageName.bdpush"
    android:writePermission="baidu.push.permission.WRITE_PUSHINFOPROVIDER.YourPackageName"
    android:protectionLevel="signature"
    android:exported="false" />

<!-- ๅœจ็™พๅบฆๅผ€ๅ‘่€…ไธญๅฟƒๆŸฅ่ฏขๅบ”็”จ็š„API Key -->
<meta-data android:name="api_key" android:value="YOUR_API_KEY" />

<!-- ๐Ÿ”น CleverTap Push Notification Receiver -->
<receiver
    android:name="com.clevertap.android.sdk.pushnotification.CTPushNotificationReceiver"
    android:exported="false">
    <intent-filter>
        <action android:name="com.clevertap.PUSH_EVENT" />
    </intent-filter>
</receiver>

<!-- ๐Ÿ”น CleverTap Notification Channel Metadata (Optional) -->
<meta-data
    android:name="com.clevertap.notification_channel"
    android:value="YourChannelId" />

<!-- push็ป“ๆŸ -->

After configuring the Manifest file, replace YourPackageName your own package name and YOUR_API_KEY with your API key.

Launch cloud push

In the onCreate function of the main activity of the current project, add the following code:

PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,
                Utils.getMetaValue(YourMainActivity.this, "api_key"));
PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,
                      Utils.getMetaValue(this@YourMainActivity, "api_key"))

Custom callback class

Create a new class in the current project, right-click and select new > Class. Fill in the class name of the class that receives cloud push callback information and push notification arrival information, and click Finish to create the class file (in this example, PushTestReceiver is used).

303

Custom Call Back Class

Open the newly created class and inherit PushMessageReceiver. The class name becomes red. Move to the class name, click _Add unimplemented _methods, reload all callback functions, and print the corresponding information, as shown in the image:

862

Inherit PushMessageReceiver Class

In the AndroidManifest.xml file of the current project, add custom receiver information as follows:

<!-- ๆญคๅค„Receiverๅๅญ—ไฟฎๆ”นไธบๅฝ“ๅ‰ๅŒ…ๅ่ทฏๅพ„ -->
<receiver android:name="YourPackageName.PushTestReceiver">
    <intent-filter>
        <!-- ๆŽฅๆ”ถpushๆถˆๆฏ -->
        <action android:name="com.baidu.android.pushservice.action.MESSAGE" />
        <!-- ๆŽฅๆ”ถbindใ€setTags็ญ‰method็š„่ฟ”ๅ›ž็ป“ๆžœ -->
        <action android:name="com.baidu.android.pushservice.action.RECEIVE" />
        <!-- ๆŽฅๆ”ถ้€š็Ÿฅ็‚นๅ‡ปไบ‹ไปถ๏ผŒๅ’Œ้€š็Ÿฅ่‡ชๅฎšไน‰ๅ†…ๅฎน -->
        <action android:name="com.baidu.android.pushservice.action.notification.CLICK" />
    </intent-filter>
</receiver>

<!-- Recommended: Add CleverTap's Push Notification Receiver -->
<receiver
    android:name="com.clevertap.android.sdk.pushnotification.CTPushNotificationReceiver"
    android:exported="false">
    <intent-filter>
        <action android:name="com.clevertap.PUSH_EVENT" />
    </intent-filter>
</receiver>

Baidu Setup and send Channel ID to CleverTap

Baidu Push requires an App ID and Secret Key to generate a Channel ID for each device. This unique Channel ID identifier allows CleverTap to send push notifications to Baidu devices.

To ensure successful push delivery, you must:

  1. Enable Baidu Push Type in AndroidManifest.xml
  2. Pass the generated Baidu Channel ID to CleverTap using pushRegistrationToken().

Enable Baidu Push Type in AndroidManifest.xml

Add the following meta-data entry inside the <application> tag:

<meta-data
    android:name="CLEVERTAP_PROVIDER_2"
    android:value="bps,bps_token,com.clevertap.android.bps.BaiduPushProvider,com.baidu.android.pushservice.PushMessageReceiver" />

Pass Token to CleverTap

In the onBind method add the method to pass the token/Channel ID to CleverTap.

public class BaiduPushReceiver extends PushMessageReceiver {

    private static final PushType BAIDU_PUSH_TYPE = new PushType(
        "bps",
        "bps_token",
        "com.clevertap.android.bps.BaiduPushProvider",
        "com.baidu.android.pushservice.PushMessageReceiver"
    );

    /**
     * ่ฐƒ็”จPushManager.startWorkๅŽ๏ผŒsdkๅฐ†ๅฏนpush
     * serverๅ‘่ตท็ป‘ๅฎš่ฏทๆฑ‚๏ผŒ่ฟ™ไธช่ฟ‡็จ‹ๆ˜ฏๅผ‚ๆญฅ็š„ใ€‚็ป‘ๅฎš่ฏทๆฑ‚็š„็ป“ๆžœ้€š่ฟ‡onBind่ฟ”ๅ›žใ€‚ ๅฆ‚ๆžœๆ‚จ้œ€่ฆ็”จๅ•ๆ’ญๆŽจ้€๏ผŒ้œ€่ฆๆŠŠ่ฟ™้‡Œ่Žทๅ–็š„channel
     * idๅ’Œuser idไธŠไผ ๅˆฐๅบ”็”จserverไธญ๏ผŒๅ†่ฐƒ็”จserverๆŽฅๅฃ็”จchannel idๅ’Œuser id็ป™ๅ•ไธชๆ‰‹ๆœบๆˆ–่€…็”จๆˆทๆŽจ้€ใ€‚
     *
     * @param context   BroadcastReceiver็š„ๆ‰ง่กŒContext
     * @param errorCode ็ป‘ๅฎšๆŽฅๅฃ่ฟ”ๅ›žๅ€ผ๏ผŒ0 - ๆˆๅŠŸ
     * @param appid     ๅบ”็”จidใ€‚errorCode้ž0ๆ—ถไธบnull
     * @param userId    ๅบ”็”จuser idใ€‚errorCode้ž0ๆ—ถไธบnull
     * @param channelId ๅบ”็”จchannel idใ€‚errorCode้ž0ๆ—ถไธบnull
     * @param requestId ๅ‘ๆœๅŠก็ซฏๅ‘่ตท็š„่ฏทๆฑ‚idใ€‚ๅœจ่ฟฝๆŸฅ้—ฎ้ข˜ๆ—ถๆœ‰็”จ๏ผ›
     * @return none
     */

    @Override
    public void onBind(Context context, int errorCode, String appid,
                       String userId, String channelId, String requestId) {
        CleverTapAPI cleverTapAPI = CleverTapAPI.getDefaultInstance(context);
        if (cleverTapAPI != null && channelId != null) {
            cleverTapAPI.pushRegistrationToken(channelId, BAIDU_PUSH_TYPE, true);
        }
    }
}
class BaiduPushReceiver : PushMessageReceiver() {

    companion object {
        private val BAIDU_PUSH_TYPE = PushType(
            "bps",
            "bps_token",
            "com.clevertap.android.bps.BaiduPushProvider",
            "com.baidu.android.pushservice.PushMessageReceiver"
        )
    }

    /**
     * ่ฐƒ็”จPushManager.startWorkๅŽ๏ผŒsdkๅฐ†ๅฏนpush
     * serverๅ‘่ตท็ป‘ๅฎš่ฏทๆฑ‚๏ผŒ่ฟ™ไธช่ฟ‡็จ‹ๆ˜ฏๅผ‚ๆญฅ็š„ใ€‚็ป‘ๅฎš่ฏทๆฑ‚็š„็ป“ๆžœ้€š่ฟ‡onBind่ฟ”ๅ›žใ€‚
     * ๅฆ‚ๆžœๆ‚จ้œ€่ฆ็”จๅ•ๆ’ญๆŽจ้€๏ผŒ้œ€่ฆๆŠŠ่ฟ™้‡Œ่Žทๅ–็š„channel idๅ’Œuser idไธŠไผ ๅˆฐๅบ”็”จserverไธญ๏ผŒ
     * ๅ†่ฐƒ็”จserverๆŽฅๅฃ็”จchannel idๅ’Œuser id็ป™ๅ•ไธชๆ‰‹ๆœบๆˆ–่€…็”จๆˆทๆŽจ้€ใ€‚
     *
     * @param context BroadcastReceiver็š„ๆ‰ง่กŒContext
     * @param errorCode ็ป‘ๅฎšๆŽฅๅฃ่ฟ”ๅ›žๅ€ผ๏ผŒ0 - ๆˆๅŠŸ
     * @param appid ๅบ”็”จidใ€‚errorCode้ž0ๆ—ถไธบnull
     * @param userId ๅบ”็”จuser idใ€‚errorCode้ž0ๆ—ถไธบnull
     * @param channelId ๅบ”็”จchannel idใ€‚errorCode้ž0ๆ—ถไธบnull
     * @param requestId ๅ‘ๆœๅŠก็ซฏๅ‘่ตท็š„่ฏทๆฑ‚idใ€‚ๅœจ่ฟฝๆŸฅ้—ฎ้ข˜ๆ—ถๆœ‰็”จ๏ผ›
     */
    override fun onBind(
        context: Context,
        errorCode: Int,
        appid: String,
        userId: String,
        channelId: String,
        requestId: String
    ) {
        val cleverTapAPI = CleverTapAPI.getDefaultInstance(context)
        if (cleverTapAPI != null && channelId != null) {
            cleverTapAPI.pushRegistrationToken(channelId, BAIDU_PUSH_TYPE, true)
        }
    }
}

Receive Push Notifications from CleverTap

In the onMessageReceived method, write the following code for CleverTap to render Push Notification and raise Notification Viewed event.

/**
 * ๆŽฅๆ”ถ้€ไผ ๆถˆๆฏ็š„ๅ‡ฝๆ•ฐใ€‚
 *
 * @param context             ไธŠไธ‹ๆ–‡
 * @param message             ๆŽจ้€็š„ๆถˆๆฏ
 * @param customContentString ่‡ชๅฎšไน‰ๅ†…ๅฎน, ไธบ็ฉบๆˆ–่€…JSONๅญ—็ฌฆไธฒ
 */
@Override
public void onMessage(Context context, String message, String customContentString) {
    if (message == null || message.isEmpty()) {
        return; // ้˜ฒๆญข็ฉบๆถˆๆฏๅดฉๆบƒ
    }

    try {
        Bundle extras = com.clevertap.android.sdk.Utils.stringToBundle(message);
        if (extras != null) {
            CleverTapAPI cleverTapAPI = CleverTapAPI.getDefaultInstance(context);
            if (cleverTapAPI != null) {
                cleverTapAPI.getPushNotificationHandler().onMessageReceived(context, extras);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

/**
 * ๆŽฅๆ”ถ้€ไผ ๆถˆๆฏ็š„ๅ‡ฝๆ•ฐใ€‚
 *
 * @param context ไธŠไธ‹ๆ–‡
 * @param message ๆŽจ้€็š„ๆถˆๆฏ
 * @param customContentString ่‡ชๅฎšไน‰ๅ†…ๅฎน, ไธบ็ฉบๆˆ–่€…JSONๅญ—็ฌฆไธฒ
 */
override fun onMessage(context: Context, message: String?, customContentString: String?) {
    if (message.isNullOrEmpty()) return // ้˜ฒๆญข็ฉบๆถˆๆฏๅดฉๆบƒ

    try {
        val extras = com.clevertap.android.sdk.Utils.stringToBundle(message)
        extras?.let {
            CleverTapAPI.getDefaultInstance(context)?.pushNotificationHandler?.onMessageReceived(context, it)
        }
    } catch (e: JSONException) {
        e.printStackTrace()
    }
}

Ensure ProGuard Rules for Baidu SDK

If you are using ProGuard, R8, or a similar tool, add these rules to prevent code obfuscation issues:

-dontwarn com.baidu.**
-keep class com.baidu.** { *; }
-keepattributes *Annotation*

Update API Key / Secret Key in Settings Dashboard

1454

Update API Key / Secret Key in Settings Dashboard