Huawei Push

This guide assumes that you have already integrated the Batch SDK into your app.

Add the Batch HMS plug-in

The Batch HMS plugin is currently in beta and available on Maven Central:

implementation 'com.batch.android:hms-plugin:1.0.0'

Using it requires at least Batch SDK 1.16.0.

Adding push notifications support

This guide assumes that you have already set up your Huawei project in your Android application and enabled PushKit on AppGallery.
If you didn't, please follow Huawei's Get Started tutorial. Then, enable the PushKit API on your AppGalery dashboard.

In order to enable push notifications support, Batch requires com.huawei.hms:push 4.0.4 or higher. We highly recommend to use the latest version when possible. Add the following to your build.gradle, if not already present:

implementation "com.huawei.hms:push:4.0.4.301"

Note: If you ended up with a sample HmsMessageService, either delete it if you only plan to use Batch, or scroll below to get instructions on how to integrate Batch in your custom service.

If you want to allow Batch to fetch your Advertising ID and facilitate debugging your integration, you can also include com.huawei.hms:ads-identifier. Don't worry, it is entirely optional.

implementation "com.huawei.hms:ads-identifier:3.4.30.307"

Note that adding this in your apps will enable Advertising ID collection in both HMS and Batch. Your app must provide a privacy policy to comply with Play Store rules. You can always disable Advertising ID collection in Batch by calling setCanUseAdvertisingID(false) on Batch's Config object. Please see Firebase's documentation regarding how to disable it.

Testing your integration

Congratulations on finishing the bulk of the integration!

After deploying a build to your device or an Google Play enabled simulator, open the Logcat tab of Android Studio. You should see the following logs:

Batch (<version>) is running in dev mode (your API key is a dev one)
Installation ID: <your installation ID>

Your first notification

1. Set up your Huawei credentials

In order to be able to send notifications using HMS, Batch needs to know about your credentials. This setup takes place on the dashboard. Click here to get instructions.

2. Obtaining your device token

You can find your device's token using the debug tool or locating the token Batch posts to the logcat (see here to know more):

I/Batch: Push - Registration ID/Push Token: Using Huawei Mobile Services 
I/Batch: Push - Registration ID/Push Token (HMS): <your device token>

3. Sending a test push

Go to ⚙ Settings → Push settings, paste your device's token and click on Save.

Then, all you have to do is to click on the "Send" button. If you sucessfuly set up the plug-in, you will receive a notification on your device.

Test push

Using a custom HmsMessageService

This step is optional and is only required if you want to use your own HmsMessageService.

First, you need to remove the HmsMessageService Batch is adding in your manifest:

<manifest 
    [...]
    xmlns:tools="http://schemas.android.com/tools">

    <!-- Add this line to remove the default Batch HmsMessageService -->
    <service
        android:name="com.batch.android.plugin.hms.BatchHmsMessageService"
        tools:node="remove" />

    <!-- Add your custom service -->
    <service
        android:name=".MyHmsMessageService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
        </intent-filter>
    </service>

</manifest>

Then, make sure to call BatchHms in your new service like so:

public class MyHmsMessageService extends HmsMessageService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        BatchHms.onMessageReceived(this, remoteMessage);
    }

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        BatchHms.onNewToken(this, token);
    }
}

Overriding the HMS App ID

When using HMS, Batch autodetects the App ID to use with the one set in your agconnect-services.json.

If for any reason you want to specify another Sender ID, add the following line in your AndroidManifest.xml, under <application>:

<meta-data android:name="batch_push_hms_app_id_override" android:resource="@string/batch_hms_app_id" />

Then, add the matching String ressource with the App ID you want to use:

res/values/strings.xml

<string name="batch_hms_app_id">123456</string>

(You do not have to name your string ressource batch_hms_app_id , it can be anything as long as it is referenced in the manifest meta-data.)

Troubleshooting

I'm not getting a token

While this can be due to a number of reasons, the usual errors are:

  • Push Kit isn't enabled on AppGallery connect.
  • Your agconnect-services.json isn't correct.
  • Your SHA-256 certificate fingerprint isn't configured on AppGallery Connect or doesn't match the one used to sign your build.

Your logcat should log more information about the error. Please check Huawei's error code reference page for more details about what the error codes mean.