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

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To integrate HUAWEI Push Kit, you must complete the following preparations:

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

Enabling HUAWEI Push Kit

For details, please refer to Enabling Services.

Setting a Data Storage Location

To use HUAWEI Push Kit, you must set a data storage location. For details, please refer to Setting a 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. If the data storage location is different from your server location, or is different from the location of a user that the app serves, messaging delay will increase. To reduce the delay caused by messaging across regions around the world, you are advised to set the data storage location based on the location of a user that the app serves. For example, if you are a developer in Latin America and most of your app users are in Latin America, you are advised to set your data storage location to Singapore. If you are a developer in the Chinese Mainland but most of your app users are in Europe, you are advised to set your data storage location to Germany.

Setting HUAWEI Push Kit Parameters

For details, please refer to Setting HUAWEI Push Kit Parameters.

Adding the AppGallery Connect Configuration File of Your App

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your app project and click the app that needs to integrate the HMS Core SDK.
  3. Go to Project Setting > General information. In the App information area, download the agconnect-services.json file.
  4. Copy the agconnect-services.json file to the app's root directory of your Android Studio project.

Adding Build Dependencies

Open the build.gradle file in the app directory.

Add build dependencies in the dependencies section.

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:' }

Synchronizing the Project

After completing the preceding configuration, click the Sync Now to synchronize the build.gradle file.

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 inheriting from the HmsMessageService class.

<service android:name=".MyPushService" android:exported="false"> <intent-filter> <action android:name="com.huawei.push.action.MESSAGING_EVENT" /> </intent-filter> </service>
  1. Open the obfuscation configuration file proguard-rules.pro of your Android Studio project.
  2. Add configurations to exclude the HMS Core 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 are using AndResGuard, add it to the allowlist in 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*"

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

Deploy TextView in MainActivity to display the token.

In MainActivity, code the method for updating the token in TextView.

public class MainActivity extends AppCompatActivity { private TextView tvToken; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvToken = findViewById(R.id.tv_log); MyReceiver receiver = new MyReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("com.huawei.codelabpush.ON_NEW_TOKEN"); MainActivity.this.registerReceiver(receiver,filter); } public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.huawei.codelabpush.ON_NEW_TOKEN".equals(intent.getAction())) { String token = intent.getStringExtra("token"); tvToken.setText(token); } } } }

Completing Service Code

Applying for a Token

Declare <meta-data> under application in the AndroidManifest.xml file.

<application <meta-data android:name="push_kit_auto_init_enabled" android:value="true" /> </application>

Receiving a Token

A token is received by the onNewToken method in thMyPushService**ce class.


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); sendTokenToDisplay(token); } private void sendTokenToDisplay(String token) { Intent intent = new Intent("com.huawei.push.codelab.ON_NEW_TOKEN"); intent.putExtra("token", token); sendBroadcast(intent); } }

The MyPushService class inherits from the HmsMessageService class where the onNewToken method is overridden. This method is used to receive the token.

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

After running the demo, ensure that your Huawei phone is connected to the network. The onNewToken method is called to return the token AFcSAHhhnxdrMCYBxth2QOG9IgY2VydAM61DTThqNux3KBC_hgzQQT*******. The token is recorded in the log. Search for PushDemoLog: to view all logs of the demo.

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 Message Sending. 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

Download the demo source code used in this codelab from the following address:

Download source code

Code copied