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 plug-in is available on Maven Central:

implementation 'com.batch.android:hms-plugin:1.1.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. During this tutorial, you will be told to enable APIs/Services: make sure to enable the PushKit API.

In order to enable push notifications support, Batch requires com.huawei.hms:push 5.3.0 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:6.5.0.300"

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>

If you don't see a push token, there might be an error in the logs describing what happened. See our troubleshooting documentation for more info.

Note: Push notifications are not supported in all emulators. Please use a physical device or a Google API/Google Play emulator to test them.

3. Obtaining your Installation ID

You can then retrieve the Installation ID, which represents an installation of your app on a device, by calling the following method:

Batch.User.getInstallationID();

While Batch prints this in the debug console on start, displaying it in a settings or about page enables users to send you this identifier. This is useful for debugging, sending test notifications, etc.

4. Sending a test push

Batch enables you to send a test notification to the application installation currently running on your device.

To do so, open the dashboard and go to ⚙ Settings → Debug. Enter your Installation ID, hit Debug and then click on "Send Test Push".

Send Test from Debug

You should receive a notification on your device. If not, or if you can't find your Installation ID, the SDK might not be properly configured.

If you need to send push notifications to your device on a regular basis, then you should add your ID as a test device by clicking the "Save as a test device" button.

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.)

Limitations

  • Push notifications sent to a HMS powered Huawei device will not show up in the Inbox API.

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.