Skip to content

showWxSubscribeMessage

请求微信订阅消息,仅支持微信小程序端。

前置依赖

依赖参数

参数名类型说明
deps.requestSubscribeMessageUniApp['requestSubscribeMessage']请求订阅消息 API
deps.detectEnvironment() => 'h5' | 'miniapp' | 'app' | 'unknown'检测环境函数(可选)
deps.logConsole['log']日志输出
deps.errorConsole['error']错误输出

环境要求

  • 微信小程序: 仅支持微信小程序端
  • 注意: H5 和 App 端不支持,需要使用其他推送方式

函数签名

typescript
function showWxSubscribeMessage(
  options?: SubscribeMessageOptions,
  deps: ShowSubscribeMessageDeps
): Promise<SubscribeMessageResult>

interface ShowSubscribeMessageDeps {
  requestSubscribeMessage: UniApp['requestSubscribeMessage']
  detectEnvironment?: () => 'h5' | 'miniapp' | 'app' | 'unknown'
  log: Console['log']
  error: Console['error']
}

interface SubscribeMessageOptions {
  tmplIds?: string[]
  tmplId?: string
  scene?: string
  extraData?: Record<string, unknown>
}

interface SubscribeMessageResult {
  errMsg?: string
  [tmplId: string]: string | unknown | undefined
  raw?: unknown
}

参数

参数名类型必填默认值说明
optionsSubscribeMessageOptions{}订阅消息选项
options.tmplIdsstring[]-需要订阅的消息模板ID列表(最多3个)
options.tmplIdstring-单个模板ID(会自动转换为数组)
depsShowSubscribeMessageDeps-依赖注入对象

返回值

类型说明
Promise<SubscribeMessageResult>Promise,成功时返回订阅结果,包含每个模板ID的订阅状态('accept'、'reject'、'ban')

工作原理

  1. 环境检测:检查当前是否为微信小程序环境
  2. 参数验证:检查模板ID列表是否提供,最多支持3个模板
  3. 调用接口:调用 uni.requestSubscribeMessage 请求订阅消息
  4. 返回结果:返回订阅结果,包含每个模板ID的订阅状态

重要提示

  • 单个小程序同时最多只能订阅3条消息
  • 用户可以选择接受或拒绝订阅
  • 订阅状态包括:'accept'(接受)、'reject'(拒绝)、'ban'(被后台封禁)

注意:H5 和 App 端不支持此功能,会返回错误提示。