Flutter 2.5.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4cc385b4b8 (5 weeks ago) • 2021-09-07 23:01:49 -0700
Engine • revision f0826da7ef
Tools • Dart 2.14.0
umeng_push_sdk: 1.0.1
umeng_common_sdk: 1.2.3

配置Android宿主项目

前置条件:下载官方SDK,加压后参考push文件夹下的example

拷贝example下的MainApplication.java,MainActivity.java,helper,到宿主项目的android文件夹下,并修改位于文件顶部的包名package

修改PushConstants.java中定义的常量如appkey、secret等信息。

微调MainActivity.java使其同时支持友盟统计并引用PushConstants.java中的常量。

package com.XXX.XXX;

import android.os.Bundle;
import androidx.annotation.NonNull;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.PushAgent;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;
import com.XXX.XXX.helper.PushConstants;

public class MainActivity extends FlutterActivity {

  private static final String TAG = "MainApplication";

  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    UMConfigure.preInit(this,PushConstants.APP_KEY, PushConstants.CHANNEL);
    UMConfigure.setLogEnabled(true);

    PushAgent.getInstance(this).onAppStart();

    //设置上下文
    com.umeng.umeng_common_sdk.UmengCommonSdkPlugin.setContext(this);
    android.util.Log.i("UMLog", "onCreate@MainActivity");
  }

  @Override
  protected void onPause() {
    super.onPause();
    MobclickAgent.onPause(this);
  }

  @Override
  protected void onResume() {
    super.onResume();
    MobclickAgent.onResume(this);
  }
}

修改AndroidManifest.xml中的application配置

android:label="应用名称"
android:name="com.XXX.XXX.MainApplication"
tools:replace="android:allowBackup, android:label"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"

配置Flutter项目

增加依赖库引用pubspec.yaml

  umeng_common_sdk: 1.2.3
  umeng_push_sdk:
    path: plugins/umeng_push_sdk-1.0.1  

这里按照官方文档直接下载的sdk压缩包。发布在pub.dev上的包未作验证。

UmengCommonSdk.initCommon('android key', 'ios key', 'dev');
UmengCommonSdk.setPageCollectionModeManual();
UmengPushSdk.register();
await UmengPushSdk.setPushEnable(true);
var registeredId = await UmengPushSdk.getRegisteredId();
print('registeredId ${registeredId}');

在终端中得到registeredId 后就可以在友盟推送的后台验证是否配置成功。


参考资料
https://developer.umeng.com/s...
https://developer.umeng.com/d...
https://developer.umeng.com/d...

03-05 21:02