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 Baidu push.
Follow to steps to enable push notifications for Baidu:
Register as a Baidu Developer
The first step to access 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 enter 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 application configuration before you can send messages and other operations.
When you need to create a new application, click the Create Application button on the application list page:


Then name your app. The application name can be a combination of Chinese, numbers or English letters, with a maximum of 32 characters. Note 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.


Application configuration - Android
If you are configuring an application for the first time, you need to select the application platform (Android). After saving this configuration, the application platform cannot be changed, please proceed with caution.
Android applications need to configure 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 or 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. Please keep confidential. ApiKey / SecretKey can be found in 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, you only need to 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 below -


Configure the AndroidManifest file
In the AndroidManifest.xml file of the current project, add permissions and declaration information
<!-- 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.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- éé
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">
</permission>
<!-- push service start -->
<!-- ç¨äşćĽćśçłťçťćśćŻäťĽäżčŻPushServicećŁĺ¸¸čżčĄ -->
<receiver android:name="com.baidu.android.pushservice.PushServiceReceiver"
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声ćďźĺŻĺ¤§ĺ¤§ćéŤserviceĺć´ťçĺćśćŻĺ°čžžé庌 -->
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<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: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>
<service android:name="com.baidu.android.pushservice.PushService" android:exported="true"
android:process=":bdservice_v1" >
<intent-filter >
<action android:name="com.baidu.android.pushservice.action.PUSH_SERVICE" />
</intent-filter>
</service>
<!-- 4.4çćŹć°ĺ˘çCommandService声ćďźćĺĺ°çąłĺé
ćććşä¸çĺŽé
ć¨éĺ°čžžç -->
<service android:name="com.baidu.android.pushservice.CommandService"
android:exported="true" />
<!-- éé
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="true" />
<!-- ĺ¨çžĺşŚĺźĺč
ä¸ĺżćĽčŻ˘ĺşç¨çAPI Key -->
<meta-data android:name="api_key" android:value="YOUR_API_KEY" />
<!-- pushçťć -->
After configuring the Manifest file, please YourPackageName
replace it with 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([email protected], "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).


Open the newly created class and inherit PushMessageReceiver. At this time, the class name is red, move to the class name, click Add unimplemented methods, reload all callback functions, and print the corresponding information, as shown in the figure:


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>
Pass Token to CleverTap
In the onBind method add the method to pass the token/Channel ID to CleverTap
/**
* č°ç¨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) {
cleverTapAPI.pushBaiduRegistrationId(channelId,true);
}
}
/**
* č°ç¨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
*/
fun onBind(context:Context, errorCode:Int, appid:String,
userId:String, channelId:String, requestId:String) {
val cleverTapAPI = CleverTapAPI.getDefaultInstance(context)
if (cleverTapAPI != null)
{
cleverTapAPI.pushBaiduRegistrationId(channelId, 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) {
try {
Bundle extras = com.clevertap.android.sdk.Utils.stringToBundle(message);
CleverTapAPI.createNotification(context,extras);
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* ćĽćśéäź ćśćŻçĺ˝ć°ă
*
* @param context ä¸ä¸ć
* @param message ć¨éçćśćŻ
* @param customContentString čŞĺŽäšĺ
厚,为犺ćč
jsonĺ珌串
*/
fun onMessage(context:Context, message:String,
customContentString:String) {
try
{
val extras = com.clevertap.android.sdk.Utils.stringToBundle(message)
CleverTapAPI.createNotification(context, extras)
}
catch (e:JSONException) {
e.printStackTrace()
}
}
Update API Key / Secret Key in Settings Dashboard


Updated 5 months ago