微信小程序实现消息推送(调用小程序推送模板接口) 完整示例

 

    

wxml文件:
 <form bind:submit="testSubmit" report-submit="true" hidden="{{emptyprompt}}">
      <button formType="submit">发送模板消息</button>
 </form>


js文件:

testSubmit: function (e) {
    var self = this;
//获取access_token :APPID为小程序appid,app_secret为小程序密钥
    wx.request({
        url: 'https://api.weixin.qq.com/cgi-bin/token?                              
        grant_type=client_credential&appid=APPID&secret=APP_SECRET
        method: 'GET',
        success: function (res) {
            self.setData({
                access_token: res.data.access_token
            })

//发送模板消息  需要注意的是openid需为触发当前事件人的openid,不然收不到推送消息
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?        
access_token=access_token',

data: {
  "touser": self.data.openid, //接收者(用户)的
  "template_id": template_id, //所调用的小程序模板id
  "page": "pages/index/index",
  "form_id":  self.data.payid,//支付场景下为本次支付的 prepay_id​​​​​​​
    //"form_id": e.detail.formId,// 表单提交场景下为 submit 事件带上的 formId​​​​​​​
  "data": {
        "keyword1": {
          "value": "339208499"
        },
        "keyword2": {
          "value": "2015年01月05日 12:30"
        },
        "keyword3": {
          "value": "腾讯微信总部"
        },
        "keyword4": {
          "value": "广州市海珠区新港中路397号"
        }
      },
      "emphasis_keyword": "keyword1.DATA"  //要放大的数据
    },
  method: 'POST',
  header: {
      'content-type': 'application/json'
  },
  success: function (res) {
      console.log(res)
  },
  fail: function (err) {
      console.log('request fail ', err);
  }
})
}




 

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值