小程序 订阅消息功能实现 wx.requestSubscribeMessage

写在前: 小程序 服务通知 订阅消息功能    小程序文档地址   (前端部分)

第一步,html 内容,添加按钮点击事件

<text>{{textcontent}}</text>
<button bindtap="subTap" hover-class="other-button-hover">允许订阅消息</button>

第二步,js 内容

data: {
    textcontent: '提示:未开启'
},
 
// 检测是否开启  更新提示
  testingTap: function() {
    let that = this;
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        // console.log(res)
        if (res.subscriptionsSetting.mainSwitch) {
          if (res.subscriptionsSetting.itemSettings != null) {
            let item = res.subscriptionsSetting.itemSettings.P2wDrfX0CYmme5mgnmyxVdbXMZFp1mjByg9dE4_YrdQ
            if (item == "reject") {
              that.setData({
                textcontent: '提示:您已经拒绝订阅消息'
              })
            } else if (item == "accept") {
              that.setData({
                textcontent: '提示:您已经开启订阅消息'
              })
            } else if (item == "ban") {
              that.setData({
                textcontent: '提示:您已经被后台封禁'
              })
            }
          }
        } else {
          that.setData({
            textcontent: '提示:订阅消息未开启'
          })
        }
      }
    })
  },
 
//授权
subTap: function() {
    let that = this;
    wx.requestSubscribeMessage({
      tmplIds: ['P2wDrfX0CYmme5mgnmyxVdbXMZFp1mjByg9dE4_YrdQ'],
      success(res) {
        that.setData({
          textcontent: '提示:授权成功'
        })
      },
      fail(res) {
        that.setData({
          textcontent: '提示:授权失败'
        })
      }
    })
  },

  

posted @ 2021-11-28 15:59  星火卓越  阅读(930)  评论(0编辑  收藏  举报