DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Notification Alert! Integration of Huawei Push Kit in Application (Kotlin)
Ribhav
Ribhav

Posted on

Notification Alert! Integration of Huawei Push Kit in Application (Kotlin)

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 or directly from the AppGallery Push Kit 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
  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'
Enter fullscreen mode Exit fullscreen mode
  1. Add the below plugin and dependencies in build.gradle(Module) file.
apply plugin: id 'com.huawei.agconnect' 
Enter fullscreen mode Exit fullscreen mode

//Huawei AGC

implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
Enter fullscreen mode Exit fullscreen mode

// Huawei Push Kit

implementation 'com.huawei.hms:push:6.3.0.302' 
Enter fullscreen mode Exit fullscreen mode
  1. Now Sync the gradle.
  2. 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>
Enter fullscreen mode Exit fullscreen mode

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

Integrating Push Kit Through Coding Assistant

Image description

  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

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

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

Image description

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

Image description

After the setting, click Send.

Result

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

Image description

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
Push Kit: Training Video

Top comments (0)

Find what you were looking for? Sign up so you can:

Β 
🌚 Enable dark mode
πŸ”  Change your default font
πŸ“š Adjust your experience level to see more relevant content