HUAWEI Push Kit is a messaging service provided by Huawei for developers. It establishes a messaging channel from the cloud to devices. By integrating HUAWEI Push Kit, developers can send messages to apps on users' devices in real time. This helps developers maintain closer ties with users and increases user awareness and engagement. The following figure shows the process of sending messages from the cloud to a device.

  1. App servers call APIs to send messages to HUAWEI Push Kit.
  2. HUAWEI Push Kit forwards the messages to devices with corresponding apps installed.

You need to perform the following operations:

What You Will Create

In this codelab, you will use the demo project that has been created for you to call HUAWEI Push Kit APIs. Through the demo project, you will:

What You Will Learn

Hardware Requirements

Software Requirements

To integrate HUAWEI HMS Core services, you must complete the following preparations:

For details, please refer to Preparations for Integrating HUAWEI HMS Core.

Select the parent product name of the app for which HUAWEI Push Kit is to be enabled. The app information page is then displayed.

Enabling the Service

Go to Develop > Overview, click the settings icon, and choose Manage APIs.

Enable HUAWEI Push Kit.

Setting the Data Storage Location

To use HUAWEI Push Kit, you must set the data storage location. For details, please refer to Setting Data Storage Location. If the data storage location is not set, the following functions will be unavailable: topic-based messaging, messaging through the WebPush agent, and messaging to iOS apps.

Setting HUAWEI Push Kit Parameters

Click the Develop tab and go to Growing > Push from the navigation bar on the left.

Click Enable to change the status of HUAWEI Push Kit to Enabled.

You have now successfully enabled HUAWEI Push Kit for your app.

Adding the AppGallery Connect Configuration File of Your App

Download the agconnect-services.json file.

Move the downloaded agconnect-services.json file to the app directory of your Android Studio project.

Adding Build Dependencies

Open the build.gradle file in the app directory.

Add all the following build dependencies to dependencies:

dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' // Add the following line implementation 'com.huawei.hms:push:' }

Configuring Obfuscation Scripts

  1. Open the obfuscation configuration file proguard-rules.pro of your Android Studio project.
  2. Add configurations to exclude HUAWEI Push Kit SDK from obfuscation.
    -ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.hianalytics.android.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;}
  3. If you have used AndResGuard, add the AndResGuard whitelist to the obfuscation configuration file.
    "R.string.agc*", "R.string.hms*", "R.string.connect_server_fail_prompt_toast", "R.string.getting_message_fail_prompt_toast", "R.string.no_available_network_prompt_toast", "R.string.third_app_*", "R.string.upsdk_*", "R.layout.hms*", "R.layout.upsdk_*", "R.drawable.upsdk*", "R.color.upsdk*", "R.dimen.upsdk*", "R.style.upsdk*"

Click Sync Now to synchronize the project.

If "synced successfully" is displayed, the synchronization is successful.

Declare service under application in the AndroidManifest.xml file so that devices can receive the push token and messages sent by the HUAWEI Push Kit server. Define the MyPushService class by extending the HmsMessageService class.

<service android:name=".MyPushService" android:exported="false"> <intent-filter> <action android:name="com.huawei.push.action.MESSAGING_EVENT" /> </intent-filter> </service>

The following figure shows the overall project code and resource structure.

Creating a Project

Create a project in Android Studio. For details, please refer to the Creating an Android Studio Project.

Creating a Widget and Writing a Listening Event

Deploy buttons and text boxes in MainActivity for calling HUAWEI Push Kit APIs in visualized mode.

In MainActivity, set a listener to listen for Button events and the method of updating text in TextView.

btnToken = findViewById(R.id.btn_get_token); btnToken.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getToken(); } });
private void showLog(final String log) { runOnUiThread(new Runnable() { @Override public void run() { View tvView = findViewById(R.id.tv_log); if (tvView instanceof TextView) { ((TextView) tvView).setText(log); Toast.makeText(MainActivity.this, pushtoken, Toast.LENGTH_SHORT).show(); } } }); }

Completing Service Code

Applying for a Token


/** * Obtain a token. */ private void getToken() { Log.i(TAG, "get token: begin"); // get token new Thread() { @Override public void run() { try { String appId = AGConnectServicesConfig.fromContext(MainActivity.this).getString("client/app_id"); pushtoken = HmsInstanceId.getInstance(MainActivity.this).getToken(appId, "HCM"); if(!TextUtils.isEmpty(pushtoken)) { Log.i(TAG, "get token:" + pushtoken); showLog(pushtoken); } } catch (Exception e) { Log.i(TAG,"getToken failed, " + e); } } }.start(); }

Receiving a Token

A token is received in the following scenarios:

  1. If the EMUI version is 10.0 or later on a Huawei device, a token is returned through the HmsInstanceId.getInstance.getToken method described in the step of applying for a token.
  2. If the EMUI version is earlier than 10.0 on a Huawei device, after the HmsInstanceId.getInstance.getToken method is called, the onNewToken method in the MyPushService class is called to obtain a token.


public class MyPushService extends HmsMessageService { private static final String TAG = "PushDemoLog"; @Override public void onNewToken(String token) { super.onNewToken(token); Log.i(TAG, "receive token:" + token); } }

The MyPushService class extends the HmsMessageService class to override the onNewToken method in the HmsMessageService class. The MyPushService class is used to:

  1. Return the token applied for using a Huawei device where the EMUI version is earlier than 10.0.
  2. Receive an updated token from the HUAWEI Push Kit server.
  3. Return a token that is successfully applied for upon retry.

Other Functions

For details about other functions of HUAWEI Push Kit, such as subscribing to a topic, and sample code, please refer to the HUAWEI Push Kit Development Guide.

Building, Loading, and Debugging the App

Connect your Huawei phone to a computer.
Method 1:
Click to start compilation and building, and install the APK to your Huawei phone.

Method 2:

  1. Use Android Studio to package the APK.
  2. Use the ADB tool to install the APK to your Huawei phone for debugging.
    adb install D:\WorkSpace\CodeLab\push_demo1\app\release\app-release.apk
  3. After running the demo, ensure that your Huawei phone is connected to the network. Click getToken to obtain the token. The token is recorded in the log. Search for PushDemoLog: to view all logs of the demo.

  4. If your Huawei phone runs EMUI 10.0 or later, the getToken API of the HMS Core Push SDK 4.0.0 directly returns the token ADHpIj5s3j6jxaYJUW7miEx6vY5TtUgb8_v1IXIbvf5g4e0Nb4ztnJ7******* in the return value.
  5. If your Huawei phone runs EMUI earlier than 10.0 or a cached token request is sent again, the onNewToken method is called to return the token AFcSAHhhnxdrMCYBxth2QOG9IgY2VydAM61DTThqNux3KBC_hgzQQT*******.

On the HUAWEI Push Kit console in AppGallery Connect, you can edit messages and send them to devices that have obtained tokens from the HUAWEI Push Kit server. For details, please refer to Sending Messages from the HUAWEI Push Kit Console. Before sending notification messages, search for the app by app name. In this example, the APK name of the app is com.huawei.codelabpush.

After successful message sending, you will receive the notification messages on your phone.

Well done. You have successfully completed this codelab and learned how to:

For more information, please click the following link:

Related documents

You can download the sample code of HUAWEI Push Kit to experience the development of HUAWEI Push Kit.

Download source code

Code copied