美文网首页
微信小程序消息推送 2019-04-19

微信小程序消息推送 2019-04-19

作者: 小元红 | 来源:发表于2019-04-20 15:35 被阅读0次

消息推送

接入微信小程序消息推送服务,开发者需要按照如下步骤完成:

1.填写服务器配置

2.验证服务器地址的有效性

3.据接口文档实现业务逻辑,接收消息和事件

wxml:使用form表达获取form_id

<form bindsubmit="formSubmit" report-submit="true" bindreset='formReset'>

      <input name="input" class="input" placeholder="please input here" />

      <button formType="submit">Submit</button>

      <button formType="reset">Reset</button>

  </form>

js:

1.根据: 发送消息模板获取 access_token 

接口地址

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$[appid]&secret=[secret]

2.  发送模版消息

接口地址

https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN

发送模板消息给微信服务器,发送成功微信发送推送消息


formSubmit: function (e) {

    var form_id = e.detail.formId;

    let that=this

    wx.request({

      url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$[appid]&secret=[secret]',

      data:{},

      // method: "POST",

      header: {

        'content-type': 'application/json' // 默认值

      },

      success: function (res) {

        console.log("token========"+res.data.access_token)

        wx.request({

          url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + res.data.access_token,

          data: {

            "touser": '[openid]',

            "template_id":"模板号",

            "page": "返回的页面",

            "form_id": form_id,

            "data": {

              "keyword1": {

                "value": "339208499"

              },

              "keyword2": {

                "value": "2015年01月05日 12:30"

              },

              "keyword3": {

                "value": "粤海喜来登酒店"

              },

              "keyword4": {

                "value": "广州市天河区天河路208号"

              }

            },

            "emphasis_keyword": "keyword1.DATA"

          },

          method:"POST",

          header: {

            'content-type': 'application/json' // 默认值

          },

          success: function (res) {

            console.log(res)

          }

        })

      }

    })

  }

注意:foem_id 只要在手机才能获取,在模拟器中是没有的

相关文章

网友评论

      本文标题:微信小程序消息推送 2019-04-19

      本文链接:https://www.haomeiwen.com/subject/ciwfgqtx.html