1. Introduction
Overview
Push Kit is a messaging service provided for you. It establishes a messaging channel from the cloud to
devices. By integrating Push Kit, you can send messages to your apps on users' devices in real time. This
helps you maintain closer ties with users and increases user awareness of and engagement with your apps. The
following figure shows the process of sending messages from the cloud to devices.
To use Push Kit, you need to perform the following operations:
- Register a developer account on HUAWEI Developers. Create and configure an app and enable Push Kit in AppGallery Connect.
- Create a project in Android Studio IDE and integrate the HMS Core SDK.
- Obtain a token of Push Kit, and call and debug Push Kit APIs.
What You Will Create
In this codelab, you will use the created demo project to call open APIs of Push Kit. Through the demo project, you will:
- Obtain a token of Push Kit.
- Use the token to send messages and receive messages from the Push Kit server.
What You Will Learn
In this codelab, you will learn how to:
- Create a project and an app in AppGallery Connect.
- Enable Push Kit.
- Integrate the HMS Core SDK.
- Send test messages from AppGallery Connect to your app.
2. What You Will Need
Hardware Requirements
- A computer (desktop or laptop) running Windows or Mac
- An Android phone, which is used to debug the app
Software Requirements
- JDK version: 1.8.211 or later
- Android Studio version: 3.X
or later
- minSdkVersion: 19 or later
- targetSdkVersion: 29 (recommended)
- compileSdkVersion: 29 (recommended)
- Gradle version: 4.6 or later (recommended)
- Test device: a Huawei phone running EMUI 8.0 or later
If you need to use multiple HMS Core kits, use the latest versions required for these kits.
Required Knowledge
Android app development basics
3. Integration Preparations
To integrate Push Kit, you must complete the following preparations:
- Register as a developer.
- Create a project and an app in AppGallery Connect.
- Generate and configure the signing certificate fingerprint.
- Set a data processing location.
- Enable Push Kit.
4. Integrating the HMS Core SDK
Adding the AppGallery Connect Configuration File of Your App
- Sign in to AppGallery Connect and click My projects.
- Find your app project and click the app that needs to integrate the HMS Core SDK.
- Go to Project settings > General information. In the App information area, download the agconnect-services.json file.
- Copy the agconnect-services.json file to the app directory of your
Android Studio project.
Configuring the Maven Repository Address for the HMS Core SDK
The procedure for configuring the Maven repository address in Android Studio is different for Gradle plugin earlier than 7.0, Gradle plugin 7.0, and Gradle plugin 7.1 or later. Click a relevant link below to find the configuration procedure for the specific Gradle plugin version.
Gradle plugin earlier than 7.0 | Gradle plugin 7.0 | Gradle plugin 7.1 or later |
Gradle plugin earlier than 7.0
a. Open the build.gradle file in the root directory of your Android Studio project.
b. Add the AppGallery Connect plugin and the Maven repository.
- Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.
- Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.
- If the agconnect-services.json file has been added to the app, go to buildscript > dependencies and add the AppGallery Connect plugin configuration.
buildscript { repositories { google() jcenter() // Configure the Maven repository address for the HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } dependencies { ... // Add the AppGallery Connect plugin configuration. You are advised to use the latest plugin version. classpath 'com.huawei.agconnect:agcp:1.6.0.300' } } allprojects { repositories { google() jcenter() // Configure the Maven repository address for the HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } }
Gradle plugin 7.0
a. Open the build.gradle file in the root directory of your Android Studio project.
b. Add the AppGallery Connect plugin and the Maven repository.
- Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.
- If the agconnect-services.json file has been added to the app, go to buildscript > dependencies and add the AppGallery Connect plugin configuration.
buildscript { repositories { google() jcenter() // Configure the Maven repository address for the HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } dependencies { ... // Add the AppGallery Connect plugin configuration. You are advised to use the latest plugin version. classpath 'com.huawei.agconnect:agcp:1.6.0.300' } }
c. Open the project-level settings.gradle file and configure the Maven repository address for the HMS Core SDK.
dependencyResolutionManagement { ... repositories { google() jcenter() // Configure the Maven repository address for the HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } }
Gradle plugin 7.1 or later
a. Open the build.gradle file in the root directory of your Android Studio project.
b. If the agconnect-services.json file has been added to the app, go to buildscript > dependencies and add the AppGallery Connect plugin configuration.
buildscript { dependencies { ... // Add the AppGallery Connect plugin configuration. You are advised to use the latest plugin version. classpath 'com.huawei.agconnect:agcp:1.6.0.300' } }
c. Open the project-level settings.gradle file and configure the Maven repository address for the HMS Core SDK.
pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() // Configure the Maven repository address for the HMS Core SDK. maven { url 'https://developer.huawei.com/repo/' } } } dependencyResolutionManagement { ... repositories { google() mavenCentral() // Configure the Maven repository address for the HMS Core SDK. maven { url 'https://developer.huawei.com/repo/' } } }
Adding Build Dependencies
- Open the build.gradle file in the app directory.
- Add a dependency in the
dependencies
block.
dependencies { // Add the following line. implementation 'com.huawei.hms:push:{version}' }
Add the AppGallery Connect plugin configuration.
- In versions earlier than Android Studio 4.0, add the following information under
apply plugin: ‘com.android.application' in the file header:
apply plugin: 'com.huawei.agconnect'
- In Android Studio 4.0 or later, add the plugin configuration in the
plugins
block:
plugins { // Add the following configuration: id 'com.huawei.agconnect' }
- In versions earlier than Android Studio 4.0, add the following information under
apply plugin: ‘com.android.application' in the file header:
Configuring the Signing Certificate Fingerprint
Copy the signing certificate generated in 3 Integration Preparations to the app directory of your project and configure the signature in the build.gradle file.
android {
signingConfigs {
config {
// Replace xxx with your signing certificate.
keyAlias 'xxx'
keyPassword 'xxxx'
storeFile file('xxx.jks')
storePassword 'xxxx'
}
}
buildTypes {
debug {
signingConfig signingConfigs.config
}
release {
signingConfig signingConfigs.config
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
Synchronizing the Project
After completing the preceding configuration, click Sync Now to synchronize the
build.gradle file.
If synced successfully is displayed, the synchronization is successful.
5. Configuring the Manifest File
You need to register service in application in the
AndroidManifest.xml file to receive data messages or obtain tokens.
service inherits from the HmsMessageService class and implements the methods in the class.
The following uses the service with the DemoHmsMessageService class name as an example. You
can customize the class name as needed.
<manifest ...>
...
<application ...>
<service android:name=".DemoHmsMessageService" android:exported="false">
<intent-filter>
<action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
...
</manifest>
6. Configuring Obfuscation Scripts
- Open the obfuscation configuration file proguard-rules.pro of your Android Studio project.
- Add configurations to exclude the SDK from obfuscation.
-ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.huawei.hianalytics.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;}
- 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*"
- (Optional) Configure the keep.xml file as follows to keep layout resources if you have
enabled R8 resource shrinking (with shrinkResources being set to true in
the project-level build.gradle file) and strict reference checks (with
shrinkMode being set to strict in the
res/raw/keep.xml file). Not keeping layout resources will lead to app rejection during
release to HUAWEI AppGallery.
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@layout/hms_download_progress,@drawable/screen_off" tools:shrinkMode="strict" />
7. Developing a Demo
The figure below shows the code and resource structure of the project. For details, please refer to
Sample Code.
Creating a Project
Create a project in Android Studio. For details, please refer to Creating an Android Studio Project.
Completing Service Code
Obtaining a Token
Add <meta-data> under application in the AndroidManifest.xml file.
<meta-data
android:name="push_kit_auto_init_enabled"
android:value="true" />
The Push SDK version must be 4.0.0.300 or later.
The Push SDK will obtain a token when the app is
launched. The app just receives the token.
name and value in
meta-data cannot be modified.
Receiving a Token
The MyPushService class inherits from the HmsMessageService class. In
MyPushService, the
onNewToken(String token, Bundle bundle) method is overridden to obtain a token.
If the version of the Push SDK you
integrated is earlier than 5.0.4.302, override the
onNewToken(String token) method.
Java
public class MyPushService extends HmsMessageService {
private static final String TAG = "PushDemoLog";
@Override
public void onNewToken(String token, Bundle bundle) {
super.onNewToken(token);
Log.i(TAG, "receive new token:" + token);
}
}
Kotlin
class MyPushService : HmsMessageService {
val TAG = "PushDemoLog"
override fun onNewToken(token: String?, bundle: Bundle?) {
Log.i(TAG, "have received refresh token:$token")
}
}
Other Functions
For details about other functions, such as subscribing to a topic, and sample code, please refer to Push Kit Development Guide.
Building, Loading, and Debugging the App
Connect your Huawei phone to a computer.
Method 1:
Click to build your app
and deploy it to your Huawei phone.
Method 2:
- Use Android Studio to generate the APK.
- 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
Obtaining a token
After running the demo, ensure that the phone is connected to the
network. Then a token can be automatically obtained and recorded in the logs. You can search for message push
logs in the demo using the PushDemoLog string.
8. Sending Messages
In
AppGallery Connect, you can edit a message and send it based on the token you have obtained. For details, please refer to
Sending a Test Message. Before sending the message, search for the app by package name. In this example, the package name of the
app is com.huawei.codelabpush.
After successful message sending, you will receive the
message on your device.
9. Congratulations
Well done. You have successfully completed this codelab and learned how to:
- Create a project and an app in AppGallery Connect.
- Enable Push Kit.
- Integrate the HMS Core SDK.
- Send test messages from AppGallery Connect to your app.
10. Reference
For more information, please click the following link:
Related document
To download the sample code, please click the following link: