# Weixin Mini Program Subscribe to messages (users subscribe via pop-up) Development Guide

# Instructions

# Step 1: Get the template ID

Get the template ID manually configured in the WeChat public platform: Log in [https://mp.weixin.qq.com-0]]](https://mp.weixin.qq.com) Get a template. If you don't have a suitable template, you can apply to add a new template. Once it's approved, you can use it.

intro

# Step 2: Get permission to distribute

One-time subscription message, long-term subscription message, see interface wx.requestSubscribeMessage

Device subscription messages, see Interface wx.requestSubscribeDeviceMessage

# Step 3: Call the interface to send a subscription message

One-time subscription message, long-term subscription message, see service interface subscribeMessage.sEND ,Number of restrictions: the opening of the ability to pay Weixin Mini Program under the upper limit is 3kw / day, not opened is 1kw / day.

Device subscription messages, see Service Interface hardwareDevice.sEND

# Note

  • The user checks "Always keep the above choice and don't ask again." After that, the next subscription call wx.requestSubscribeMessage does not pop up. Keep the previous choice. To change the choice, you need to open the Weixin Mini Program setting to change it.

# Subscribe to News Event Push

When a user triggers the subscription message pop-up, the results of the user's relevant behavior events are pushed to the server address configured by the developer or the WeChat cloud-hosted service.

# XML Format Examples

<xml>
    <ToUserName><![CDATA[gh_123456789abc]]></ToUserName>
    <FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>
    <CreateTime>1610969440</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[subscribe_msg_popup_event]]></Event>
    <SubscribeMsgPopupEvent>
        <List>
            <TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>
            <SubscribeStatusString><![CDATA[accept]]></SubscribeStatusString>
            <PopupScene>2</PopupScene>
        </List>
        <List>
            <TemplateId><![CDATA[9nLIlbOQZC5Y89AZteFEux3WCXRRRG5Wfzkpssu4bLI]]></TemplateId>
            <SubscribeStatusString><![CDATA[reject]]></SubscribeStatusString>
            <PopupScene>2</PopupScene>
        </List>
    </SubscribeMsgPopupEvent>
</xml>

# Example of JSON format

{
  "ToUserName": "gh_123456789abc",
  "FromUserName": "o7esq5OI1Uej6Xixw1lA2H7XDVbc",
  "CreateTime": "1620973045",
  "MsgType": "event",
  "Event": "subscribe_msg_popup_event",
  "List": [   {
        "TemplateId": "hD-ixGOhYmUfjOnI8MCzQMPshzGVeux_2vzyvQu7O68",
        "SubscribeStatusString": "accept",
        "PopupScene": "0"
    }],
 }

If the "List" has only one object, only the object itself is returned; If the List contains more than one object, an array containing all objects is returned.

# Parameter explaination

parameter Introductions
ToUserName Weixin Mini Program Account ID
FromUserName User openid
CreateTime timestamp
TemplateId Template id (there may be more than one id for a single subscription)
SubscribeStatusString Subscribe results (accept receive; Reject
PopupScene Frame scene, 0 is in the Weixin Mini Program page



When a user manages a message in the upper right corner of the message card in the service notification on the mobile phone, the corresponding behavior event is pushed to the server address configured by the developer or the WeChat cloud-hosted service.(Currently only events that cancel subscriptions are pushed, i.e. settings to "opt out" of messages)

# XML Format Examples

<xml>
    <ToUserName><![CDATA[gh_123456789abc]]></ToUserName>
    <FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>
    <CreateTime>1610969440</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[subscribe_msg_change_event]]></Event>
    <SubscribeMsgChangeEvent>
        <List>          <TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>
            <SubscribeStatusString><![CDATA[reject]]></SubscribeStatusString>
        </List>
    </SubscribeMsgChangeEvent>
</xml>

# Example of JSON format

