flutter开的app应用商店审核-极光推送初始化的问题

393 阅读3分钟

app开发完了准备上架各个厂家的应用商店,小米,oppo,vivo,荣耀都可以,但是在华为反馈: 在用户同意隐私政策前,您的应用应用集成的[极光] SDK获取用户信息:软件安装列表,MAC地址,ANDROID ID,不符合相关法律法规要求。直接说解决方案吧。极光工程师回复的是 JCollectionAuth.setAuth(context, false)接口在Application的onCreate方法中调用,直到用户点击同意隐私条款才能设置JCollectionAuth.setAuth(context, true),然后调用初始化接口
接下来说修改步骤 1、java修改 1.1、image.png
找到该文件夹新增这些代码 (android/app/src/main/java/com/yourcompany/yourapp/MainActivity.java)

package com.example.yourapp;
import android.os.Bundle;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;

import cn.jiguang.api.JCoreInterface;
import cn.jiguang.analytics.page.JCollectionInterface;
import cn.jpush.android.api.JPushInterface;

public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "jpush";

  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    super.configureFlutterEngine(flutterEngine); 
    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
      .setMethodCallHandler((call, result) -> {
        if (call.method.equals("agreePrivacyAndInit")) {
          JCollectionInterface.setAuth(getApplicationContext(), true);
          JPushInterface.setDebugMode(true);
          JPushInterface.init(getApplicationContext());
          result.success(null);
        } else {
          result.notImplemented();
        }
      });
  }
}

1.2、在android/app/src/main/java/com/yourcompany/yourapp/看看有没有Application.java,没有就先建一个

Application.java代码


import android.app.Application;
import cn.jiguang.analytics.page.JCollectionInterface;

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化时关闭采集
        JCollectionInterface.setAuth(this, false);
    }
}

1.3、修改 AndroidManifest.xml 注册自定义 Application 找到 android/app/src/main/AndroidManifest.xml,添加:

<application
    android:name=".MyApp"  <!-- 注意这里要写你的类名 跟Application.java文件的 class MyApp -->
    android:label="yourapp"
    ... >

1.4、在封装极光推送

import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:jverify/jverify.dart';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../tools/utils.dart';

class JPushService {
  final JPush jpush = JPush();
  final Jverify jverify = Jverify();
  final Logger logger = Logger();
  late SharedPreferences prefs;
  static const MethodChannel _channel = MethodChannel('jpush_helper');

  Future<void> initPlatformState() async {
    try {
      prefs = await SharedPreferences.getInstance();
      jpush.addEventHandler(
        // 收到通知时的回调
        onReceiveNotification: (Map<String, dynamic> message) async {
          print("flutter onReceiveNotification: $message");
        },

        // 点击通知时的回调
        onOpenNotification: (Map<String, dynamic> message) async {
          print("flutter onOpenNotification: $message");
        },

        // 收到自定义消息时的回调
        onReceiveMessage: (Map<String, dynamic> message) async {
          print("flutter onReceiveMessage: $message");
        },

        // 收到通知授权结果时的回调
        onReceiveNotificationAuthorization:
            (Map<String, dynamic> message) async {
          print("flutter onReceiveNotificationAuthorization: $message");
        },

        // 通知消息不显示时的回调
        onNotifyMessageUnShow: (Map<String, dynamic> message) async {
          print("flutter onNotifyMessageUnShow: $message");
        },

        // 应用内消息显示时的回调
        onInAppMessageShow: (Map<String, dynamic> message) async {
          print("flutter onInAppMessageShow: $message");
        },

        // 命令结果返回时的回调
        onCommandResult: (Map<String, dynamic> message) async {
          print("flutter onCommandResult: $message");
        },

        // 应用内消息点击时的回调
        onInAppMessageClick: (Map<String, dynamic> message) async {
          print("flutter onInAppMessageClick: $message");
        },

        // 设备与极光服务器连接时的回调
        onConnected: (Map<String, dynamic> message) async {
          print("flutter onConnected: $message");
        },
      );
    } on PlatformException {
      print("Failed to get platform version.");
    }
    //这个很重要自己手动打开写
    jpush.setAuth(enable: true);
    jpush.setup(
      appKey: "appKey",
      channel: "xxxxxx",
      production: false,
      debug: true,
    );
    jverify.setDebugMode(true);

    jverify.setup(
      appKey: "appKey",
      channel: "developer-default",
    );

    jpush.applyPushAuthority(
        const NotificationSettingsIOS(sound: true, alert: true, badge: true));
    jpush.getRegistrationID().then((rid) async { 
      await prefs.setString('registrationID', rid);
    });
    await Future.delayed(Duration(milliseconds: 800)); // ⏱ 可加一点延时
    jverify.isInitSuccess().then((result) {
      if (result['result'] == true) {
        logger.i("JVerify 初始化成功");
      } else {
        logger.i("JVerify 初始化失败");
      }
    });
    await isNotificationEnabled();
  }  
} 

大功告成。现在清除flutter clean清除缓存,在flutter pub get

2、Kotlin 2.1、找到该文件夹新增这些代码 (android/app/src/main/kotlin/com/yourcompany/yourapp),有MainActivityNew文件就添加一下代码。没有的话及新增一个kotlin文件代码如下:

package com.ai.tunbao
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import cn.jiguang.api.utils.JCollectionAuth
class MainActivityNew : FlutterActivity() {
    private val CHANNEL = "jpush_helper"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 禁用极光自动采集
        JCollectionAuth.setAuth(applicationContext, false)
    }
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
                call, result ->
            if (call.method == "setJCollectionAuthTrue") {
                JCollectionAuth.setAuth(applicationContext, true)
                result.success(true)
            } else {
                result.notImplemented()
            }
        }
    }
}

2.2、修改 AndroidManifest.xml 注册自定义 Application 找到 android/app/src/main/AndroidManifest.xml,添加:

<application>
<activity  
android:name=".MainActivityNew" // **主要是这一句 跟上面文件的 class MainActivityNew 一致**
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:exported="true"
    android:hardwareAccelerated="true"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait">
    <meta-data
        android:name="io.flutter.embedding.android.NormalTheme"
        android:resource="@style/NormalTheme" />
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity> 
</application>

2.3、android/build.gradle添加对应代码

allprojects {
    repositories {
        google()
        mavenCentral()
        // ✅ 添加极光仓库
        maven { url 'https://sdk.jpush.cn/gradle-plugin/' } //这句
    }
}

2.4、android/app/build.gradle添加对应代码

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id "com.google.firebase.crashlytics"
    id 'com.google.gms.google-services'
}

dependencies {
    implementation platform('com.google.firebase:firebase-bom:33.9.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'cn.jiguang.sdk:jverification:3.2.0' // 或与你插件版本匹配的 JVerify 原生库
    // ✅ 添加 JCore SDK(包含 JCollectionAuth)
    implementation 'cn.jiguang.sdk:jcore:3.3.2' // 版本建议与你 flutter jpush 插件匹配
}

剩下的跟Java一样的,在1.4的步骤来 这样就没有问题