English | 中文

hw_push_test

This is a demo on how to use Huawei Push.

Integrating Huawei Push

  1. Add Huawei Push sdk
flutter pub get huawei_push
  1. Add agconnect-services.json file in android/app/ (Get agconnect-services.json).

  2. Add Huawei's AppId in android/app/src/main/AndroidManifest.xml

<application>
    ...
    <meta-data
        android:name="com.huawei.hms.client.appid"
        android:value="Your huawei appid">
    </meta-data>
    ...
</application>
  1. Add xxx.jks signature file in android/app/ directory (Configure signature

), and add signingConfigs information to android/app/build.gradle, and configure obfuscation file (Configure obfuscation file).

android{
    ...
    signingConfigs {
        config {
            keyAlias 'xxxx'
            keyPassword 'xxxx'
            storeFile file('huawei_push_flutter.jks')
            storePassword 'xxxx'
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.config
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.debug
        }
    }
    ...
}
  1. Upload the certificate fingerprint to Huawei Push backend (Get certificate fingerprint).

Setting up in im sdk

  1. Integrate im sdk.

  2. Upload Huawei Push configuration to Easemob backend Upload certificate to im backend.

  3. Need to start HWPush during initialization.

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  final options = ChatOptions(appKey: "Your Appkey");
  // Enable push
  options.enableHWPush();
  EMClient.getInstance.init(options).then((value) => runApp(const MyApp()));
}
  1. Get Huawei Push token, and pass the Huawei Push token to im sdk after im login
Push.getTokenStream.listen(
    (event) async {
    _token = event;
    try {
        await ChatClient.getInstance.pushManager.updateHMSPushToken(_token);
    } catch (e) {
        debugPrint('bind token error: $e');
    }
    showResult('TokenEvent', _token);
    },
    onError: (e) {
    debugPrint('get token error: $e');
    },
);