Overview

Push Kit is a messaging service provided for you to establish a cloud-to-device messaging channel. After integrating Push Kit, you can send messages to your apps on user devices in real time. This helps you maintain closer ties with users and increase user awareness of and engagement with your apps.

To use Push Kit, you need to perform the following operations:

What You Will Create

In this codelab, you will use a demo project created through HMS Toolkit to call the APIs of Push Kit. Through the demo project, you'll be able to implement the following functions:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Through Marketplace of Android Studio

Start Android Studio, go to File > Settings > Plugins > Marketplace, search for HMS Toolkit, and click Install. After the installation, restart Android Studio.

Through Local Installation Package

  1. Download the HMS Toolkit installation package.
  2. Start Android Studio, go to File > Settings > Plugins, click Install Plugin from Disk, and select the downloaded package to install. After the installation, restart Android Studio.

Before using the HMS Core capabilities, configure the development environment via Configuration Wizard.

Operations in AppGallery Connect

You need to create an app and configure it in AppGallery Connect.

  1. Sign in to AppGallery Connect with your HUAWEI ID. If you have no HUAWEI ID, register one by referring to Registration and Verification.
  2. Create a project and create an app under the project. Pay attention to the following parameter settings:
    • Platform: Select Android.
    • Package name: It must be the same as that of the app you are developing.
After the configuration, you do not need to perform the next step as prompted. Go to Configuration Wizard to proceed with subsequent operations.

Operations in Configuration Wizard

Before using HMS Core kits, you need to configure dependencies of the kits in Configuration Wizard, and HMS Toolkit will automatically check the configuration.

  1. In Android Studio, go to HMS > Configuration Wizard.
  2. After sign-in authorization, select the team name, project module, kit (up to 5 kits), and certificate type, click Generate to generate an SHA-256 certificate fingerprint, and click Next.
    The certificate types are described as follows:
    • Use your certificate: Use the certificate that you created to generate an SHA-256 certificate fingerprint. This method is used in app release scenarios.
    • Use Android debug certificate: Select the debugging certificate provided by Android to generate an SHA-256 certificate fingerprint. This method is used only in app debugging scenarios.
      After selecting this option, you can select an existing certificate file or create a certificate. To learn more about the certificate, please refer to Sign your app.

  3. HMS Toolkit automatically checks the integration environment of the kit, including the general environment and the environment dedicated for the kit.
  4. HMS Toolkit checks whether each configuration item is correct.
    • If an item fails to pass the check, click Link to manually modify the item as prompted. Then, click Retry to perform the check again.
    • If all the items are passed, you can integrate the kit.

Operations in AppGallery Connect

  1. If you are using AndResGuard, add its trustlist 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*"

Enable SmartCoder, search for push, and select Receive a Data Message in the search result, as shown in the following figure.

Development by Scenario (Receive a Data Message)

Click Receive a Data Message. You can view its details. Then click back.

On the Coding Assistant page displayed, drag and drop the Receive a Data Message card to the coding area.

Call the method of obtaining a token in the first activity after the app launch.

In the onCreate method of MainActivity, enter hmspush. The complete API code is displayed on the right, as shown in the following figure.

Press Enter. The coding is completed.

Other Functions

To implement other functions such as topic subscription, drag and drop the related cards or copy the related code. You can also manually implement them by referring to the development guide.

Now, you can use Cloud Debugging of HMS Toolkit to debug your app on a real device. For details, please refer to Cloud Debugging.

Using a Remote Real Device

Go to HMS > Cloud Debugging or click the icon shown in the following figure.

On the Select Remote Device page, you can select a desired device by Resolution, EMUI Version, Android Version, Series, and Others.

Select a device, and click to run the app on the remote real device.

Using a Local Real Device

Click to run the app on the local real device.

After running the demo project, ensure that the network of the device is connected. Then, a token can be automatically obtained and recorded in the logs. You can search for message push logs in the demo using the PushGetDataService character string.

On the toolbar of the Push Kit card, select Message Test to test whether the server can push messages to the device.

Click Get APP ID and APP SECRET. The tool automatically reads the app ID and app secret from AppGallery Connect.

Click Next. On the page that is displayed, set parameters as promoted, including the token.

Click Send. The device will receive the message.

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

For more information, please click the following links:
Push Kit Development GuideHMS Toolkit Operation Guide
To download the sample code, please click the button below:

Download

Code copied