Huawei Mobile Services

Integrate Huawei Mobile Services in your app to support Engagement push notifications on Huawei devices

📘

Newer phones manufactured by Huawei come with Huawei Mobile Services (HMS) - a service that delivers push notifications instead of Google's Firebase Cloud Messaging (FCM).

To be able to send push notifications from the Engagement platform and receive them in your app on Huawei devices, you must set up Huawei Mobile Services (HMS), implement HMS in your app, and configure the Huawei Push Service integration in the Engagement web app.

👍

The SDK provides a push setup self-check feature to help developers successfully set up push notifications. The self-check will try to track the push token, request the Engagement backend to send a silent push to the device, and check if the app is ready to open push notifications.

To enable the setup check, set Exponea.checkPushSetup = true before initializing the SDK.

We suggest you turn the self-check feature on while implementing the push notifications for the first time or if you need to do some troubleshooting.

Set up Huawei Mobile Services

First, you must set up Huawei Mobile Services:

  1. Register and set up a Huawei Developer account.
  2. Create a Project and App in AppGallery Connect.
  3. Generate and configure a Signing Certificate.
  4. Enable Push Kit in AppGallery Connect APIs.
  5. Update Gradle scripts and add generated agconnect-services.json to your app.
  6. Configure the Signing Information in your app.

📘

For detailed instructions, please refer to Preparations for Integrating HUAWEI HMS Core in the official HMS documentation.

Implement HMS Message Service in Your App

Next, you must create and register a service that extends HmsMessageService. The SDK's automatic tracking relies on your app providing this implementation.

👍

This implementation is not included in the SDK in order to keep it as small as possible and avoid including the libraries that are not essential for its functionality. You can copy the example code below and use it in your app.

  1. Create the service:

    import android.app.NotificationManager  
    import android.content.Context  
    import com.exponea.sdk.Exponea  
    import com.huawei.hms.push.HmsMessageService  
    import com.huawei.hms.push.RemoteMessage
    
    class MyHmsMessagingService: HmsMessageService() {
    
        private val notificationManager by lazy {
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        }
    
        override fun onMessageReceived(message: RemoteMessage) {
            super.onMessageReceived(message)
            if (!Exponea.handleRemoteMessage(applicationContext, message.dataOfMap, notificationManager)) {
                // push notification is from another push provider
            }
        }
    
        override fun onNewToken(token: String) {
            super.onNewToken(token)
            Exponea.handleNewHmsToken(applicationContext, token)
        }
    }
    
  2. Register the service in AndroidManifest.xml:

    <service android:name="MyHmsMessagingService" android:exported="false">  
        <intent-filter> 
            <action android:name="com.huawei.push.action.MESSAGING_EVENT"/>  
        </intent-filter>
    </service>  
    <meta-data  android:name="push_kit_auto_init_enabled" android:value="true"/>
    

The SDK will only handle push notification messages sent from the Engagement platform. A helper method Exponea.isExponeaPushNotification() is also provided.

If you run the app, the SDK should the track push token to the Engagement platform. If you enabled the self-check, it will you inform you of this. Alternatively, you can find the customer in the Engagement web app and check the customer property huawei_push_notification_id.

❗️

If you are integrating the SDK into an existing project, you may face an issue that your HmsMessageService is not called automatically.

To retrieve a fresh push token, consider requesting a token manually as soon as possible after after application start:

Refer to Obtaining and Deleting a Push Token in the HMS documentation for instructions on how to retrieve the current push token.

❗️

The methods Exponea.handleNewToken and Exponea.handleRemoteMessage can be used before SDK initialization if a previous initialization was done. In such a case, each method will track events with the configuration of the last initialization. Consider initializing the SDK in Application::onCreate to make sure a fresh configuration is applied in case of an application update.

Configure the Huawei Push Service Integration in Engagement

  1. In Huawei App Gallery Connect, navigate to Project settings > App information > OAuth 2.0 client ID. Locate the Client ID and Client secret copy their values. You will use these to configure the Huawei Push Service integration in Engagement.
    HMS - Client ID and Client secret

  2. Open the Engagement web application and navigate to Data & Assets > Integrations. Click + Add new integration.

  3. Locate Huawei Push Service and click + Add integration.
    Engagement Integrations - Select Firebase Cloud Messaging integration

  4. Enter the Client ID and Client secret values you copied in step 1. Click Save integration to finish.
    Engagement Integrations - Configure Firebase Cloud Messaging integration

  5. Navigate to Settings > Project settings > Channels > Push notifications > Android Notifications and set Huawei integration to Huawei Push Service.