flutter_local_notifications 在 HarmonyOS 上的使用指南
本文介绍了适用于HarmonyOS的flutter_local_notifications插件,这是一个跨平台的Flutter本地通知解决方案。主要内容包括:插件支持基本通知、定时通知、周期性通知等功能;通过Git方式引入依赖的安装方法;详细的使用示例,如初始化插件、请求权限、显示各类通知(基础通知、进度条通知、定时通知、周期性通知)以及取消通知等操作。该插件已针对鸿蒙系统进行适配,为开发者提供了
插件介绍
flutter_local_notifications 是一个功能强大的跨平台 Flutter 插件,用于在移动应用中显示本地通知。该插件已针对 HarmonyOS 平台进行了适配,允许开发者在鸿蒙系统上实现丰富的本地通知功能,包括基本通知、定时通知和周期性通知等。
主要功能特性:
- 显示基本本地通知
- 支持定时通知(按指定时间触发)
- 支持周期性通知(按分钟、小时、天、周间隔重复)
- 管理通知通道和权限
- 处理通知点击事件
- 支持通知进度条显示
- 支持应用角标设置
安装与引入
由于这是适用于 HarmonyOS 的自定义修改版本,需要通过 Git 形式引入依赖。在您的 Flutter 项目中,修改 pubspec.yaml 文件,添加以下配置:
dependencies:
flutter_local_notifications:
git:
url: https://gitcode.com/openharmony-sig/fluttertpc_flutter_local_notifications.git
path: flutter_local_notifications
添加依赖后,执行以下命令安装包:
flutter pub get
基本使用
1. 初始化插件
首先需要初始化插件,配置基本设置:
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
// 创建插件实例
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
// 初始化设置
Future<void> initNotifications() async {
// 鸿蒙平台初始化设置
const OhosInitializationSettings ohosInitializationSettings = OhosInitializationSettings(
defaultIcon: 'app_icon', // 默认通知图标
);
// 跨平台初始化设置
const InitializationSettings initializationSettings = InitializationSettings(
ohos: ohosInitializationSettings,
);
// 初始化插件
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
// 处理通知点击事件
onDidReceiveNotificationResponse: (NotificationResponse response) {
// 在这里处理通知点击事件
print('通知被点击,响应: ${response.payload}');
},
);
}
2. 请求通知权限
在鸿蒙系统上,需要请求通知权限才能显示通知:
Future<bool> requestNotificationPermissions() async {
// 获取鸿蒙平台特定实现
final OhosFlutterLocalNotificationsPlugin? ohosImplementation =
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
OhosFlutterLocalNotificationsPlugin>();
if (ohosImplementation != null) {
// 请求通知权限
final bool? granted = await ohosImplementation.requestNotificationsPermission();
return granted ?? false;
}
return false;
}
3. 显示基本通知
Future<void> showBasicNotification() async {
// 鸿蒙平台通知详情
const OhosNotificationDetails ohosNotificationDetails = OhosNotificationDetails(
icon: 'app_icon',
slotType: OhosNotificationSlotType.other,
importance: OhosImportance.defaultImportance,
autoCancel: true,
badgeNumber: 1,
);
// 跨平台通知详情
const NotificationDetails notificationDetails = NotificationDetails(
ohos: ohosNotificationDetails,
);
// 显示通知
await flutterLocalNotificationsPlugin.show(
1, // 通知ID
'通知标题', // 标题
'这是一条来自 flutter_local_notifications 的测试通知', // 内容
notificationDetails,
payload: 'notification_payload', // 可选负载数据
);
}
4. 显示带进度条的通知
Future<void> showProgressNotification() async {
const OhosNotificationDetails ohosNotificationDetails = OhosNotificationDetails(
icon: 'app_icon',
slotType: OhosNotificationSlotType.other,
importance: OhosImportance.defaultImportance,
showProgress: true,
maxProgress: 100,
progress: 50,
indeterminate: false, // 是否为不确定进度
);
const NotificationDetails notificationDetails = NotificationDetails(
ohos: ohosNotificationDetails,
);
await flutterLocalNotificationsPlugin.show(
2,
'下载进度',
'正在下载文件...',
notificationDetails,
);
}
5. 定时通知
import 'package:timezone/timezone.dart' as tz;
Future<void> scheduleNotification() async {
// 设置时区(使用本地时区)
final String localTimeZone = await flutterLocalNotificationsPlugin.getLocalTimezone();
final tz.Location location = tz.getLocation(localTimeZone);
// 设置通知触发时间(5秒后)
final tz.TZDateTime scheduledDate = tz.TZDateTime.now(location).add(const Duration(seconds: 5));
const OhosNotificationDetails ohosNotificationDetails = OhosNotificationDetails(
icon: 'app_icon',
slotType: OhosNotificationSlotType.other,
importance: OhosImportance.defaultImportance,
);
const NotificationDetails notificationDetails = NotificationDetails(
ohos: ohosNotificationDetails,
);
// 调度通知
await flutterLocalNotificationsPlugin.zonedSchedule(
3,
'定时通知',
'这是一条5秒后触发的定时通知',
scheduledDate,
notificationDetails,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
);
}
6. 周期性通知
Future<void> showPeriodicNotification() async {
const OhosNotificationDetails ohosNotificationDetails = OhosNotificationDetails(
icon: 'app_icon',
slotType: OhosNotificationSlotType.other,
importance: OhosImportance.defaultImportance,
);
const NotificationDetails notificationDetails = NotificationDetails(
ohos: ohosNotificationDetails,
);
// 每小时显示一次通知
await flutterLocalNotificationsPlugin.periodicallyShow(
4,
'周期性通知',
'这是一条每小时重复的通知',
RepeatInterval.hourly,
notificationDetails,
);
}
7. 取消通知
// 取消特定ID的通知
Future<void> cancelNotification(int notificationId) async {
await flutterLocalNotificationsPlugin.cancel(notificationId);
}
// 取消所有通知
Future<void> cancelAllNotifications() async {
await flutterLocalNotificationsPlugin.cancelAll();
}
高级功能
管理通知通道
在鸿蒙系统上,可以管理通知通道(槽位):
Future<void> manageNotificationSlots() async {
final OhosFlutterLocalNotificationsPlugin? ohosImplementation =
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
OhosFlutterLocalNotificationsPlugin>();
if (ohosImplementation != null) {
// 添加通知槽位
await ohosImplementation.addNotificationSlot(const OhosNotificationSlot(
type: OhosNotificationSlotType.other,
importance: OhosImportance.defaultImportance,
name: '其他通知',
description: '用于显示一般通知',
));
// 获取所有通知槽位
final List<OhosNotificationSlot>? slots = await ohosImplementation.getNotificationSlots();
print('通知槽位: $slots');
}
}
检查通知状态
Future<void> checkNotificationStatus() async {
final OhosFlutterLocalNotificationsPlugin? ohosImplementation =
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
OhosFlutterLocalNotificationsPlugin>();
if (ohosImplementation != null) {
// 检查通知是否启用
final bool? isEnabled = await ohosImplementation.areNotificationsEnabled();
print('通知是否启用: $isEnabled');
}
}
约束与限制
在 HarmonyOS 上使用 flutter_local_notifications 插件时,需要注意以下限制:
-
兼容性要求:
- Flutter: 3.7.12-ohos-1.1.3 或 3.22.1-ohos-1.0.3
- SDK: 5.0.0(12)
- IDE: DevEco Studio 5.1.0.828
- ROM: 5.1.0.130 SP8
-
不支持的功能:
- 通知声音播放
- 通知振动
- 自定义振动模式
- 持续通知(ongoing)
- 不确定进度条
- LED灯效
- 全屏意图
- 通知负载数据(payload)
-
权限注意事项:
- 使用
zonedSchedule方法需要主动申请权限
- 使用
总结
flutter_local_notifications 插件为 HarmonyOS 提供了丰富的本地通知功能,开发者可以轻松实现:
- 基本通知显示
- 定时和周期性通知
- 通知权限管理
- 通知通道配置
- 通知点击事件处理
- 进度条通知
- 应用角标设置
虽然在 HarmonyOS 上存在一些功能限制,但插件仍然提供了核心的通知功能,能够满足大多数应用的需求。通过简单的 API 调用,开发者可以快速集成本地通知功能,提升应用的用户体验。
更多详细的使用示例,可以参考项目中的 example 目录。
欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net
更多推荐




所有评论(0)