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:

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:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Required Knowledge

Android app development basics

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

For details, please refer to Configuring App Information in AppGallery Connect.

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 settings > General information. In the App information area, download the agconnect-services.json file.
  4. 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

Adding Build Dependencies

  1. Open the build.gradle file in the app directory.
  2. Add a dependency in the dependencies block.
    dependencies { // Add the following line. implementation 'com.huawei.hms:push:{version}' }
  3. 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' }

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.

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>
  1. Open the obfuscation configuration file proguard-rules.pro of your Android Studio project.
  2. 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.**{*;}
  3. 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*"
  4. (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" />

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:

  1. Use Android Studio to generate 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

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.

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.

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

For more information, please click the following link:

Related document
To download the sample code, please click the following link:

Download

Code copied