Huawei Channel v4 Connection

Last updated: 2020-09-01 18:06:05

    Operation Scenarios

    Huawei launches HMS Push SDK v4.0 as an upgrade of Huawei Push SDK v2.6.3.
    Compared with SDK v2.6, v4.0 features major changes in API call and push service enablement steps.
    As TPNS always keeps up with the update progress of each vendor channel's push service, it provides plugin dependency packages integrated with HMS Push SDK v4.0 for your choice.

    Note:

    • HMS Push SDK v4 is incompatible with v2. If both are integrated, their dependency packages will conflict with each other. Please choose one of them for integration.
    • HMS Push SDK v4 no longer supports direct delivery of custom parameters in key-value format from the server and the onEvent callback for notification click events provided in v2. You are recommended to use the general intent scheme on Android to deliver such events. For more information, please see How do I set the message click event?.
    • You can still integrate Huawei Push SDK v2 to use the Huawei Push service. For more information, please see Huawei Channel v2 Connection.
    • For Huawei Push, you can successfully register with the Huawei channel and push messages through it only in a signed package environment.
    • The Huawei channel supports click callback but not arrival callback.

    Application Configuration on Huawei Push Platform

    Getting key

    1. Go to the Huawei Developer platform.
    2. Sign up for a developer account and log in to the platform. For more information, please see Account Registration and Verification. (If you are registering a new account, you need to complete identity verification.)
    3. Create an application on the Huawei Push platform. For more information, please see Creating Application. (The application package name must be the same as that entered in the TPNS Console.)
    4. Get and copy the application's AppID and AppSecret and paste them in Application Configuration > Huawei Channel in the TPNS Console.

    Configuring SHA-256 certificate fingerprint

    Get the SHA-256 certificate fingerprint and configure it on the Huawei Push platform as instructed in Generating Signature Certificate Fingerprint.

    Getting Huawei Push configuration file

    Download the latest configuration file agconnect-services.json from Huawei developer platform in My Project -> Select Project -> Project Setting.

    Enabling push service

    Enable the push service on the Huawei Push platform. For more information, please see Enabling Push Service.

    SDK Integration (Two Methods)

    Using Android Studio Gradle for automatic integration

    1. In the build.gradle file in the Android project-level directory, add the Huawei repository address and HMS Gradle plugin dependencies under repositories and dependencies in buildscript, respectively:

      buildscript {
       repositories {
           google()
           jcenter()
           maven {url 'http://developer.huawei.com/repo/'}     // Huawei Maven repository address
       }
       dependencies {
           // Other `classpath` configurations
           classpath 'com.huawei.agconnect:agcp:1.2.1.301'     // Gradle plugin dependencies of Huawei Push
       }
      }
    2. In the build.gradle file in the Android project-level directory, add the Huawei dependency repository address under repositories in allprojects:

      allprojects {
       repositories {
           google()
           jcenter()
           maven {url 'http://developer.huawei.com/repo/'}     // Huawei Maven repository address
       }
      }
    3. Copy the application configuration file agconnect-services.json obtained from the Huawei Push platform to the app module directory.

    4. Add the following configuration to the build.gradle file at its beginning in the app module:

      // Other Gradle plugins of application
      apply plugin: 'com.huawei.agconnect'      // HMS Push SDK v4 Gradle plugin
      android {
       // Application configuration content
      }
    5. Import the dependencies related to Huawei Push into the build.gradle file under the app module:

      dependencies {
       // Other dependencies of the program
       implementation 'com.tencent.tpns:huawei:v3-1.0.0.0-release'      // HMS Push v4 TPNS plugin
       implementation 'com.huawei.hms:push:4.0.3.301'       // HMS SDK v4 push module dependency package
       }

    Using Android Studio for manual integration

    If you cannot access Huawei Maven repository in your internal development environment, you can try the following manual integration method:

    1. Manually download all dependency packages required by HMS Push SDK v4.0 from the Huawei repository and store them in the libs directory under the app module:

      1. One JAR dependency for Gradle script: agcp-1.2.1.301.jar
      2. Nine AAR program dependencies:
        1. agconnect-core-1.0.0.300.aar
        2. base-4.0.2.300.aar
        3. network-common-4.0.2.300.aar
        4. network-grs-4.0.2.300.aar
        5. opendevice-4.0.1.301.aar
        6. push-4.0.3.301.aar
        7. tasks-1.3.3.300.aar
        8. update-2.0.6.300.aar
        9. huawei-v3-1.0.0.0-release.aar
    2. In the build.gradle file in the Android project-level directory, add HMS Gradle plugin dependencies under dependencies in buildscript:

      buildscript {
       repositories {
           google()
           jcenter()
       }
       dependencies {
           // Other `classpath` configurations
           classpath files('app/libs/agcp-1.2.1.301.jar')     // Gradle plugin dependencies of Huawei Push
       }
      }
    3. Copy the application configuration file agconnect-services.json obtained from the Huawei Push platform to the app module directory.

    4. Add the following configuration to the build.gradle file at its beginning in the app module:

      // Other Gradle plugins of application
      apply plugin: 'com.huawei.agconnect'      // HMS Push SDK v4 Gradle plugin
      android {
       // Application configuration content
      }
    5. Import the dependencies related to Huawei Push into the build.gradle file under the app module:

      dependencies {
       // Other dependencies of the program
       implementation fileTree(include: ['*.aar'], dir: 'libs')
       }
    6. Add the following components between the <application> and </application> tags in the manifest file:

      <application>
       <service
           android:name="com.huawei.android.hms.tpns.HWHmsMessageService"
           android:exported="false">
           <intent-filter>
               <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
           </intent-filter>
       </service>
      </application>

    Huawei Push Activation

    Enable the third-party push API before calling the TPNS registration API XGPushManager.registerPush:

    // Enable the third-party push
    XGPushConfig.enableOtherPush(getApplicationContext(), true);

    The log of successful registration is as follows:

    I/TPush: [XGOtherPush] other push token is : 0865551032618726300001294600CN01 other push type: huawei
    I/TPush: [a] binder other push token with accid = 2100274337  token = 17c32948df0346d5837d4748192e9d2f14c81e08 otherPushType = huawei otherPushToken = 0865551032618726300001294600CN01

    Code Obfuscation

    -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.**{*;}

    Note:

    Obfuscation rules must be stored in the proguard-rules.pro file at the application project level.

    Advanced Configuration (Optional)

    Arrival receipt configuration for Huawei channel

    The arrival receipt for the Huawei channel should be configured by yourself. After configuring this feature as instructed in Receipt Configuration Guide for Huawei Channel, you can view the arrival data for the Huawei push channel in the push records.

    Badge adaptation for Huawei devices

    You can set the application badge on Huawei devices after applying for the application badge setting permission and setting the application start class. For more information, please see Badge Adaption Guide.

    Troubleshooting

    Querying Huawei Push registration error code

    The Huawei Push service has strict requirements on the connection configuration. If you fail to register with the Huawei channel, you can use either of the following ways to get the Huawei Push registration error code:

    1. In debugging mode of the push service, filter logs by the keyword OtherPush or HMSSDK to view the return code logs.
    2. Call the XGPushConfig.getOtherPushErrCode(context) API in the callback method of the TPNS registration API XGPushManager.registerPush to get the vendor channel registration return code:

    You can find the cause of the error and get the solution in Common error codes.

    Others

    When your application is released on Huawei AppGallery, it may fail the audit with the error message "Error:28: you need to package the certificate file into the APK when integrating with HMS. Please directly copy the assets directory to the application project's root directory" displayed. Please fix the problem as follows:

    Download the official Huawei HMS SDK and copy all files and subdirectories in the `assets` directory to that in your application project. If the `assets` directory does not exist in the application project, create one.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help