From ef8aa255e236051c37afa22d2ed19e2bd3d6a7cd Mon Sep 17 00:00:00 2001 From: Liuyf Date: Thu, 13 Feb 2025 09:38:50 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dandroid=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E8=BF=9B=E5=85=A5=E9=94=81=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E9=A1=B5=E9=9D=A2=E5=AE=9A=E4=BD=8D=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E9=9C=80=E8=A6=81=E9=80=80=E5=87=BA=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=E6=89=8D=E8=83=BD=E6=AD=A3=E5=B8=B8=E9=82=A3?= =?UTF-8?q?=E4=B8=AA=E8=8E=B7=E5=8F=96=E5=AE=9A=E4=BD=8D=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/app/src/main/AndroidManifest.xml | 2 + .../gaode/lockAddressGaoDe_logic.dart | 51 --- .../gaode/lockAddressGaoDe_page.dart | 413 ++++++++++-------- pubspec.lock | 30 +- 4 files changed, 228 insertions(+), 268 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 03314074..db2e332c 100755 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -10,6 +10,8 @@ + + diff --git a/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_logic.dart b/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_logic.dart index bc55b983..c645dad7 100755 --- a/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_logic.dart +++ b/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_logic.dart @@ -1,8 +1,3 @@ - - -import 'package:amap_flutter_location/amap_flutter_location.dart'; -import 'package:amap_flutter_location/amap_location_option.dart'; -import 'package:permission_handler/permission_handler.dart'; import 'package:star_lock/tools/baseGetXController.dart'; import 'lockAddressGaoDe_state.dart'; @@ -10,46 +5,6 @@ import 'lockAddressGaoDe_state.dart'; class LockAddressGaoDeLogic extends BaseGetXController{ LockAddressGaoDeState state = LockAddressGaoDeState(); - // Future requestPermission() async { - // final status = await Permission.location.request(); - // AppLog.log("Permission.location.request()=status:$status"); - // // state.permissionStatus = status; - // switch (status) { - // case PermissionStatus.denied: - // AppLog.log("拒绝"); - // break; - // case PermissionStatus.granted: - // requestLocation(); - // break; - // case PermissionStatus.limited: - // AppLog.log("限制"); - // break; - // default: - // AppLog.log("其他状态"); - // requestLocation(); - // break; - // } - // } - // - // Future requestLocation() async { - // state.location = AMapFlutterLocation() - // ..setLocationOption(AMapLocationOption()) - // ..onLocationChanged().listen((event) { - // AppLog.log("listenLocationChanged$event"); - // state.latitude.value = double.parse(event['latitude'] as String); - // state.longitude.value = double.parse(event['longitude'] as String); - // if (state.latitude.value != 0 && state.longitude.value != 0) { - // // widget.callback(event); - // state.addressInfo.value = event; - // // currentLocation = CameraPosition( - // // target: LatLng(latitude, longitude), - // // zoom: 10, - // // ); - // } - // }) - // ..startLocation(); - // } - void pushAddAction(){ } @@ -63,12 +18,6 @@ class LockAddressGaoDeLogic extends BaseGetXController{ @override void onInit() { super.onInit(); - - // AMapFlutterLocation.updatePrivacyAgree(true); - // AMapFlutterLocation.updatePrivacyShow(true, true); - // AMapFlutterLocation.setApiKey("11d49b3f4fc09c04a02bbb7500925ba2", "883a3355d2d77c2fdc2667030dc97ffe"); - // - // requestPermission(); } @override diff --git a/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_page.dart b/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_page.dart index ce2f6f09..e219a237 100755 --- a/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_page.dart +++ b/lib/mine/addLock/lockAddress/gaode/lockAddressGaoDe_page.dart @@ -14,9 +14,7 @@ import 'package:star_lock/widget/permission/permission_dialog.dart'; import '../../../../appRouters.dart'; import '../../../../app_settings/app_colors.dart'; -import '../../../../blue/blue_manage.dart'; import '../../../../flavors.dart'; -import '../../../../tools/appRouteObserver.dart'; import '../../../../tools/titleAppBar.dart'; import 'lockAddressGaoDe_logic.dart'; @@ -30,68 +28,139 @@ class LockAddressGaoDePage extends StatefulWidget { class _LockAddressGaoDePageState extends State with RouteAware { final LockAddressGaoDeLogic logic = Get.put(LockAddressGaoDeLogic()); - final LockAddressGaoDeState state = Get - .find() - .state; - - // 高德地图 - static AMapApiKey amapApiKeys = - AMapApiKey(iosKey: F.aMapKey.iosKey, androidKey: F.aMapKey.androidKey); - + final LockAddressGaoDeState state = Get.find().state; AMapController? mapController; - AMapFlutterLocation location = AMapFlutterLocation(); + Map? _addressInfo; - PermissionStatus? permissionStatus; - Map? addressInfo; + late StreamSubscription> _locationListener; + + AMapFlutterLocation _amapLocationPlugin = AMapFlutterLocation(); @override void initState() { super.initState(); - AMapFlutterLocation.updatePrivacyAgree(true); + + /// 设置是否已经包含高德隐私政策并弹窗展示显示用户查看,如果未包含或者没有弹窗展示,高德定位SDK将不会工作 + /// + /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy + /// 必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意 + /// + /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy + /// + /// [hasContains] 隐私声明中是否包含高德隐私政策说明 + /// + /// [hasShow] 隐私权政策是否弹窗展示告知用户 AMapFlutterLocation.updatePrivacyShow(true, true); + + /// 设置是否已经取得用户同意,如果未取得用户同意,高德定位SDK将不会工作 + /// + /// 高德SDK合规使用方案请参考官网地址:https://lbs.amap.com/news/sdkhgsy + /// + /// 必须保证在调用定位功能之前调用, 建议首次启动App时弹出《隐私政策》并取得用户同意 + /// + /// [hasAgree] 隐私权政策是否已经取得用户同意 + AMapFlutterLocation.updatePrivacyAgree(true); AMapFlutterLocation.setApiKey(F.aMapKey.androidKey, F.aMapKey.iosKey); + + /// 动态申请定位权限 requestPermission(); - } + ///设置Android和iOS的apiKey
+ /// + /// 定位Flutter插件提供了单独的设置ApiKey的接口, + /// 使用接口的优先级高于通过Native配置ApiKey的优先级(通过Api接口配置后,通过Native配置文件设置的key将不生效), + /// 使用时可根据实际情况决定使用哪种方式 + /// + ///key的申请请参考高德开放平台官网说明
+ /// + ///Android: https://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key + /// + ///iOS: https://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key + // AMapFlutterLocation.setApiKey( + // "anroid ApiKey", "ios ApiKey"); - Future requestPermission() async { - final bool status = await PermissionDialog.request(Permission.location); + ///iOS 获取native精度类型 if (Platform.isIOS) { - _setLocationOption(); - requestIOSLocation(); + requestAccuracyAuthorization(); } - if (!Platform.isIOS || status) { - _setLocationOption(); - requestAndroidLocation(); - location.startLocation(); - } - } + ///注册定位结果监听 + _locationListener = _amapLocationPlugin + .onLocationChanged() + .listen((Map result) { + setState(() { + _addressInfo = result; + }); + }); - Future requestAndroidLocation() async { - location.onLocationChanged().listen((Map event) { - if (event.isNotEmpty) { - setState(() { - addressInfo = event; - }); - location.stopLocation(); - } + WidgetsBinding.instance.addPostFrameCallback((_) { + _startLocation(); }); } - Future requestIOSLocation() async { - location.setLocationOption(AMapLocationOption()); - location.onLocationChanged().listen((Map event) { - // AppLog.log("listenLocationChanged$event"); - // EasyLoading.dismiss(); - if (event.isNotEmpty) { - setState(() { - addressInfo = event; - }); - // location.stopLocation(); - } - }); - location.startLocation(); + @override + void dispose() { + super.dispose(); + + ///移除定位监听 + _locationListener.cancel(); + + ///销毁定位 + _amapLocationPlugin.destroy(); + } + + ///设置定位参数 + void _setLocationOption() { + AMapLocationOption locationOption = new AMapLocationOption(); + + ///是否单次定位 + locationOption.onceLocation = false; + + ///是否需要返回逆地理信息 + locationOption.needAddress = true; + + ///逆地理信息的语言类型 + locationOption.geoLanguage = GeoLanguage.DEFAULT; + + locationOption.desiredLocationAccuracyAuthorizationMode = + AMapLocationAccuracyAuthorizationMode.ReduceAccuracy; + + locationOption.fullAccuracyPurposeKey = "AMapLocationScene"; + + ///设置Android端连续定位的定位间隔 + locationOption.locationInterval = 2000; + + ///设置Android端的定位模式
+ ///可选值:
+ ///
  • [AMapLocationMode.Battery_Saving]
  • + ///
  • [AMapLocationMode.Device_Sensors]
  • + ///
  • [AMapLocationMode.Hight_Accuracy]
  • + locationOption.locationMode = AMapLocationMode.Hight_Accuracy; + + ///设置iOS端的定位最小更新距离
    + locationOption.distanceFilter = -1; + + ///设置iOS端期望的定位精度 + /// 可选值:
    + ///
  • [DesiredAccuracy.Best] 最高精度
  • + ///
  • [DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度
  • + ///
  • [DesiredAccuracy.NearestTenMeters] 10米
  • + ///
  • [DesiredAccuracy.Kilometer] 1000米
  • + ///
  • [DesiredAccuracy.ThreeKilometers] 3000米
  • + locationOption.desiredAccuracy = DesiredAccuracy.Best; + + ///设置iOS端是否允许系统暂停定位 + locationOption.pausesLocationUpdatesAutomatically = false; + + ///将定位参数设置给定位插件 + _amapLocationPlugin.setLocationOption(locationOption); + } + + ///开始定位 + void _startLocation() { + ///开始定位之前设置定位参数 + _setLocationOption(); + _amapLocationPlugin.startLocation(); } @override @@ -115,90 +184,93 @@ class _LockAddressGaoDePageState extends State ), ), SizedBox( - child: (addressInfo != null && addressInfo!.containsKey('latitude')) + child: (_addressInfo != null && + _addressInfo!.containsKey('latitude')) ? Column( - children: [ - SizedBox( - height: 1.sw / 5 * 4, - width: 1.sw, - child: AMapWidget( - apiKey: amapApiKeys, - // 初始化地图中心 - initialCameraPosition: CameraPosition( - target: LatLng( - double.parse( - addressInfo!['latitude'].toString()), - double.parse( - addressInfo!['longitude'].toString())), - zoom: 10.0, - ), - //定位小蓝点 - myLocationStyleOptions: MyLocationStyleOptions( - true, - ), - // 普通地图normal,卫星地图satellite,夜间视图night,导航视图 navi,公交视图bus, - mapType: MapType.normal, - // 缩放级别范围 - minMaxZoomPreference: - const MinMaxZoomPreference(3, 20), - // 隐私政策包含高德 必须填写 - privacyStatement: const AMapPrivacyStatement( - hasAgree: true, hasContains: true, hasShow: true), - // 地图创建成功时返回AMapController - onMapCreated: (AMapController controller) { - mapController = controller; - }, - onLocationChanged: (AMapLocation location) { - print("onLocationChanged ${location}"); - }, - ), - ), - Container( - // color: Colors.red, - margin: - EdgeInsets.only(left: 25.w, top: 20.h, right: 25.w), - child: Row( - mainAxisAlignment: MainAxisAlignment.start, children: [ - Flexible( - child: Text('检查以确保以下地址是正确的'.tr, - style: TextStyle(fontSize: 24.sp))), - ], - ), - ), - // SizedBox(height: 20.h), - Container( - // color: Colors.red, - // height: 45.h, - margin: EdgeInsets.only( - left: 25.w, top: 20.h, right: 25.w), - child: Column( - children: [ - Row( + SizedBox( + height: 1.sw / 5 * 4, + width: 1.sw, + child: AMapWidget( + apiKey: AMapApiKey( + iosKey: F.aMapKey.iosKey, + androidKey: F.aMapKey.androidKey), + // 初始化地图中心 + initialCameraPosition: CameraPosition( + target: LatLng( + double.parse( + _addressInfo!['latitude'].toString()), + double.parse( + _addressInfo!['longitude'].toString())), + zoom: 10.0, + ), + //定位小蓝点 + myLocationStyleOptions: MyLocationStyleOptions( + true, + ), + // 普通地图normal,卫星地图satellite,夜间视图night,导航视图 navi,公交视图bus, + mapType: MapType.normal, + // 缩放级别范围 + minMaxZoomPreference: + const MinMaxZoomPreference(3, 20), + // 隐私政策包含高德 必须填写 + privacyStatement: const AMapPrivacyStatement( + hasAgree: true, hasContains: true, hasShow: true), + // 地图创建成功时返回AMapController + onMapCreated: (AMapController controller) { + mapController = controller; + }, + onLocationChanged: (AMapLocation location) { + print("onLocationChanged ${location}"); + }, + ), + ), + Container( + // color: Colors.red, + margin: + EdgeInsets.only(left: 25.w, top: 20.h, right: 25.w), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, children: [ - Expanded( - child: Text( - addressInfo!['address'].toString() ?? - '', - style: const TextStyle( - color: Colors.grey, - fontSize: 16, - fontWeight: FontWeight.w500, - overflow: TextOverflow.clip))), + Flexible( + child: Text('检查以确保以下地址是正确的'.tr, + style: TextStyle(fontSize: 24.sp))), ], ), - SizedBox(height: 5.h), - Container( - height: 1.h, - color: AppColors.mainColor, - ), - ], - )), - ], - ) + ), + // SizedBox(height: 20.h), + Container( + // color: Colors.red, + // height: 45.h, + margin: EdgeInsets.only( + left: 25.w, top: 20.h, right: 25.w), + child: Column( + children: [ + Row( + children: [ + Expanded( + child: Text( + _addressInfo!['address'].toString() ?? + '', + style: const TextStyle( + color: Colors.grey, + fontSize: 16, + fontWeight: FontWeight.w500, + overflow: TextOverflow.clip))), + ], + ), + SizedBox(height: 5.h), + Container( + height: 1.h, + color: AppColors.mainColor, + ), + ], + )), + ], + ) : SizedBox( - height: 1.sw / 5 * 4 + 65.h * 2, - child: Center(child: Text('地图加载中,请稍候。。。'.tr))), + height: 1.sw / 5 * 4 + 65.h * 2, + child: Center(child: Text('地图加载中,请稍候。。。'.tr))), ), SizedBox(height: 200.h), Row( @@ -227,13 +299,13 @@ class _LockAddressGaoDePageState extends State style: TextStyle(color: Colors.black, fontSize: 24.sp), ), onPressed: () { - if (addressInfo!.isEmpty) { + if (_addressInfo!.isEmpty) { logic.showToast('还未获取到位置信息哦,请耐心等待一下!'.tr); return; } Get.toNamed(Routers.saveLockPage, arguments: { - 'addressInfo': addressInfo, + 'addressInfo': _addressInfo, 'pwdTimestamp': state.pwdTimestamp.value, 'lockInfo': state.lockInfo, 'featureValue': state.featureValue, @@ -251,70 +323,31 @@ class _LockAddressGaoDePageState extends State ); } - ///设置定位参数 - void _setLocationOption() { - AMapLocationOption locationOption = AMapLocationOption(); - - ///是否单次定位 - locationOption.onceLocation = true; - - ///是否需要返回逆地理信息 - locationOption.needAddress = true; - - ///逆地理信息的语言类型 - locationOption.geoLanguage = GeoLanguage.DEFAULT; - - locationOption.desiredLocationAccuracyAuthorizationMode = - AMapLocationAccuracyAuthorizationMode.ReduceAccuracy; - - locationOption.fullAccuracyPurposeKey = 'AMapLocationScene'; - - ///设置Android端连续定位的定位间隔 - locationOption.locationInterval = 2000; - - ///设置Android端的定位模式
    - ///可选值:
    - ///
  • [AMapLocationMode.Battery_Saving]
  • - ///
  • [AMapLocationMode.Device_Sensors]
  • - ///
  • [AMapLocationMode.Hight_Accuracy]
  • - locationOption.locationMode = AMapLocationMode.Battery_Saving; - - ///设置iOS端的定位最小更新距离
    - locationOption.distanceFilter = -1; - - ///设置iOS端期望的定位精度 - /// 可选值:
    - ///
  • [DesiredAccuracy.Best] 最高精度
  • - ///
  • [DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度
  • - ///
  • [DesiredAccuracy.NearestTenMeters] 10米
  • - ///
  • [DesiredAccuracy.Kilometer] 1000米
  • - ///
  • [DesiredAccuracy.ThreeKilometers] 3000米
  • - locationOption.desiredAccuracy = DesiredAccuracy.BestForNavigation; - - ///设置iOS端是否允许系统暂停定位 - locationOption.pausesLocationUpdatesAutomatically = false; - - ///将定位参数设置给定位插件 - location.setLocationOption(locationOption); + ///获取iOS native的accuracyAuthorization类型 + void requestAccuracyAuthorization() async { + AMapAccuracyAuthorization currentAccuracyAuthorization = + await _amapLocationPlugin.getSystemAccuracyAuthorization(); + if (currentAccuracyAuthorization == + AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) { + print("精确定位类型"); + } else if (currentAccuracyAuthorization == + AMapAccuracyAuthorization.AMapAccuracyAuthorizationReducedAccuracy) { + print("模糊定位类型"); + } else { + print("未知定位类型"); + } } - @override - void didChangeDependencies() { - super.didChangeDependencies(); - - /// 路由订阅 - AppRouteObserver().routeObserver.subscribe(this, ModalRoute.of(context)!); - } - - @override - void dispose() { - AppRouteObserver().routeObserver.unsubscribe(this); - super.dispose(); - - BlueManage().disconnect(); - - location.stopLocation(); - location.destroy(); + /// 动态申请定位权限 + void requestPermission() async { + // 申请权限 + final bool hasLocationPermission = + await PermissionDialog.request(Permission.location); + if (hasLocationPermission) { + print("定位权限申请通过"); + } else { + print("定位权限申请不通过"); + } } /// 从上级界面进入 当前界面即将出现 @@ -340,7 +373,7 @@ class _LockAddressGaoDePageState extends State void didPushNext() { super.didPushNext(); - location.stopLocation(); - location.destroy(); + _amapLocationPlugin.stopLocation(); + _amapLocationPlugin.destroy(); } } diff --git a/pubspec.lock b/pubspec.lock index 58763db6..1f831260 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -73,7 +73,7 @@ packages: source: hosted version: "2.6.0" asn1lib: - dependency: "direct main" + dependency: transitive description: name: asn1lib sha256: "4bae5ae63e6d6dd17c4aac8086f3dec26c0236f6a0f03416c6c19d830c367cf5" @@ -329,21 +329,13 @@ packages: source: hosted version: "0.3.4+2" crypto: - dependency: "direct main" + dependency: transitive description: name: crypto sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab url: "https://pub.dev" source: hosted version: "3.0.3" - cryptography: - dependency: "direct main" - description: - name: cryptography - sha256: d146b76d33d94548cf035233fbc2f4338c1242fa119013bead807d033fc4ae05 - url: "https://pub.dev" - source: hosted - version: "2.7.0" csslib: dependency: transitive description: @@ -417,7 +409,7 @@ packages: source: hosted version: "3.4.0" encrypt: - dependency: "direct main" + dependency: transitive description: name: encrypt sha256: "62d9aa4670cc2a8798bab89b39fc71b6dfbacf615de6cf5001fb39f7e4a996a2" @@ -456,14 +448,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.0" - fast_rsa: - dependency: "direct main" - description: - name: fast_rsa - sha256: f2bc97888877e93242a4eb1ee8d88ed77b593f02ad6a1b0ae188945809d52ca3 - url: "https://pub.dev" - source: hosted - version: "3.6.6" ffi: dependency: "direct main" description: @@ -576,14 +560,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.1" - flat_buffers: - dependency: transitive - description: - name: flat_buffers - sha256: "23e2ced0d8e8ecdffbd9f267f49a668c74438393b9acaeac1c724123e3764263" - url: "https://pub.dev" - source: hosted - version: "2.0.5" flustars: dependency: "direct main" description: From 481e8fff066dad3b995a62b7bc18965f594bcf4d Mon Sep 17 00:00:00 2001 From: Liuyf Date: Thu, 13 Feb 2025 09:54:29 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix=EF=BC=9A=E6=9E=84=E5=BB=BAandroid?= =?UTF-8?q?=E6=97=B6=20=E6=B3=A8=E5=85=A5key=20=E6=B2=A1=E6=9C=89=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=80=BC=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/app/build.gradle | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index ce046057..41903854 100755 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -248,22 +248,22 @@ android { JPUSH_PKGNAME : "这里不重要,在口味配置", //JPush 上注册的包名对应的 Appkey. // JPUSH_APPKEY : "7ff37d174c1a568a89e98dad",//--skyAppKey -// JPUSH_APPKEY : "251fc8074820d122b6de58d2",//--鑫泓佳AppKey -// JPUSH_CHANNEL : "flutter_channel", + JPUSH_APPKEY : "default",//--鑫泓佳AppKey + JPUSH_CHANNEL : "default", //若不集成厂商通道,可直接跳过以下配置 //以下为sky的配置 // XIAOMI_APPID : "MI-2882303761520287291", // XIAOMI_APPKEY : "MI-5352028744291", //以下均为鑫泓佳的配置 -// XIAOMI_APPID : "MI-2882303761520314939", -// XIAOMI_APPKEY : "MI-5312031456939", -// OPPO_APPKEY : "OP-47f668c9943248118502aa58d066393b", -// OPPO_APPID : "OP-31726001", -// OPPO_APPSECRET: "OP-05723986bba64183a71530b496922450", -// VIVO_APPKEY : "75fe8e570425b714e08d0390b14797cb", -// VIVO_APPID : "105752244", -// HONOR_APPID : "104458196", + XIAOMI_APPID : "default", + XIAOMI_APPKEY : "default", + OPPO_APPKEY : "default", + OPPO_APPID : "default", + OPPO_APPSECRET: "default", + VIVO_APPKEY : "default", + VIVO_APPID : "default", + HONOR_APPID : "default", ] splits { abi { From 2f41195d8befc89d2ba3b15d0f9bdb9682c9d693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDaisyWu=E2=80=9D?= <“18682150237@163.com”> Date: Thu, 13 Feb 2025 10:56:24 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DiOS=E6=94=B6?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=E6=8E=A8=E9=80=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/Runner/AppDelegate.m | 64 ++++++++++++++++++- ios/Runner/XSFlutterManager.m | 5 ++ .../handle/impl/udp_talk_request_handler.dart | 9 ++- lib/tools/NativeInteractionTool.dart | 12 ++++ lib/tools/push/xs_jPhush.dart | 21 ++++-- 5 files changed, 97 insertions(+), 14 deletions(-) diff --git a/ios/Runner/AppDelegate.m b/ios/Runner/AppDelegate.m index c6d3389b..76d97654 100755 --- a/ios/Runner/AppDelegate.m +++ b/ios/Runner/AppDelegate.m @@ -9,6 +9,7 @@ @interface AppDelegate() +@property (nonatomic, strong) FlutterMethodChannel *methodChannel; @end @@ -20,11 +21,24 @@ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // [UMConfigure initWithAppkey:@"671244ae80464b33f6df9646" channel:@"Product"]; + + + //Required + //notice: 3.0.0 及以后版本注册可以这样写,也可以继续用之前的注册方式 + JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; + entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings; + if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { + // 可以添加自定义 categories + // NSSet *categories for iOS10 or later + // NSSet *categories for iOS8 and iOS9 + } + [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; + XSFlutterManager *VC = [[XSFlutterManager alloc] init]; self.window.rootViewController = VC; [self.window makeKeyAndVisible]; - + return YES; } @@ -64,7 +78,7 @@ */ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { - //sdk注册DeviceToken + /// Required - 注册 DeviceToken [JPUSHService registerDeviceToken:deviceToken]; } @@ -72,7 +86,8 @@ * 苹果推送注册失败回调 */ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { - NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error); + //Optional + NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } /** @@ -96,6 +111,49 @@ NSLog(@"Receive message title: %@, content: %@.", title, body); } +# pragma mark- JPUSHRegisterDelegate + +// iOS 12 Support +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{ + if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + //从通知界面直接进入应用 + }else{ + //从通知设置界面进入应用 + } +} + +// iOS 10 Support +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { + // Required + NSDictionary * userInfo = notification.request.content.userInfo; + if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + [JPUSHService handleRemoteNotification:userInfo]; + } + completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置 +} + +// iOS 10 Support +- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { + // Required + NSDictionary * userInfo = response.notification.request.content.userInfo; + if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + [JPUSHService handleRemoteNotification:userInfo]; + } + completionHandler(); // 系统要求执行这个方法 +} + +- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { + + // Required, iOS 7 Support + [JPUSHService handleRemoteNotification:userInfo]; + completionHandler(UIBackgroundFetchResultNewData); +} + +- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { + + // Required, For systems with less than or equal to iOS 6 + [JPUSHService handleRemoteNotification:userInfo]; +} diff --git a/ios/Runner/XSFlutterManager.m b/ios/Runner/XSFlutterManager.m index d53ee425..c2ce6138 100755 --- a/ios/Runner/XSFlutterManager.m +++ b/ios/Runner/XSFlutterManager.m @@ -64,6 +64,7 @@ activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) { if (completed) { NSLog(@"completed"); + // 分享成功 } else { NSLog(@"canceled"); @@ -72,6 +73,10 @@ }; result(@"push返回到flutter"); } + if ([method isEqualToString:@"getBundleIdentifier"]) { + NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; + result(bundleIdentifier); // 返回 Bundle Identifier + } }]; [GeneratedPluginRegistrant registerWithRegistry:self]; diff --git a/lib/talk/starChart/handle/impl/udp_talk_request_handler.dart b/lib/talk/starChart/handle/impl/udp_talk_request_handler.dart index 9fb5ce97..f339dd9a 100644 --- a/lib/talk/starChart/handle/impl/udp_talk_request_handler.dart +++ b/lib/talk/starChart/handle/impl/udp_talk_request_handler.dart @@ -14,7 +14,6 @@ import 'package:star_lock/talk/starChart/proto/gateway_reset.pb.dart'; import 'package:star_lock/talk/starChart/proto/generic.pb.dart'; import 'package:star_lock/talk/starChart/proto/talk_expect.pb.dart'; import 'package:star_lock/talk/starChart/proto/talk_request.pb.dart'; -import 'package:star_lock/tools/push/xs_jPhush.dart'; import 'package:star_lock/tools/storage.dart'; class UdpTalkRequestHandler extends ScpMessageBaseHandle @@ -79,7 +78,9 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle // 收到来电请求时进行本地通知 Future _showTalkRequestNotification( {required String talkObjectName}) async { - final Map message = { + /** + * + final Map message = { 'platform': 'all', 'audience': 'all', 'notification': >{ @@ -95,8 +96,7 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle }; XSJPushProvider().showCustomNotification(message); - - /* + */ const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails('1', 'flutter_channel', importance: Importance.max, @@ -113,7 +113,6 @@ class UdpTalkRequestHandler extends ScpMessageBaseHandle await flutterLocalNotificationsPlugin.show(0, '呼叫提醒'.tr, '${'收到来自'.tr}($talkObjectName)${'锁的呼叫'.tr}。', platformChannelSpecifics, payload: 'item x'); - */ } @override diff --git a/lib/tools/NativeInteractionTool.dart b/lib/tools/NativeInteractionTool.dart index d7c0b943..5326d2bf 100755 --- a/lib/tools/NativeInteractionTool.dart +++ b/lib/tools/NativeInteractionTool.dart @@ -1,5 +1,6 @@ import 'package:flutter/services.dart'; import 'package:star_lock/flavors.dart'; +import 'package:star_lock/tools/push/xs_jPhush.dart'; import '../app_settings/app_settings.dart'; @@ -52,4 +53,15 @@ class NativeInteractionTool { } }); } + + Future getBundleIdentifier() async { + try { + final String? bundleIdentifier = + await sendChannel.invokeMethod('getBundleIdentifier'); + return bundleIdentifier; + } on PlatformException catch (e) { + print("Failed to get bundle identifier: '${e.message}'."); + return null; + } + } } diff --git a/lib/tools/push/xs_jPhush.dart b/lib/tools/push/xs_jPhush.dart index 4e25400d..d21cf996 100755 --- a/lib/tools/push/xs_jPhush.dart +++ b/lib/tools/push/xs_jPhush.dart @@ -1,11 +1,13 @@ import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:jpush_flutter/jpush_flutter.dart'; import 'package:star_lock/flavors.dart'; import 'package:star_lock/mine/minePersonInfo/minePersonInfoEditAccount/minePersonInfoEditAccount/mineUnbindPhoneOrEmail_entity.dart'; import 'package:star_lock/network/api_repository.dart'; +import 'package:star_lock/tools/NativeInteractionTool.dart'; import 'package:star_lock/tools/baseGetXController.dart'; import 'package:star_lock/tools/push/message_management.dart'; import 'package:star_lock/tools/push/notification_service.dart'; @@ -15,7 +17,6 @@ import '../../app_settings/app_settings.dart'; class XSJPushProvider { final JPush jpush = JPush(); - final NotificationService _notificationService = NotificationService(); // appKey: 251fc8074820d122b6de58d2--鑫泓佳AppKey // appKey: 7ff37d174c1a568a89e98dad--sky @@ -27,6 +28,9 @@ class XSJPushProvider { } print('jPushKey ${F.jPushKey}'); + // final String? bundleIdentifier = + // await NativeInteractionTool().getBundleIdentifier(); + // print('bundleIdentifier: $bundleIdentifier'); jpush.setup( appKey: F.jPushKey, channel: 'flutter_channel', @@ -40,6 +44,10 @@ class XSJPushProvider { addJPushEventHandler(); AppLog.log('JPush initialized.'); + + final String rid = await jpush.getRegistrationID(); + print('flutter get registration id : $rid'); + pushBindDeviceID(rid); } //极光推送事件处理方法 @@ -69,11 +77,13 @@ class XSJPushProvider { final String rid = await jpush.getRegistrationID(); AppLog.log('onConnected registration id : $rid'); await Storage.setString(pushDeviceID, rid); - await pushBindDeviceID(rid, Platform.isAndroid ? 10 : 20); + await pushBindDeviceID(rid); return Future.value(); }, ); + + // Remove the incorrect addEventHandler call } Future showCustomNotification(Map message) async { @@ -86,7 +96,7 @@ class XSJPushProvider { NotificationService().showImageNotification(title, content, imageUrl); } - Future pushBindDeviceID(String deviceID, int deviceType) async { + Future pushBindDeviceID(String deviceID) async { try { if (deviceID.isEmpty) { AppLog.log('Device ID is empty.'); @@ -95,10 +105,9 @@ class XSJPushProvider { AppLog.log('onConnected registration id : $rid'); await Storage.setString(pushDeviceID, rid); deviceID = rid; - deviceType = Platform.isAndroid ? 10 : 20; } - final MineUnbindPhoneOrEmailEntity entity = - await ApiRepository.to.pushBindAppId(deviceID, deviceType); + final MineUnbindPhoneOrEmailEntity entity = await ApiRepository.to + .pushBindAppId(deviceID, Platform.isAndroid ? 10 : 20); if (entity.errorCode!.codeIsSuccessful) { AppLog.log('绑定成功'); } else {