小程序 訂閱消息功能實現 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: '提示:授權失敗'
        })
      }
    })
  },

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM