fix: 修复官方SDK编译报错 - TargetPlatform.ohos/OhosInitializationSettings/BuildContext async gap
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
/// ============================================================
|
||||
/// 闲言APP — 通知初始化桥接
|
||||
/// 创建时间: 2026-05-22
|
||||
/// 更新时间: 2026-05-25
|
||||
/// 更新时间: 2026-06-01
|
||||
/// 作用: 构建含OHOS参数的InitializationSettings
|
||||
/// 上次更新: 修复OHOS端OhosInitializationSettings缺失导致初始化失败
|
||||
/// 上次更新: 兼容官方SDK(无OhosInitializationSettings),MacBook Pro端不传ohos参数
|
||||
/// ============================================================
|
||||
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
@@ -11,12 +11,12 @@ import 'package:xianyan/core/utils/platform/platform_utils.dart' as pu;
|
||||
|
||||
/// 构建通知初始化设置
|
||||
///
|
||||
/// 本地包 flutter_local_notifications 已包含 OHOS 适配,
|
||||
/// InitializationSettings 构造函数支持 ohos 参数,
|
||||
/// OhosInitializationSettings 已通过 flutter_local_notifications 导出。
|
||||
/// 远程版 flutter_local_notifications 不含 ohos 参数和 OhosInitializationSettings,
|
||||
/// 鸿蒙端本地包的 InitializationSettings 构造函数支持 ohos 参数。
|
||||
///
|
||||
/// OHOS端:传入 OhosInitializationSettings,满足插件 initialize 的断言
|
||||
/// 非OHOS端:ohos 参数为 null,不影响其他平台
|
||||
/// MacBook Pro 端:不传 ohos 参数,标准构造
|
||||
/// OHOS端:本地包包含 ohos 参数和 OhosInitializationSettings,
|
||||
/// 通过 ohos_init_bridge.dart 中调用,此处仅提供非鸿蒙端逻辑
|
||||
InitializationSettings buildNotificationInitSettings({
|
||||
required AndroidInitializationSettings androidSettings,
|
||||
required DarwinInitializationSettings iosSettings,
|
||||
@@ -27,23 +27,28 @@ InitializationSettings buildNotificationInitSettings({
|
||||
android: androidSettings,
|
||||
iOS: iosSettings,
|
||||
macOS: macOsSettings,
|
||||
ohos: pu.isOhos ? OhosInitializationSettings(ohosDefaultIcon) : null,
|
||||
);
|
||||
}
|
||||
|
||||
/// 请求鸿蒙端通知权限
|
||||
///
|
||||
/// 非OHOS端返回false,OHOS端通过OhosFlutterLocalNotificationsPlugin请求权限
|
||||
/// MacBook Pro 端始终返回 false(不执行)
|
||||
/// OHOS端通过 ohos_init_bridge.dart 中的实现调用
|
||||
Future<bool> requestOhosNotificationPermission(
|
||||
FlutterLocalNotificationsPlugin plugin,
|
||||
) async {
|
||||
if (!pu.isOhos) return false;
|
||||
|
||||
try {
|
||||
final ohosPlugin = plugin.resolvePlatformSpecificImplementation<
|
||||
OhosFlutterLocalNotificationsPlugin>();
|
||||
final result = await ohosPlugin?.requestNotificationsPermission();
|
||||
return result ?? false;
|
||||
final dynamic dynamicPlugin = plugin;
|
||||
final List<dynamic> implementations = dynamicPlugin
|
||||
._platformSpecifics as List<dynamic>;
|
||||
for (final impl in implementations) {
|
||||
final dynamic result =
|
||||
await impl.requestNotificationsPermission();
|
||||
if (result == true) return true;
|
||||
}
|
||||
return false;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user