@heybox/hb-sdk API Reference - v0.9.5
    正在准备搜索索引...

    接口 CompanionNotifications

    Companion 请求 Host 展示固定类别系统通知的能力。

    interface CompanionNotifications {
        show(input: CompanionNotification): Promise<void>;
    }
    索引

    方法

    方法

    • 请求 Host 为当前 Companion Session 展示固定类别的系统通知。

      参数

      • input: CompanionNotification

        必须是包含合法 category 的非数组对象,详见 CompanionNotification。 支持 completed、failed、stopped、attention 四类。缺失或其他值返回 INVALID_PARAMS。 JavaScript 传入的额外字段会被忽略并从 bridge payload 移除,不会作为自定义标题、正文、按钮、 URL 或原生通知参数传给 Host。

      返回 Promise<void>

      Host 确认本次通知请求已处理后以 undefined resolve。不返回通知 ID,也不保证操作系统 最终展示、用户看见或点击通知;系统通知权限、免打扰和平台投递策略仍由 Host / 操作系统决定。

      HbMiniProgramSDKError input 缺失、不是对象、是数组,或 category 不在四个固定值中 时,在发送 bridge 请求前返回 INVALID_PARAMS。

      HbMiniProgramSDKError 当前 proxy 已知 Session 已退出时返回 COMPANION_SESSION_EXITED;controller 已转移给其他 Runtime generation 时返回 COMPANION_SESSION_OWNERSHIP_LOST。本地检查与 Host 展示之间存在竞态,Host 会用私有 controller generation 再次拒绝过期调用。

      HbMiniProgramSDKError 当前 Host、操作系统或 Session 无法展示通知时返回 COMPANION_NOTIFICATION_UNAVAILABLE;Companion 不受支持、进程已异常退出或因输出上限结束时 还可能返回 COMPANION_UNSUPPORTED、COMPANION_PROCESS_CRASHED、COMPANION_OUTPUT_LIMIT。

      HbMiniProgramSDKError 当前运行快照缺少或未启用 companion 权限时返回 PERMISSION_NOT_IN_SNAPSHOT / PERMISSION_DENIED;Host 未开放 companion.showNotification 时返回 METHOD_FORBIDDEN,旧 Runtime 可返回 METHOD_NOT_FOUND。未知 Host 失败会脱敏为 COMPANION_HOST_FAILED;SDK / Runtime 终止、不可用或请求超时还可能返回 SDK_DESTROYED、 RUNTIME_UNAVAILABLE、REQUEST_TIMEOUT。

      notifications 只在 Session 快照的 capability 集合包含 'notifications' 时存在;调用前必须按 session.notifications 是否存在分支。该 capability 与 stdio、attachments 相互独立:Session 可以只提供其中一项;显示通知不要求 stdio framing,也不会创建附件、返回 FileHandle、消耗 direct-handle 配额或要求 filesystem / network 权限。

      SDK 先校验 input 和当前 controller,再只发送私有 Session ID、controller generation 与固定 category;Runtime 重复校验并裁剪 payload。页面无法传入标题、正文或任意系统通知配置,Host 根据 category 生成安全、平台一致的固定文案。需要展示业务详情时,应在 Mini-program UI 内呈现, 不要把未知字段附加到 input 期待 Host 透传。

      调用不要求新的可信用户手势,但要求当前进程仍在运行且当前 Runtime generation 持有 controller。 session.state === 'running' 不能证明 controller 未转移;应监听 onOwnershipLost() 并停止后续通知请求。页面完全断开后 不承诺继续发送业务通知;该 API 不是 Host 后台定时通知、推送订阅或持久通知队列。

      V1 真实 Companion notifications 仅支持新 PC Host 的 Windows x64 与 macOS arm64;Browser Dev Host 只记录确定性 Fake diagnostic,不展示真实系统通知。其他平台、旧 Host、Session 未声明 capability 或 operation 缺失时 fail closed,不回退为浏览器 Notification API、toast 或 stdio 消息。

      import hbSDK, { HbMiniProgramSDKError } from '@heybox/hb-sdk'

      const session = await hbSDK.companion.getActiveSession('runner')
      const notifications = session?.notifications
      if (notifications) {
      try {
      await notifications.show({ category: 'completed' })
      } catch (error) {
      if (
      error instanceof HbMiniProgramSDKError &&
      error.code === 'COMPANION_NOTIFICATION_UNAVAILABLE'
      ) {
      showCompletionInPage()
      } else {
      throw error
      }
      }
      }