# Huawei Push

*This guide assumes that you have already* [*integrated the Batch SDK into your app*](https://doc.batch.com/developer/sdk/android/sdk-integration)*.*

### Add the Batch HMS plug-in

The Batch HMS plug-in is available on Maven Central:

{% tabs %}
{% tab title="Kotlin" %}
{% code title="build.gradle" %}

```kts
implementation("com.batch.android:hms-plugin:2.0.0")
```

{% endcode %}
{% endtab %}

{% tab title="Groovy" %}
{% code title="build.gradle" %}

```groovy
implementation 'com.batch.android:hms-plugin:2.0.0'
```

{% endcode %}
{% endtab %}
{% endtabs %}

Using it requires at least Batch SDK 2.0.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.*\
\&#xNAN;*If you didn't, please follow* [*Huawei's Get Started tutorial*](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html)*. 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` 6.5.0 or higher. We highly recommend to use the latest version when possible.

Add the following to your *build.gradle*, if not already present:

{% tabs %}
{% tab title="Kotlin" %}
{% code title="build.gradle" %}

```kts
implementation("com.huawei.hms:push:6.5.0.300")
```

{% endcode %}
{% endtab %}

{% tab title="Groovy" %}
{% code title="build.gradle" %}

```groovy
implementation 'com.huawei.hms:push:6.5.0.300'
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
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.
{% endhint %}

### 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:

```markdown
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](https://doc.batch.com/getting-started/features/mobile-engagement-platform/settings/app-settings).

**2. Obtaining your device token**

You can find your device's token using the [debug tool](https://doc.batch.com/developer/sdk/android/profile-data/debug) or locating the token Batch posts to the logcat *(*[*see here*](https://developer.android.com/tools/debugging/debugging-studio) *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](#troubleshooting) for more info.

{% hint style="info" %}
Push notifications are not supported in all emulators. Please use a physical device or a Google API/Google Play emulator to test them.
{% endhint %}

**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:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
Batch.User.getInstallationID()
```

{% endtab %}

{% tab title="Java" %}

```java
Batch.User.getInstallationID();
```

{% endtab %}
{% endtabs %}

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](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2F9tXIqqSmkaMLleeNs9GE%2Fdashboard_android_debug_push_test.png?alt=media\&token=e333b658-1eb4-44d0-9bd4-a10fe852da6f)

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:

{% code title="AndroidManifest.xml" %}

```xml
<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>
```

{% endcode %}

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

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
class MyHmsMessageService : HmsMessageService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
        BatchHms.onMessageReceived(this, remoteMessage)
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        BatchHms.onNewToken(this, token)
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
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);
    }
}
```

{% endtab %}
{% endtabs %}

### 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>`:

{% code title="AndroidManifest.xml" %}

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

{% endcode %}

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

{% code title="res/values/strings.xml" %}

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

{% endcode %}

(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 an HMS powered Huawei device will not show up in the [Inbox API](https://doc.batch.com/developer/sdk/android/inbox).

### 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](https://developer.huawei.com/consumer/en/doc/development/HMS-References/push-ErrorEnum4) for more details about what the error codes mean.
