DEV Community

HMS Community
HMS Community

Posted on

Expert: Courier App MVVM Jetpack (HMS Push Kit) in Android using Kotlin- Part-2

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
1.Huawei Phone EMUI 3.0 or later.
2.Non-Huawei phones Android 4.4 or later (API level 19 or higher).
3.HMS Core APK 4.0.0.300 or later
4.Android Studio
5.AppGallery Account
App Gallery Integration process
1.Sign In and Create or Choose a project on AppGallery Connect portal.
2.Navigate to Project settings and download the configuration file.
3.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

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.hms.corrierapp.databinding.ActivitySendTrackBinding
import com.hms.corrierapp.viewmodel.OrderViewModelFactory
import com.hms.corrierapp.viewmodel.TrackViewModel


class TrackActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySendTrackBinding
    private lateinit var viewModel: TrackViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_send_track)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_send_track)
        val factory = OrderViewModelFactory()
        viewModel = ViewModelProviders.of(this, factory).get(TrackViewModel::class.java)
        binding.lifecycleOwner = this

        viewModel.initNotification(this).observe(this, Observer {
            Toast.makeText(this, "Sent", Toast.LENGTH_SHORT).show();
        })

    }

}
Enter fullscreen mode Exit fullscreen mode

Push Service:

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
        )
    }
}
Enter fullscreen mode Exit fullscreen mode

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/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050048870
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-introduction-0000001050040060
Push Kit Training Video:
https://developer.huawei.com/consumer/en/training/course/video/101583005582480166

Top comments (0)