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:
Register a developer account on HUAWEI Developers. Create a project, and add and configure an app in AppGallery Connect.
Create a project in Android Studio.
Use HMS Toolkit to call the APIs of Push Kit and debug your app.
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:
Configure the development environment for Push Kit in HMS Toolkit.
Use the SmartCoder tool of HMS Toolkit to integrate Push Kit.
Use the Message Test tool of HMS Toolkit to send a test message.
Use the Cloud Debugging tool of HMS Toolkit to debug the app on a remote or local real device.
Hardware Requirements
A computer (desktop or laptop) running Windows 7 or Windows 10
A Huawei phone (with a USB cable), which is used for debugging. (Or you can use a remote real device provided by HMS Toolkit for debugging.)
Software Requirements
JDK installation package
Android SDK package
Android Studio 3.X and 4.1.2 or earlier
HMS Core (APK) 4.X or later
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.
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.
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.
In Android Studio, go to HMS > Configuration Wizard.
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.
HMS Toolkit automatically checks the integration environment of the kit, including the general environment and the environment dedicated for the kit.
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
If you are using AndResGuard, add its trustlist to the obfuscation configuration file.
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: