Flutter---认识-Notification

基本概念

复制代码
本地通知:应用自己创建和管理的通知

推送通知:从服务器推送的通知(FCM/APNs)

前台服务通知:保持应用在后台运行

定时通知:在指定时间触发的通知

通知的重要性级别

Dart 复制代码
enum Importance {
  none,      // 不显示
  min,       // 最低,不发出声音
  low,       // 低,可能发出声音
  default,   // 默认,发出声音
  high,      // 高,显示在手机弹窗里,但是不会直接弹出
  max,       // 最高,一定会弹出(浮动通知)
}

主要依赖

Dart 复制代码
dependencies:
  flutter_local_notifications: ^19.5.0
  timezone: ^0.10.0  # 用于定时通知
  permission_handler: ^11.0.0          # 权限管理
  android_alarm_manager_plus: ^3.0.2   # 精确定时(Android)

通道参数的详细说明

Dart 复制代码
AndroidNotificationChannel(
  // 必需参数
  'channel_id',                // 唯一ID
  '通道名称',                  // 用户看到的名称
  
  // 重要参数
  importance: Importance.max,  // 重要性
  description: '描述',         // 通道描述
  
  // 声音和震动
  playSound: true,             // 播放声音
  sound: RawResourceAndroidNotificationSound('sound'), // 自定义声音
  enableVibration: true,       // 启用震动
  vibrationPattern: Int64List.fromList([0, 1000, 500, 1000]),
  
  // 视觉
  ledColor: Colors.blue,       // LED颜色
  enableLights: true,          // 启用指示灯
  
  // 角标
  showBadge: true,             // 显示角标
  
  // 行为
  channelShowBadge: true,      // 通道显示角标
  channelAction: NotificationChannelAction.update, // 通道动作
);

通知配置的详细解释

Dart 复制代码
AndroidNotificationDetails(
  // 基础配置
  'channel_id',
  '通道名称',
  
  // 重要性行为
  importance: Importance.max,
  priority: Priority.high,
  
  // 声音震动
  playSound: true,
  sound: RawResourceAndroidNotificationSound('sound'),
  enableVibration: true,
  vibrationPattern: Int64List.fromList([0, 1000, 500, 1000]),
  
  // 视觉
  color: Colors.blue,
  colorized: true,
  ledColor: Colors.blue,
  ledOnMs: 1000,
  ledOffMs: 500,
  enableLights: true,
  
  // 图标
  icon: '@mipmap/ic_launcher',
  largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_launcher'),
  
  // 角标
  channelShowBadge: true,
  number: 1, // 角标数字
  
  // 行为控制
  autoCancel: true,        // 点击后自动取消
  ongoing: false,          // 是否持续通知
  onlyAlertOnce: false,    // 是否只提醒一次
  timeoutAfter: 5000,      // 超时时间(ms)
  
  // 样式
  styleInformation: null,  // 样式信息
  
  // 动作
  actions: [],             // 操作按钮
  
  // 分组
  groupKey: 'group_key',
  setAsGroupSummary: false,
  groupAlertBehavior: GroupAlertBehavior.all,
  
  // 可见性
  visibility: NotificationVisibility.public,
  localOnly: false,
  
  // 快捷方式
  shortcutId: 'shortcut_id',
  
  // 类别
  category: AndroidNotificationCategory.message,
  
  // 音频属性
  audioAttributesUsage: AudioAttributesUsage.notification,
);
相关推荐
2601_9495430121 小时前
Flutter for OpenHarmony垃圾分类指南App实战:我的成就实现
flutter
Miguo94well21 小时前
Flutter框架跨平台鸿蒙开发——海龟汤APP的开发流程
flutter·华为·harmonyos·鸿蒙
不爱吃糖的程序媛21 小时前
Flutter-OH 3.35.7-ohos-0.0.2 版本发布公告
flutter
无穷小亮1 天前
Flutter框架跨平台鸿蒙开发——Excel函数教程APP的开发流程
flutter·华为·excel·harmonyos·鸿蒙
无穷小亮1 天前
Flutter框架跨平台鸿蒙开发——打字练习APP开发流程
flutter·华为·harmonyos·鸿蒙
子春一1 天前
Flutter for OpenHarmony:构建一个高精度 Flutter 计时器:深入解析 Timer、状态同步与 UI 响应式设计
flutter·ui
雨季6661 天前
构建 OpenHarmony 简易文字行数统计器:用字符串分割实现纯文本结构感知
开发语言·前端·javascript·flutter·ui·dart
雨季6661 天前
Flutter 三端应用实战:OpenHarmony 简易倒序文本查看器开发指南
开发语言·javascript·flutter·ui
九 龙1 天前
Flutter框架跨平台鸿蒙开发——水电缴费提醒APP的开发流程
flutter·华为·harmonyos·鸿蒙
2401_892000521 天前
Flutter for OpenHarmony 猫咪管家App实战 - 添加支出实现
前端·javascript·flutter