Notification Alert! Integration of Huawei Push Kit in Application (Kotlin)
Tags: android, kotlin, programming, huawei
Published: 2022-03-04 08:17:31

## **Introduction**
In this article, I will be integrating Huawei Push Kit in an Application.

## **Push Kit**
 
Push notifications offers a great way to increase your application’s user engagement and boost your retention rates by sending meaningful messages or by informing users about your application. Push Kit supports two types of messages: notification messages and data messages. You can send notifications and data messages to your users from your server using the [Push Kit APIs](https://developer.huawei.com/consumer/en/doc/development/HMS-3-References/push-server-send) or directly from the [AppGallery Push Kit Console. ](https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/push-console)

## **Requirements**
1. Any operating system (MacOS, Linux and Windows).
2. Must have a Huawei phone with HMS 4.0.2.300 or later.
3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.
4. Minimum API Level 21 is required.
5. Required EMUI 9.0.0 and later version devices.

## **Integrate HMS Dependencies**
1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.
2. Create a project in android studio, refer Creating an Android Studio Project.
3. Generate a SHA-256 certificate fingerprint.
4. To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.
Or use cmd as explained in [SHA256 CODE](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#3)
5. Create an App in AppGallery Connect.
6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory.
7. Enter SHA-256 certificate fingerprint and click Save.
8. Click Manage APIs tab and enable Push Kit.
9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.

```
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
```

10. Add the below plugin and dependencies in build.gradle(Module) file.

```
apply plugin: id 'com.huawei.agconnect' 
```
//Huawei AGC 

```
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
```
 // Huawei Push Kit 

```
implementation 'com.huawei.hms:push:6.3.0.302' 
```
11. Now Sync the gradle.
12. Add the required permission to the Manifestfile.xml file.

```
// Push Kit
<uses-permission android:name="android.permission.INTERNET" />
<service
    android:name=".PushService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
    </intent-filter>
</service>
```

Sometimes adding everything manually can be burden. Let’s use Configuration Wizard for the setup.


## **Configuration Wizard**
Now, we will help how to use configuration wizard of HMS Toolkit to automatically complete necessary configuration for development preparations.

- Understand how to use the configuration Wizard.
- Once all the items are passed, you are good to go.
 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ahy4834kk4ekmgqgoi6.png)                            

## **Integrating Push Kit Through Coding Assistant**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbl8hqv695q67kmbwt1h.png)
1.	Navigate to HMS > Coding Assistant, and select Push Kit in the kit list. The following figure shows all the scenarios of Push Kit.

2.	Completing the Code by Dragging and Dropping Scenario Cards.

3.	Select a scenario card for development, for example, Send notification message-Open home pages of corresponding apps, and drag and drop the card to the code area to generate the code for obtaining a push token.


## **Cloud Debugging**
Use Cloud Debugging in HMS Toolkit to debug the app on a real device.
To use Cloud Debugging, you need to sign in using a HUAWEI ID, complete identity verification, and then authorize the sign-in. 
1.	Choose HMS > CloudDebugging.

2.	You can use Available Devices. Select a device and click RUN.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqx5qrop8jbh3qg67j7w.PNG)

## **Using a Tool to Send Messages**

- On the toolbar of the Push Kit card, select Message Test to test whether the server can push messages to the device.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fseybh65a7t6ujd9r9sp.png)
 

- Click Get APP ID and APP SECRET. The tool automatically reads the app ID and app secret from AppGallery Connect. Click Next. 

 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvqbvwzp66rjlegthwi1.png)

- On the page that is displayed, set parameters as prompted or based on the following table.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2nr4lw4zmq9weaps3e1.png)
 
After the setting, click Send.

## **Result**

After successful message sending, you will receive a notification message on your device.                   

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4trg888iip3xmfsonbq3.jpg)


## **Tips and Tricks**
1. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
2. Make sure you have added the agconnect-services.json file to app folder.
3. Make sure you have added SHA-256 fingerprint without fail.
4. Make sure all the dependencies are added properly.

## **Conclusion**
In this article, we have learnt integration of Push Kit in application. It will guide you to increase your application’s user engagement and boost your retention rates by sending meaningful messages or by informing users about your application.

## **Reference**
Push Kit:[ Documentation](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)