[Huawei Developers] Freemind R:
Notifications play a vital role in your app success. They give you a platform to reach out to your users, interact with them, give them updates, update the app data and much more.

While notification is an important part of the whole app development process, it somehow is typical to integrate as compared to the normal android apis. The reason? Well, while normal apis need configuration inside the app only, notifications need more than that — outside configuration like handling user tokens, and sending notifications using those tokens.

The main goal of this app is to be a sample of how to build an high quality Android application that uses the Architecture components, MVVM, RxJava,HMS PUSH Kit,HMS Analytics Kit etc. in Kotlin

What is Kotlin? The Java alternative explained

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429132749.31231617055526078112134276885293:50510506024149:2800:704759DD1C43C54E811FFC137AC5B3A9D06CD4AEDF464CB57EB05324A5F50CC2.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429132749.31231617055526078112134276885293:50510506024149:2800:704759DD1C43C54E811FFC137AC5B3A9D06CD4AEDF464CB57EB05324A5F50CC2.png[/IMG]

Kotlin is a general purpose, free, open source, statically typed “pragmatic” programming language initially designed for the JVM (Java Virtual Machine) and Android that combines object-oriented and functional programming features

Safety features in Kotlin

· Speaking of avoiding common errors, Kotlin was designed to eliminate the danger of null pointer references and streamline the handling of null values.

· It does this by making a null illegal for standard types, adding nullable types, and implementing shortcut notations to handle tests for null.

· To avoid the verbose grammar normally needed for null testing, Kotlin introduces a safe call, written ?.. For example, b?.length returns b.length if b is not null, and null otherwise. The type of this expression is Int?.

RxJava:

· RxJava has become the single most important skill for Android development.

· Most of you must have worked with it in some form, either in your own codebase or through other third party libraries, like Fast Android Networking and Retrofit

How does this Reactive Stream implement?

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429133537.07713700918280665279609070185282:50510506024149:2800:7C877B577CDF25E75A36BF4290E866592773CA5C9FAF7172F4A0266569253510.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429133537.07713700918280665279609070185282:50510506024149:2800:7C877B577CDF25E75A36BF4290E866592773CA5C9FAF7172F4A0266569253510.png[/IMG]

· Publisher: These interfaces(APIs) define the structure of RxJava.

interface Publisher<T> {
fun subscribe(s: Subscriber<in T?>?)
}

· Subscriber: A Subscriber exposes the following callback methods.

interface Subscriber<T> {
fun onSubscribe(s: Subscription?)
fun onNext(t: T)
fun onError(t: Throwable?)
fun onComplete()
}

· Subscription: When the Subscriber becomes ready to start handling events, it signals the Publisher through its Subscription

interface Subscription {
fun request(n: Long)
fun cancel()
}

Publishers

Let’s see how publishers are structured in RxJava. RxJava provides following types of event publishers:

· Flowable: A Publisher that emits 0..N elements, and then completes successfully or with an error.

· Observable: Similar to Flowables but without a backpressure strategy. They were introduced in RxJava 1.x.

· Single: It completes with a value successfully or an error.(doesn’t have onComplete callback, instead onSuccess(val)).

· Maybe: It completes with/without a value or completes with an error.

Completable: It just signals if it has completed successfully or with an error.

What is MVVM?

Model-View-ViewModel architecture consists of 3 parts.

· The View gets user’s actions and sends to the ViewModel, or listens live data stream from the ViewModel and provides to the users.

· The ViewModel gets user’s actions from the View or provides data to View.

· The Model abstracts the data source. View and ViewModel uses that on data stream.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429134218.28329239664798975163169773899404:50510506024149:2800:5A772F7454AD0A2F20753352CC5D4B96FA73BCB4265912D2541750D9960B0E0F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429134218.28329239664798975163169773899404:50510506024149:2800:5A772F7454AD0A2F20753352CC5D4B96FA73BCB4265912D2541750D9960B0E0F.png[/IMG]

LiveData:

· LiveData is one of the newly introduced architecture components. LiveData is an observable data holder.

· This allows the components in your app to be able to observe LiveData objects for changes without creating explicit and rigid dependency paths between them.

· This decouples completely the LiveData object producer from the LiveData object consumer.

ViewModel:

· ViewModel is also one of the newly introduced architecture components.

· Architecture components provide a new class called ViewModel, which is responsible for preparing the data for the UI/View.

· ViewModel gives you a good base class for your MVVM ViewModel layer since ViewModel (and its children AndroidViewModel)’s extending classes are automatically having their holding data retained during configuration changes.

· This means that after configuration changes, this ViewModel holded data is immediately available to the next activity or fragment instance.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429134502.55211150196964131645159074560508:50510506024149:2800:AA1EE7ED859154686929208A80F55A51D72FB525BD2D658D9D92861CB5AD55DE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429134502.55211150196964131645159074560508:50510506024149:2800:AA1EE7ED859154686929208A80F55A51D72FB525BD2D658D9D92861CB5AD55DE.png[/IMG]

Project Structure:

We will create packages by features. It will make your code more modular and manageable.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429134634.67520435129831122226619434846880:50510506024149:2800:9BBCF04F04C41B478626F50A07DCB57190455112E6B61336C2070955FD84B6DC.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429134634.67520435129831122226619434846880:50510506024149:2800:9BBCF04F04C41B478626F50A07DCB57190455112E6B61336C2070955FD84B6DC.png[/IMG]

DataBinding:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429134712.60995614055352138843531144759068:50510506024149:2800:31069A861AF6D1A3C667C06C6ED7533E2B96424FA228689C739AAD6A72EB21B4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/767/951/2640091000001767951.20200429134712.60995614055352138843531144759068:50510506024149:2800:31069A861AF6D1A3C667C06C6ED7533E2B96424FA228689C739AAD6A72EB21B4.png[/IMG]

· Android data binding allows for 1 or 2-way binding within the layout XML files of Android. This should be familiar to anyone who has done web based development with Angular or Ember, or C# WPF development.

· The most basic case is to add an on-click listener. This requires no registering of on click listeners in any of your application code, just some XML in the layout file and a function to be called in your ViewModel class

<?xml version="1.0" encoding="utf-8"?>
<layout >
<data>
<variable
name=" "
type=""/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@{viewModel.getHourOfDay()}"
tools:text="14:00" />
</RelativeLayout>
</layout>

Weather Notification API:

The Weather Notifications API enables you to set weather-driven push notifications to applications. With predefined weather parameter threshold values, you are able to be informed of certain weather phenomena like when rain, strong winds or exceptional heat starts.

The following content will be the coding process.

If you are interested, check the full content by https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201235519702030082&fid=0101187876626530001

More information about HMS, you can visit https://forums.developer.huawei.com/forumPortal/en/home