2024-01-24 18:44:34 +08:00
|
|
|
|
import 'package:flutter/services.dart';
|
2024-06-04 15:48:39 +08:00
|
|
|
|
import 'package:star_lock/flavors.dart';
|
2025-11-29 14:45:42 +08:00
|
|
|
|
import 'package:star_lock/tools/push/message_management.dart';
|
2025-02-13 10:56:24 +08:00
|
|
|
|
import 'package:star_lock/tools/push/xs_jPhush.dart';
|
2024-01-24 18:44:34 +08:00
|
|
|
|
|
2024-04-26 15:38:59 +08:00
|
|
|
|
import '../app_settings/app_settings.dart';
|
|
|
|
|
|
|
2024-01-24 18:44:34 +08:00
|
|
|
|
///原生交互配置
|
2024-05-22 15:23:44 +08:00
|
|
|
|
class NativeInteractionConfig {
|
2024-03-09 17:17:38 +08:00
|
|
|
|
static String methodSendChannel = 'starLockFlutterSend';
|
|
|
|
|
|
static String receiveEventChannel = 'starLockFlutterReceive';
|
2025-11-29 14:45:42 +08:00
|
|
|
|
static String methodPushChannel = 'starLockFlutterPushCache';
|
2024-01-24 18:44:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///原生交互flutter向原生发送消息
|
2024-03-09 17:17:38 +08:00
|
|
|
|
typedef BlockBlueStatus = void Function(String status);
|
2024-05-22 15:23:44 +08:00
|
|
|
|
|
|
|
|
|
|
class NativeInteractionTool {
|
2025-11-29 14:45:42 +08:00
|
|
|
|
MethodChannel? _pushCacheChannel;
|
2024-05-22 15:23:44 +08:00
|
|
|
|
MethodChannel sendChannel =
|
|
|
|
|
|
MethodChannel(NativeInteractionConfig.methodSendChannel);
|
|
|
|
|
|
MethodChannel receiveChannel =
|
|
|
|
|
|
MethodChannel(NativeInteractionConfig.receiveEventChannel);
|
2024-01-24 18:44:34 +08:00
|
|
|
|
|
2024-03-09 17:17:38 +08:00
|
|
|
|
///加载原生分享
|
2024-05-22 15:23:44 +08:00
|
|
|
|
void loadNativeShare({required String shareText}) {
|
2024-06-04 15:48:39 +08:00
|
|
|
|
final String urlToShare = '${F.apiPrefix}/apps';
|
2024-06-21 09:28:25 +08:00
|
|
|
|
sendChannel.invokeMethod('loadNativeShare',
|
|
|
|
|
|
<String, String>{'shareText': shareText, 'urlToShare': urlToShare});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///加载原生文件分享
|
|
|
|
|
|
void loadNativeFileShare({required String shareText}) {
|
|
|
|
|
|
sendChannel.invokeMethod('loadNativeShare',
|
|
|
|
|
|
<String, String>{'shareText': shareText, 'urlToShare': 'fileShare'});
|
2024-03-09 17:17:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///获取设备蓝牙状态
|
2024-05-22 15:23:44 +08:00
|
|
|
|
void sendGetBlueStatus() {
|
2024-03-09 17:17:38 +08:00
|
|
|
|
sendChannel.invokeMethod('sendGetBlueStatus');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///获取设备蓝牙是否打开
|
2024-05-22 15:23:44 +08:00
|
|
|
|
void receiveChannelBlueIsOnEvent(BlockBlueStatus blockBlueStatus) {
|
2024-03-09 17:17:38 +08:00
|
|
|
|
receiveChannel.setMethodCallHandler((MethodCall call) async {
|
2024-08-19 15:24:14 +08:00
|
|
|
|
// AppLog.log('收到原生发送的信息call.method: ${call.method} call.arguments:${call.arguments}');
|
2024-03-09 17:17:38 +08:00
|
|
|
|
switch (call.method) {
|
|
|
|
|
|
case 'getBlueStatus':
|
|
|
|
|
|
// 获取设备蓝牙开启/关闭状态
|
|
|
|
|
|
final String message = call.arguments;
|
|
|
|
|
|
blockBlueStatus(message);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw MissingPluginException();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2024-01-24 18:44:34 +08:00
|
|
|
|
}
|
2025-02-13 10:56:24 +08:00
|
|
|
|
|
2025-11-29 14:45:42 +08:00
|
|
|
|
/// 初始化推送接收(前台/后台切入场景)
|
|
|
|
|
|
void setupPushReceiver() {
|
|
|
|
|
|
receiveChannel.setMethodCallHandler((MethodCall call) async {
|
|
|
|
|
|
switch (call.method) {
|
|
|
|
|
|
case 'receivePush':
|
|
|
|
|
|
final Map<dynamic, dynamic> data = call.arguments;
|
|
|
|
|
|
try {
|
|
|
|
|
|
final Map<String, dynamic> push = Map<String, dynamic>.from(data);
|
|
|
|
|
|
print('收到原生 receivePush:$push');
|
|
|
|
|
|
MessageManagement.shuntingBus(push);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
print('NativeInteractionTool.setupPushReceiver 解析失败:$e');
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'getBlueStatus':
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw MissingPluginException();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 获取待处理的推送消息
|
|
|
|
|
|
Future<Map<String, dynamic>?> getPendingPush() async {
|
|
|
|
|
|
_pushCacheChannel ??= MethodChannel(NativeInteractionConfig.methodPushChannel);
|
|
|
|
|
|
print('进入getPendingPush');
|
|
|
|
|
|
try {
|
|
|
|
|
|
final Map<dynamic, dynamic>? data = await _pushCacheChannel!.invokeMethod('getPendingPush');
|
|
|
|
|
|
return data != null ? Map<String, dynamic>.from(data) : null;
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
print("获取缓存推送消息失败: '${e.toString()}'.");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-13 10:56:24 +08:00
|
|
|
|
Future<String?> 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-01-24 18:44:34 +08:00
|
|
|
|
}
|