天天看点

订阅号消息 服务号 信息推送

<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 )

使用效果