NotificationService

# NotificationService

NotificationService 是 AutumnBox 的应用内通知中心。插件可以通过它向用户推送通知消息(成功、错误、警告、信息),用户可以在通知面板中查看和管理这些消息。所有通知持久化在内存中,支持已读标记和未读计数。

# 获取方式

import { useService, useServiceState } from '@autumnbox/sdk/hooks';
import { NotificationService } from '@autumnbox/sdk/services';

// React 组件中
const notificationService = useService(NotificationService);

// pluginMain 或非 React 环境中
const notificationService = context.getService(NotificationService);

# API 一览

方法 / 属性 签名 说明
notifications readonly notifications: IReadonlyState<readonly INotification[]> 响应式的通知列表
unreadCount readonly unreadCount: IReadonlyState<number> 响应式的未读通知数
push push(type, title, message): void 推送一条通知
markRead markRead(id: string): void 标记某条通知为已读
markAllRead markAllRead(): void 标记所有通知为已读
clear clear(): void 清空所有通知

# push 参数说明

push(
  type: 'success' | 'error' | 'info' | 'warning',
  title: string,
  message: string,
): void
  • type -- 通知类型,决定通知的图标和颜色
  • title -- 通知标题,简要描述发生了什么
  • message -- 通知正文,提供详细信息

# 推送通知

四种通知类型对应不同场景:

import { NotificationService } from '@autumnbox/sdk/services';
import { useService } from '@autumnbox/sdk/hooks';

const notify = useService(NotificationService);

// 成功:操作顺利完成
notify.push('success', '文件传输完成', '已成功将 backup.tar.gz 推送到设备');

// 错误:操作失败或遇到异常
notify.push('error', '命令执行失败', 'pm install 返回 exit code 1,可能是签名冲突');

// 警告:需要关注但不阻断
notify.push('warning', '存储空间不足', '设备剩余空间仅 200MB,可能无法完成后续操作');

// 信息:一般性提示
notify.push('info', '后台任务已启动', 'Logcat 日志采集已开始');

# 订阅通知状态

# 未读计数(通知角标)

unreadCount 非常适合用来渲染通知图标上的角标:

import { useService, useServiceState } from '@autumnbox/sdk/hooks';
import { NotificationService } from '@autumnbox/sdk/services';
import { Badge, Button } from 'antd';
import { BellOutlined } from '@ant-design/icons';

const NotificationBadge: React.FC = () => {
  const notify = useService(NotificationService);
  const [unread] = useServiceState(notify.unreadCount);

  return (
    <Badge count={unread} size="small">
      <Button icon={<BellOutlined />} shape="circle" />
    </Badge>
  );
};

# 管理通知

const notify = useService(NotificationService);

// 标记单条已读
notify.markRead(notificationId);

// 全部标记已读
notify.markAllRead();

// 清空所有通知
notify.clear();

# 实战示例:文件传输通知

在文件推送完成或失败时自动推送通知:

import { useService } from '@autumnbox/sdk/hooks';
import { NotificationService } from '@autumnbox/sdk/services';
import { DeviceFileSystemService } from '@autumnbox/sdk/services';
import { useRequiredDevice } from '@autumnbox/sdk/hooks';

const FileUploader: React.FC = () => {
  const notify = useService(NotificationService);
  const fileService = useService(DeviceFileSystemService);
  const device = useRequiredDevice();

  const handleUpload = async (file: File) => {
    try {
      await fileService.push(device, `/sdcard/Download/${file.name}`, file);
      notify.push(
        'success',
        '文件已上传',
        `${file.name} (${(file.size / 1024).toFixed(1)} KB) 已传输到 /sdcard/Download/`,
      );
    } catch (err) {
      notify.push(
        'error',
        '上传失败',
        `${file.name} 传输失败:${err instanceof Error ? err.message : '未知错误'}`,
      );
    }
  };

  // ...渲染上传 UI
};

# 下一步