FORUMS

Using HMS Push Kit for Push Notifications

1,031 posts
Thanks Meter: 1,110
 
By XDARoni, XDA Community Manager on 26th June 2020, 01:59 PM
Post Reply Email Thread
Ever wanted to send push notifications to your Android app? Yes? Well then, there's a good chance you've used a delivery service like Google Cloud Messaging or Firebase Cloud Messaging. Unfortunately, these services won't work on Huawei devices.

Luckily, Huawei has implemented its own notification delivery framework as part of HMS. If you're targeting Huawei devices with your app and need to deliver push notifications, this guide is for you. We'll be talking about receiving notifications through HMS on Android.

Preparation
First up, make sure you have a Huawei Developer Account. This process can take a couple days, and you'll need one to use this SDK, so be sure to start that as soon as possible. You can sign up at https://developer.huawei.com.

Next, you'll want to obtain the SHA-256 representation of your app's signing key. If you don't have a signing key yet, be sure to create one before continuing. To obtain your signing key's SHA-256, you'll need to use Keytool which is part of the JDK installation. Keytool is a command-line program. If you're on Windows, open CMD. If you're on Linux, open Terminal.
On Windows, you'll need to "cd" into the directory containing the Keytool executable. For example, if you have JDK 1.8 v231 installed, Keytool will be located at the following path:
Code:
Select Code
C:\Program Files\Java\jdk1.8.0_231\bin\
Once you find the directory, "cd" into it:
Code:
Select Code
C: #Make sure you're in the right drive
cd C:\Program Files\Java\jdk1.8.0_231\bin\
Next, you need to find the location of your keystore. Using Android's debug keystore as an example, where the Android SDK is hosted on the "E:" drive in Windows, the path will be as follows:
Code:
Select Code
E:\AndroidSDK\.android\debug.keystore
(Keytool also supports JKS-format keystores.)

Now you're ready to run the command. On Windows, it'll look something like this:
Code:
Select Code
keytool -list -v -keystore E:\AndroidSDK\.android\debug.keystore
On Linux, the command should be similar, just using UNIX-style paths instead.
Enter the keystore password, and the key name (if applicable), and you'll be presented with something similar to the following:


Make note of the SHA256 field.

SDK Setup
Now we're ready to add the Push Kit SDK to your Android Studio project. Go to your Huawei Developer Console and click the HUAWEI AppGallery tile. Agree to the terms of use if prompted.
Click the "My projects" tile here. If you haven't already added your project to the AppGallery, add it now. You'll be asked for a project name. Make it something descriptive so you know what it's for.


Now, you should be on a screen that looks something like the following:


Click the "Add app" button. Here, you'll need to provide some details about your app, like its name and package name.


Once you click OK, some SDK setup instructions will be displayed. Follow them to get everything added to your project. You'll also need to add the following to the "dependencies" section of your app-level build.gradle file:

Code:
Select Code
implementation 'com.huawei.hms:push:4.0.4.301'


If you ever need to come back to these instructions, you can always click the "Add SDK" button after "App information" on the "Project setting" page.


Now you should be back on the "Project setting" page. Find the "SHA-256 certificate fingerprint" field under "App information," click the "+" button, and paste your SHA-256.


Go back to the Manage APIs tab on the "Project setting" page. Scroll down until you find "Push Kit" and make sure it's enabled.



Next, expand the "Growing" category in the sidebar and select "Push Kit."


You should see something similar to the following:


Click the "Enable now" button. After a few seconds, you should see this popup:


Next, go to the "Settings" tab. You should then be prompted to select a data storage location. Choose the one that's best for you.



Now, if you're using obfuscation in your app, you'll need to whitelist a few things for HMS to work properly.

For ProGuard:
Code:
Select Code
-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.hianalytics.android.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.huawei.hms.**{*;}
For AndResGuard:
Code:
Select Code
"R.string.hms*",
"R.string.agc*",
"R.string.connect_server_fail_prompt_toast",
"R.string.getting_message_fail_prompt_toast",
"R.string.no_available_network_prompt_toast",
"R.string.third_app_*",
"R.string.upsdk_*",
"R.layout.hms*",
"R.layout.upsdk_*",
"R.drawable.upsdk*",
"R.color.upsdk*",
"R.dimen.upsdk*",
"R.style.upsdk*
Next up, you'll need to create a class extending HmsMessageService:

Code:
Select Code
class MyMessageService : HmsMessageService() {
    //...
}
And add it to your AndroidManifest.XML:

Code:
Select Code
<service
    android:name=".MyMessageService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
    </intent-filter>
</service>
That's it! The Push Kit SDK should now be available in your project.

Basic Usage
To use the Push Kit SDK, you'll need to generate a token for each device using your app. The SDK can take care of this for you. Simply add the following to your AndroidManifest.xml:

Code:
Select Code
<application>
    <!--.....-->

    <meta-data 
        android:name="push_kit_auto_init_enabled" 
        android:value="true" /> 
</application>
Then, inside MyMessageService, you just need to override the following method:

Code:
Select Code
class MyMessageService : HmsMessageService() {
    //Make sure whatever happens here takes fewer than 10 seconds.
    //Otherwise, you'll run into Android's Service restrictions
    //and you'll need to create a Job.
    override fun onNewToken(token: String?) {
        if (!token.isNullOrBlank()) {
            //Send the token to your server
        }
    }
}
(For more advanced behavior, like manually applying for tokens, see Huawei's documentation.)

To receive messages, simply override the onMessageReceived() method:

Code:
Select Code
class MyMessageService : HmsMessageService() {
    //...

    //A new message has been received
    override fun onMessageReceived(message: RemoteMessage?) {
        if (message == null) {
            //The message is null. Nothing to handle.
            return
        }

        //There is a lot of potential data contained in a message.
        val collapseKey = message.collapseKey //String
        val data = message.data //String
        val from = message.from //String
        val to = message.to //String
        val messageId = message.messageId //String
        val sentTime = message.sentTime //Long
        val dataOfMap = message.dataOfMap //MutableMap<String, String>
        val messageType = message.messageType //String
        val ttl = message.ttl //Int
        val token = message.token //String

        //You can also get a notification template for the message
        val notification = message.notification //RemoteMessage$Notification

        val imageUrl = notification.imageUrl //Uri
        val title = notification.title //String
        val titleLocalizationKey = notification.titleLocalizationKey //String
        val titleLocalizationArgs = notification.titleLocalizationArgs //Array<String>
        val body = notification.body //String
        val bodyLocalizationKey = notification.bodyLocalizationKey //String
        val bodyLocalizationArgs = notification.bodyLocalizationArgs //Array<String>
        val icon = notification.icon //String
        val sound = notification.sound //String
        val tag = notification.tag //String
        val color = notification.color //String
        val clickAction = notification.clickAction //String
        val channelId = notification.channelId //String
        val link = notification.link //Uri
        val notifyId = notification.notifyId //Int

        //Just like with onNewToken(), this method needs to complete
        //within 10 seconds.
    }
}
Conclusion
That's all you need to receive push notifications on Android through HMS!

To learn how you can set up message sending, and for more advanced methods of notification handling, be sure to check out Huawei's official documentation.
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes