Baidu Integration

Braze is capable of sending push notifications to Android devices using Baidu Cloud Push. Our Baidu push sample app, China Push Sample, provides a full implementation example.

Using Baidu Cloud Push does not require you to distribute your apps via the Baidu App Store.

Step 1: Create a Baidu Account

  • Visit the Baidu Portal. You will see a homepage similar to the following. Click 登录 (Log In) among the menu options on the top right to bring up a dialog that will allow you to log in or create a new account.

Baidu Portal

  • To create a new account, in the log-in dialog, click 立即注册 (New Account) directly to the bottom right of the large blue 登录 (Log In) button.

Baidu Login Dialog

  • Enter an email address, password, and captcha in the account creation page. Accept the license agreement and click 注册 (Create Account) to register.

Baidu Sign-up Page

  • You will recieve an email from Baidu. Follow the verification link. Make sure to follow the verification link within 48 hours.

Baidu Verification Email

  • On the following page, you will complete an SMS verification. Choose your country code (美国 in the example image denotes the United States). Enter your phone number and click 发送验证短信 (Send SMS). You should receive a text message containing a six-digit number, which you will enter in the 验证码 (Verification Code). Make sure to enter your verification code within 30 minutes. Click 提交 (Submit) to submit. Congratulations! You now have a Baidu account.

Baidu Verification SMS

Step 2: Register as a Baidu Developer

  • Visit the Baidu Developer Portal. Open the dropdown menu in the top right of the screen. Choose 注册开发者 (Create New Developer Account) to begin registration.

Baidu Developer Portal

  • On the registration page, choose your account type (个人 for personal, 公司 for business) and developer type (Developer is preselected and correct for most cases). Enter your name, a bio, and phone number with country code in parenthesis (For example, (1)xxxxxxxxxx). Click 发送验证码 (Send Verification Code) and enter the verification code in the following line. The next two fields, developer website and developer logo, are optional. Accept the license agreement and click 提交 (Submit) to submit. Congratulations! You now have a Baidu developer account.

Baidu Developer Registration

Step 3: Register your Application with Baidu

Baidu Project Portal

  • On the following page, enter your application name. The following two checkboxes are to activate additional Baidu services. In most cases, these should be left blank.

Baidu App Name

  • Upon setting up your application, you will be taken to a console which displays information about your app, including the API Key. Click on the cloud push link in the menu on the left hand side of the console. On the following page, click 推送设置 (Set Up Push).

Baidu App Console

Baidu Continue

  • On the following page, enter your app package name (e.g. com.braze.sample) and specify whether to cache messages, and if so, how long. This indicates to Baidu how long to continue to attempt to send messages to offline users. Click 保存设置 (Save Settings) to save.

Baidu Configure Cloud

Step 4: Add Baidu to your Application

Baidu SDK Portal

  • Inside the SDK, you will find the push service jar and platform specific native libraries. Integrate these into your project. There is also a Baidu demo push application which can be used as a reference along with our sample Baidu push application. Make sure your app targets the highest SDK version currently supported by Baidu. This documentation is current for Baidu Cloud Push Android SDK version 4.6.2.38.

  • Add the following required Baidu permissions to your application’s AndroidManifest.xml. See our sample app’s AndroidManifest.xml for a sample implementation.

    1
    2
    3
    4
    5
    6
    7
    8
    
      <uses-permission android:name="android.permission.READ_PHONE_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" />
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    
  • Baidu’s library contains broadcast receivers which handle incoming push messages. Declare the internal Baidu receivers in your application’s AndroidManifest.xml inside the <application> element.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
    <!-- 用于接收系统消息以保证 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 android:name="com.baidu.android.pushservice.action.media.CLICK"/>
          </intent-filter>
        </receiver>
        <!-- Push 服务接收客户端发送的各种请求-->
        <!-- 注意:RegistrationReceiver 在 2.1.1 及之前版本有拼写失误,为 RegistratonReceiver ,用 新版本 SDK 时请更改为如下代码-->
        <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>
        <!-- Push 服务 -->
        <!-- 注意:在 4.0 (包含)之后的版本需加上如下所示的 intent-filter action -->
        <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>
    
  • You will also need to create a broadcast receiver which listens for incoming push messages and notifications. Declare your own receiver in your application’s AndroidManifest.xml, inside the <application> element. This receiver will need to extend com.baidu.android.pushservice.PushMessageReceiver and implement methods that receive event updates from the Baidu push service. See our sample app’s receiver for a sample implementation.

    1
    2
    3
    4
    5
    6
    7
    
        <receiver android:name=".MyPushMessageReceiver">
          <intent-filter>
            <action android:name="com.baidu.android.pushservice.action.MESSAGE"/>
            <action android:name="com.baidu.android.pushservice.action.RECEIVE"/>
            <action android:name="com.baidu.android.pushservice.action.notification.CLICK"/>
          </intent-filter>
        </receiver>
    
  • In your main activity’s onCreate() method, add the following line, which will register your application with Baidu and begin listening for incoming push messages. Make sure to replace “Your-API-Key” with your project’s Baidu API Key.

    1
    
    PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY, "Your-API-Key");
    
  • Finally, you will need to register your users with Braze. In the onBind() method of the Baidu broadcast receiver that you created in part 5, send the channelId to Braze using Appboy.registerAppboyPushMessages(channelId). See our sample app’s main activity for a sample implementation.

    1
    
    Appboy.getInstance(context).registerAppboyPushMessages(channelId);
    

Step 5: Registering Push Opens

  • Baidu supports sending extra key-value pairs with push messages in JSON format. Your broadcast receiver’s public void onNotificationClicked(Context context, String title, String description, String customContentString) method will be called whenever a user clicks an incoming push message. The parameter customContentString contains the extras in JSON format. All messages from Braze will contain the following two key-value pairs:

    1
    2
    3
    4
    
    {
      "source": "Appboy",
      "cid": "your-campaign-Id"
    }
    
  • Whenever onNotificationClicked is called your Baidu receiver, your receiver should send an Intent to your application containing customContentString. Your application will log the click to Braze using the customContentString. See our sample app’s main activity for a sample implementation.

  • The following sample code passes customContentString to Braze and logs a click.

    1
    2
    
    String customContentString = intent.getStringExtra(ChinaPushMessageReceiver.NOTIFICATION_CLICKED_KEY);
    AppboyNotificationUtils.logBaiduNotificationClick(mApplicationContext, customContentString);
    

Step 6: Extras

  • Aside from reserved keys used by Braze, the parameter customContentString will also contain all user defined custom key-value pairs. To extract your key-value pairs, wrap customContentString in a JSONObject and retrieve your extras.

    1
    2
    3
    4
    5
    6
    
    try {
        JSONObject myExtras = new JSONObject(customContentString);
        String myValue = myExtras.optString("my_key", null);
      } catch (Exception e) {
        Log.e(TAG, String.format("Caught an exception processing customContentString: %s", customContentString), e);
      }
    

Step 7: Set Up Baidu Keys

You need to input your Baidu API Key and Baidu Secret Key into the Braze dashboard. Both keys are available from the [Baidu application console][52].

On the “App Settings Page” (where your API keys are located), select your Android China app. Enter your Baidu API Key and Baidu Secret Key in their respective fields in the Push Notifications section.

APIKey

Resources

WAS THIS PAGE HELPFUL?