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

  1. Open the build.gradle file in the root directory of your Android Studio project.
  2. 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. classpath 'com.huawei.agconnect:agcp:1.4.2.300' } } allprojects { repositories { google() jcenter() // Configure the Maven repository address for the HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } }

      In Gradle 7.0 or later, configuration under allprojects > repositories is migrated to the project-level settings.gradle file.
      The following is a configuration example of the settings.gradle file:

      dependencyResolutionManagement { ... repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } }

Adding Build Dependencies

  1. Open the build.gradle file in the app directory.
  2. Add build dependencies in the dependencies block.
    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:5.1.1.301' }
  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.
In Android 11, the way for an app to query other apps on the user device and interact with them has been changed. If targetSdkVersion is 30 or later for your app, add the queries element in the manifest element in AndroidManifest.xml to allow your app to access HMS Core (APK).

<manifest ...> ... <application ...> <service android:name=".DemoHmsMessageService" android:exported="false"> <intent-filter> <action android:name="com.huawei.push.action.MESSAGING_EVENT"/> </intent-filter> </service> </application> ... <queries> <intent> <action android:name="com.huawei.hms.core.aidlservice" /> </intent> </queries> ... </manifest>