实现推送消息触发的远程开锁请求流程,包括: 1. 新增RemoteUnlockRequestEvent和PushExtraEvent事件类型 2. 在锁详情页添加倒计时弹窗和动画效果 3. 实现Native层推送数据缓存和转发机制 4. 添加RemoteUnlockCoordinator处理应用未启动时的推送导航
105 lines
3.5 KiB
Dart
Executable File
105 lines
3.5 KiB
Dart
Executable File
import 'package:flutter/services.dart';
|
||
import 'package:star_lock/flavors.dart';
|
||
import 'package:star_lock/tools/push/message_management.dart';
|
||
import 'package:star_lock/tools/push/xs_jPhush.dart';
|
||
|
||
import '../app_settings/app_settings.dart';
|
||
|
||
///原生交互配置
|
||
class NativeInteractionConfig {
|
||
static String methodSendChannel = 'starLockFlutterSend';
|
||
static String receiveEventChannel = 'starLockFlutterReceive';
|
||
static String methodPushChannel = 'starLockFlutterPushCache';
|
||
}
|
||
|
||
///原生交互flutter向原生发送消息
|
||
typedef BlockBlueStatus = void Function(String status);
|
||
|
||
class NativeInteractionTool {
|
||
MethodChannel? _pushCacheChannel;
|
||
MethodChannel sendChannel =
|
||
MethodChannel(NativeInteractionConfig.methodSendChannel);
|
||
MethodChannel receiveChannel =
|
||
MethodChannel(NativeInteractionConfig.receiveEventChannel);
|
||
|
||
///加载原生分享
|
||
void loadNativeShare({required String shareText}) {
|
||
final String urlToShare = '${F.apiPrefix}/apps';
|
||
sendChannel.invokeMethod('loadNativeShare',
|
||
<String, String>{'shareText': shareText, 'urlToShare': urlToShare});
|
||
}
|
||
|
||
///加载原生文件分享
|
||
void loadNativeFileShare({required String shareText}) {
|
||
sendChannel.invokeMethod('loadNativeShare',
|
||
<String, String>{'shareText': shareText, 'urlToShare': 'fileShare'});
|
||
}
|
||
|
||
///获取设备蓝牙状态
|
||
void sendGetBlueStatus() {
|
||
sendChannel.invokeMethod('sendGetBlueStatus');
|
||
}
|
||
|
||
///获取设备蓝牙是否打开
|
||
void receiveChannelBlueIsOnEvent(BlockBlueStatus blockBlueStatus) {
|
||
receiveChannel.setMethodCallHandler((MethodCall call) async {
|
||
// AppLog.log('收到原生发送的信息call.method: ${call.method} call.arguments:${call.arguments}');
|
||
switch (call.method) {
|
||
case 'getBlueStatus':
|
||
// 获取设备蓝牙开启/关闭状态
|
||
final String message = call.arguments;
|
||
blockBlueStatus(message);
|
||
break;
|
||
default:
|
||
throw MissingPluginException();
|
||
}
|
||
});
|
||
}
|
||
|
||
/// 初始化推送接收(前台/后台切入场景)
|
||
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;
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
}
|