{
      "ToUserName": "gh_123456789abc",
      "FromUserName": "o7esq5OI1Uej6Xixw1lA2H7XDVbc",
      "CreateTime": "1610968440",
      "MsgType": "event",
      "Event": "subscribe_msg_change_event",
      "List": [  {
                "TemplateId":"BEwX0BOT3MqK3Uc5oTU3CGBqzjpndk2jzUf7VfExd8",
                "SubscribeStatusString": "reject"
      }],
}

If the "List" has only one object, only the object itself is returned; If the List contains more than one object, an array containing all objects is returned.

# Parameter explaination

parameter Introductions
ToUserName Weixin Mini Program Account ID
FromUserName User openid
CreateTime timestamp
TemplateId Template id (there may be more than one id for a single subscription)
SubscribeStatusString Subscription results (reject)



3 The final result of calling the subscription message interface to send a message to the user pushes down the result event to the server address configured by the developer or the WeChat cloud-hosted service.

# XML Format Examples

<xml>
    <ToUserName><![CDATA[gh_123456789abc]]></ToUserName>
    <FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>
    <CreateTime>1610969468</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[subscribe_msg_sent_event]]></Event>
    <SubscribeMsgSentEvent>
        <List>       <TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>
            <MsgID>1700827132819554304</MsgID>
            <ErrorCode>0</ErrorCode>
            <ErrorStatus><![CDATA[success]]></ErrorStatus>
        </List>
    </SubscribeMsgSentEvent>
</xml>

# Example of JSON format

{
    "ToUserName": "gh_123456789abc",
    "FromUserName": "o7esq5PHRGBQYmeNyfG064wEFVpQ",
    "CreateTime": "1620963428",
    "MsgType": "event",
    "Event": "subscribe_msg_sent_event",
    "List": {
        "TemplateId": "BEwX0BO-T3MqK3Uc5oTU3CGBqzjpndk2jzUf7VfExd8",
        "MsgID": "1864323726461255680",
        "ErrorCode": "0",
        "ErrorStatus": "success"
      }
      
}

# 参数说明

参数 说明
ToUserName 小程序账号ID
FromUserName 用户openid
CreateTime 时间戳
TemplateId 模板id(一次订阅可能有多个id)
MsgID 消息id(调用接口时也会返回)
ErrorCode 推送结果状态码(0表示成功)
ErrorStatus 推送结果状态码对应的含义

注意:失败仅包含因异步推送导致的系统失败

# 订阅消息语音提醒

从基础库 2.18.0 开始支持

当前小程序订阅消息通知与微信消息的通知的提示音是一样的,对于部分订阅消息模板,增加语音提醒能力,播报语料部分字段支持开发者定义。

当开发者调用wx.requestSubscribeMessage时仅订阅1条消息且该模板支持开启语音提醒,用户在订阅时可以选择开启语音提醒。开启后将在接收订阅消息时会同步播报语音提醒。

当用户开启了语音提醒,开发者通过wx.getSetting获取的该模板的订阅状态为'acceptWithAudio'。

订阅弹窗样式如下:

intro

当前支持开启语音提醒的模板及播报语料如下:

标题 类型 类目 播报语料
收款到账通知 长期订阅 金融业-银行、金融业-收单商户服务 小程序示例收款10元,其中“小程序示例”会播报为下发小程序的小程序简称,10会播报为模版中的收款金额

以下情况会导致语音提醒无法播报:

  1. 用户将服务通知设置为免打扰
  2. 用户开启了手机静音模式或手机音量过低
  3. 用户未打开微信新消息通知,可引导用户前往微信-“我”-“设置”-“新消息通知”中打开
  4. 用户未打开系统对微信的通知
  5. 用户开启了低电量模式
  6. 用户版本过低:需要iOS 8.0.6与安卓8.0.3及以上

# 订阅消息添加提醒

从基础库 2.22.0 开始支持

用户在订阅小程序长期订阅消息时,可以根据自己的使用情况添加提醒。添加后,用户在收到消息时,在微信内将由横幅通知提醒。

当用户添加了提醒,开发者通过wx.getSetting获取的该模板的订阅状态为'acceptWithAlert'。

订阅弹窗样式如下:

intro