[Huawei Developers] HMS Community:
Overview

In this article, I will create a Courier android application using Kotlin in which I will integrate HMS Core kits such as HMS Account and AuthService Kit.

We have integrated HMS Account and AuthService Kit in part-1 of this series. Kindly go through the link below-

// pls add part-1 link

App will make use of android MVVM clean architecture using Jetpack components such as DataBinding, AndroidViewModel, Observer, LiveData and much more.

In this article, we are going to implement DataBinding using Observable pattern.

Huawei Push Kit Introduction

Push Kit is a messaging service provided by Huawei for developers. Push Kit establishes a messaging channel from the cloud to devices. By integrating HUAWEI Push Kit, developers can send messages to apps on users’ devices in real time. Push Kit helps developers rapidly to reach the target audience. Push notification can be sent to everyone or to a specific person individually. For send notification, user’s token is required. You can send messages to a device group or specific user. Push Kit has two different notification type. Text only style (Shows longer text messages) and big picture style (Shows large text and image messages).

Prerequisite

Huawei Phone EMUI 3.0 or later.

Non-Huawei phones Android 4.4 or later (API level 19 or higher).

HMS Core APK 4.0.0.300 or later

Android Studio

AppGallery Account

App Gallery Integration process

Sign In and Create or Choose a project on AppGallery Connect portal.

Navigate to Project settings and download the configuration file.

Navigate to General Information, and then provide Data Storage location.

App Development

Add Required Dependencies:

Launch Android studio and create a new project. Once the project is ready.

Navigate to the Gradle scripts folder and open build.gradle (module: app).

In the same build.gradle file, add the lifecycle library to your dependencies. This library helps to connect the UI to a ViewModel and LiveData.

Add following dependency for HMS Push Kits

Navigate to the Gradle scripts folder and open build.gradle (project: app).

Code Implementation

Created following package model, push, viewmodel.

Model: In your primary folder, create a new package and name it model.

package com.hms.corrierapp.push

import android.util.Log
import com.huawei.hms.push.HmsMessageService
import com.huawei.hms.push.RemoteMessage
import java.util.*

class PushService : HmsMessageService() {

private val TAG = "PushService"

override fun onNewToken(token: String?) {
super.onNewToken(token)
Log.i(TAG, "Receive Token : $token");
}

override fun onMessageReceived(message: RemoteMessage?) {
super.onMessageReceived(message)

Log.i(
TAG, "getCollapseKey: " + message?.collapseKey
+ "\n getData: " + message?.data
+ "\n getFrom: " + message?.from
+ "\n getTo: " + message?.to
+ "\n getMessageId: " + message?.messageId
+ "\n getSendTime: " + message?.sentTime
+ "\n getMessageType: " + message?.messageType
+ "\n getTtl: " + message?.ttl
)

val notification: RemoteMessage.Notification = message!!.notification

Log.i(
TAG, "\n getImageUrl: " + notification.imageUrl
+ "\n getTitle: " + notification.title
+ "\n getTitleLocalizationKey: " + notification.titleLocalizationKey
+ "\n getTitleLocalizationArgs: " + Arrays.toString(notification.titleLocalizationArgs)
+ "\n getBody: " + notification.body
+ "\n getBodyLocalizationKey: " + notification.bodyLocalizationKey
+ "\n getBodyLocalizationArgs: " + Arrays.toString(notification.bodyLocalizationArgs)
+ "\n getIcon: " + notification.icon
+ "\n getSound: " + notification.sound
+ "\n getTag: " + notification.tag
+ "\n getColor: " + notification.color
+ "\n getClickAction: " + notification.clickAction
+ "\n getChannelId: " + notification.channelId
+ "\n getLink: " + notification.link
+ "\n getNotifyId: " + notification.notifyId
)
}
}

package com.hms.corrierapp.push

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class AccessTokenClient {

companion object{
private const val TIMEOUT: Long = 500000

fun getClient() : Retrofit {

val logginInterceptor = HttpLoggingInterceptor()
logginInterceptor.level = HttpLoggingInterceptor.Level.BODY

val client = OkHttpClient.Builder()
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(TIMEOUT,TimeUnit.MICROSECONDS)
.readTimeout(TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(logginInterceptor).build()

return Retrofit.Builder()
.baseUrl("https://oauth-login.cloud.huawei.com/").client(client)
.addConverterFactory(GsonConverterFactory.create()).build()
}

}
}

package com.hms.corrierapp.push

import retrofit2.Call
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.Headers
import retrofit2.http.POST

interface AccessTokenInterface {

@FormUrlEncoded
@Headers("Content-Type:application/x-www-form-urlencoded; charset=UTF-8")
@POST("oauth2/v3/token")
fun createAccessToken(
@Field("grant_type") grant_type : String,
@Field("client_secret") client_secret : String,
@Field("client_id") client_id : String) : Call<AccessTokenModel>
}

App Build Result

Tips and Tricks

Identity Kit displays the HUAWEI ID registration or sign-in page first. The user can use the functions provided by Identity Kit only after signing in using a registered HUAWEI ID.

Push Kit supports cross-region messaging, but the messaging performance may be affected. To minimize cross-region messaging, it is recommended that you deploy servers in regions where users gather.

Conclusion

In this article, we have learned how to integrate Huawei ID and Push Kit in Android application. After completely read this article user can easily implement Huawei ID and Client Side Push Notification in the Courier android application using Kotlin.

Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.

References

HMS Docs:

https://developer.huawei.com/consum.../HMSCore-Guides/introduction-0000001050048870

Document

developer.huawei.com

Push Kit Training Video:

https://developer.huawei.com/consumer/en/training/course/video/101583005582480166