Android集成极光推送Flutter

  • Post author:
  • Post category:其他




该文章只包含Andriod的集成方式



一、Flutter文档集成步骤文档地址




极光推送客户端集成插件



二、关于集成方法总结



①、创建应用

在极光推送官方网站上创建应用获取

Appkey



②、安装

jpush_flutter

插件

在你项目的

pubspec.yaml

文件中的

dependencies

节点下添加以下代码

dependencies:
  jpush_flutter: 2.1.4

执行flutter pub get



③、配置android文件夹下的build.gradle

 defaultConfig {
        applicationId "com.example.frametrim"
        minSdkVersion 19
        targetSdkVersion 30
//        targetSdkVersion flutter.targetSdkVersion
        versionCode 1
        versionName "1.0"
        // 这里添加
        multiDexEnabled true

//极光推送开始
        ndk {
            //选择要添加的对应 cpu 类型的 .so 库。
            abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a'
        }

        manifestPlaceholders = [
                applicationName:"io.flutter.app.FlutterApplication",
                JPUSH_PKGNAME: applicationId,
                JPUSH_APPKEY : "你的Appkey", // NOTE: JPush 上注册的包名对应的 Appkey.
                JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
        ]
        //极光推送结束
    }



④、接入基础推送功能

  final JPush jpush = new JPush();
  String? debugLable = 'Unknown';



  void initState() {
    super.initState();
    initPlatformState();
  }


 Future<void> initPlatformState() async {
    String? platformVersion;

    try {
      jpush.addEventHandler(
          onReceiveNotification: (Map<String, dynamic> message) async {
            print("flutter onReceiveNotification: $message");
            setState(() {
              debugLable = "flutter onReceiveNotification: $message";
            });
          }, onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");
        setState(() {
          debugLable = "flutter onOpenNotification: $message";
        });
      }, onReceiveMessage: (Map<String, dynamic> message) async {
        print("flutter onReceiveMessage: $message");
        setState(() {
          debugLable = "flutter onReceiveMessage: $message";
        });
      }, onReceiveNotificationAuthorization:
          (Map<String, dynamic> message) async {
        print("flutter onReceiveNotificationAuthorization: $message");
        setState(() {
          debugLable = "flutter onReceiveNotificationAuthorization: $message";
        });
      });
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    jpush.setup(
      appKey: "你自己的AppKey", //你自己应用的 AppKey
      channel: "theChannel",
      production: false,
      debug: true,
    );
    jpush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true));

    // Platform messages may fail, so we use a try/catch PlatformException.
    jpush.getRegistrationID().then((rid) {
      print("flutter get registration id : $rid");
      setState(() {
        debugLable = "flutter getRegistrationID: $rid";
      });
    });

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      debugLable = platformVersion;
    });
  }



  • 中间遇到问题



    刚开始编译时候一直报错:
E:\Flutter\frametrim\android\app\src\debug\AndroidManifest.xml:27:9-33:20 Error:
	android:exported needs to be explicitly specified for element <receiver#com.jiguang.jpush.JPushEventReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
E:\Flutter\frametrim\android\app\src\debug\AndroidManifest.xml:120:9-138:20 Error:
	android:exported needs to be explicitly specified for element <receiver#cn.jpush.android.service.PushReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

后来发现导致这个问题的原因是我的targetSdkVersion 设置为31 降低到30就可以了,但是问题是我同事的设置31就没问题成功编译,我的studio就是不行,具体的原因暂时没找到。



详细的设置别名之类的功能等在开发中再做整理



版权声明:本文为wz_1992原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。