小程式 訂閱訊息功能實現 wx.requestSubscribeMessage
阿新 • • 發佈:2021-11-28
寫在前: 小程式 服務通知 訂閱訊息功能 小程式文件地址 (前端部分)
第一步,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: '提示:授權失敗' }) } }) },