天天看點

訂閱号消息 服務号 資訊推送

<button bindtap="subscribeMsg">點選訂閱消息</button>
 
 // 訂閱消息
    subscribeMsg() {
        let that = this
        let tmplId = ['gh8CVR5Qn0-an']; //['gh8CVR5Qn0-an','YqJnLuXMt7027NAEBB0p'] 一條或者多條
        //var template_ids = app.globalData.tmplIds;
        wx.requestSubscribeMessage({
            tmplIds: tmplId,//template_ids
            success(res) {
                if (res[tmplId] == 'accept') { //某條訂閱資訊 接收或者拒絕
                    that.cloudSendMsg();
                } else if (res[tmplId] == 'reject') { // 使用者拒絕授權
                    wx.showModal({
                        title: '溫馨提示',
                        content: "您已關閉消息推送,如需要消息推送服務,請點選确定跳轉設定頁面打開授權後再次嘗試。",
                        success: function(modal) {
                            if (modal.confirm) { // 點選确定
                                wx.openSetting({ withSubscriptions: true })
                            }
                        }
                    })
                }
            },
            fail(err) {
                if (err.errCode == '20004') {
                    wx.showModal({
                        title: '溫馨提示',
                        content: "您的消息訂閱主開關已關閉,如需要消息推送服務,請點選确定跳轉設定頁面打開授權後再次嘗試。",
                        success: function(modal) {
                            if (modal.confirm) { // 點選确定
                                wx.openSetting({ withSubscriptions: true })
                            }
                        }
                    })
                }
            },
           complete(res) {
            console.log('complete  調用完成')
            // 無論取消還是接收都會執行:比如 下單(無論訂閱号是取消還是接收都執行)
            this.pay()
          }
        })
    },
           

1.errMsg:“requestSubscribeMessage:fail can only be invoked by user TAP gesture.”

解決:bindtap點選才行、不能延遲調用

2.errMsg:“requestSubscribeMessage:fail 開發者工具暫時不支援此 API 調試,請使用真機進行開發”

解決:真機調試才行

3.errMsg:“requestSubscribeMessage:fail:No template data return, verify the template id exist”

errorCode:20001

解決:确認模闆ID是訂閱消息的模闆ID且正确。

4.errMsg:“requestSubscribeMessage:fail:Templates count out of max bounds”

errCode:20003

解決:模版數量超出,最多3個

5.requestSubscribeMessage:fail last call has not ended

解決:如果申請的是“一次性訂閱”,一次觸發隻有一次推送消息機會,是以我們會在其他頁面點選事件去調用requestSubscribeMessage,擷取更多次的推送消息機會。但是每個手機擷取是有上限的,大概幾十個吧。然後隻有用掉推送消息次數,才能重新成功調用,否則會出現以上問題。

注意-坑:

1.bindtap點選才可以調起

2.真機調試才行(–2020/01/03:貌似微信開發者工具也可以調用了) 反正我做項目的時候是上傳到體驗版才行,親測

3.不能延遲去調用wx.requestSubscribeMessage,如先調用其他接口請求成功再去它、使用定時器延遲調用

4.目前長期性訂閱消息僅向政務民生、醫療、交通、金融、教育等線下公共服務開放,後期将逐漸支援到其他線下公共服務業務

<button bindtap="tapName">點選訂閱消息</button>
 tapName() {
    // 消息設定
    // wx.getSetting({
    //   success (res) {
    //     console.log(res.authSetting)
    //     // res.authSetting = {
    //     //   "scope.userInfo": true,
    //     //   "scope.userLocation": true
    //     // }
    //   }
    // })
    //打開配置
    wx.openSetting({
      success (res) {
        console.log(res.authSetting)
        // res.authSetting = {
        //   "scope.userInfo": true,
        //   "scope.userLocation": true
        // }
      }
    })
    // 接受模闆id
    wx.requestSubscribeMessage({
      tmplIds: ['3UuMJMDSiTuFAzkRmBnsMW2iPr97p5v-Wf8pgBy2h7c'], //模闆id 數組
      success: (res) => {
        console.log(res, '=======123')
        this.pagexx()
      }
    })

  },
           
pagexx() {
    wx.request({
      url: 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=access_token', //僅為示例,并非真實的接口位址
      method:'POST',
      data: {
        "touser": "openid",
        "template_id": "3UuMJMDSiTuFAzkRmBnsMW2iPr97p5v-Wf8pgBy2h7c", //模闆
        "page": "/pages/home/index/index",
        "miniprogram_state": "developer",
        "lang": "zh_CN",
        "data": {
          "name1": {
            "value": "就診人"
          },
          "time2": {
            "value": "15:43"
          },
          "thing3": {
            "value": "藥品資訊"
          },
          "thing4": {
            "value": "提示說明"
          }
        }
      },
      header: {
        'content-type': 'application/json' // 預設值
      },
      success(res) {
        console.log(res, '=========1234')
      }
    })
  },
           

服務推送(模闆消息 2020/1/10 失效了)

先擷取擷取模闆的 form_id

console.log(e.detail.formId,'==========')
  POST https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN
           

步驟一:擷取模闆 ID

有兩個方法可以擷取模闆 ID:

通過模闆消息管理接口擷取模闆 ID(詳見 模闆消息管理)

在微信公衆平台手動配置擷取模闆 ID

登入 https://mp.weixin.qq.com 擷取模闆,如果沒有合适的模闆,可以申請添加新模闆,稽核通過後可使用,詳見 模闆稽核說明

步驟二:頁面的 form 元件,屬性 report-submit 為 true 時,可以聲明為需要發送模闆消息,此時點選按鈕送出表單可以擷取 formId,用于發送模闆消息。或者當使用者完成 支付行為,可以擷取 prepay_id 用于發送模闆消息。

Submit

步驟三:調用接口下發模闆消息(詳見 templateMessage.send )

使用效果