小程序推送消息(Template)


最近搞小程序模擬推送消息,才發現小程序推送消息接口准備下線。

請注意,小程序模板消息接口將於2020年1月10日下線,開發者可使用訂閱消息功能

 咱們現在有需求,所以不管下不下,完成再說。

一:”獲取access_token的接口地址:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

二:發送模板消息 
先在微信公眾平台選用怒需要的模板id,例如 
選用模板消息:

https://mp.weixin.qq.com/wxopen/tmplmsg?action=self_list&token=264012870&lang=zh_CN

登錄 https://mp.weixin.qq.com 獲取模板,如果沒有合適的模板,可以申請添加新模板,審核通過后可使用

 

 

 

三:頁面的 form 組件,屬性 report-submit 為 true 時,可以聲明為需要發送模板消息,此時點擊按鈕提交表單可以獲取 formId,用於發送模板消息。或者當用戶完成 支付行為,可以獲取 prepay_id 用於發送模板消息。

我這里使用表單可以獲取 formId模擬操作推送消息

 

四:調用接口下發模板消息

HTTPS 調用

請求地址

(POST請求) https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN

請求參數

屬性 類型 默認值 必填 說明
access_token string   接口調用憑證
touser string   接收者(用戶)的 openid
template_id string   所需下發的模板消息的id
page string   點擊模板卡片后的跳轉頁面,僅限本小程序內的頁面。支持帶參數,(示例index?foo=bar)。該字段不填則模板無跳轉。
form_id string   表單提交場景下,為 submit 事件帶上的 formId;支付場景下,為本次支付的 prepay_id
data Object   模板內容,不填則下發空模板。具體格式請參考示例。
emphasis_keyword string   模板需要放大的關鍵詞,不填則默認無放大

 

具體操作代碼:

 

 <form bind:submit="testSubmit" report-submit="true">
      <button formType="submit">發送模板消息</button>
 </form>

 

onLoad: function (options) {
    var that = this;
    
        //獲取 access_token

    that.getAccessToken();

  },


  getAccessToken: function () {
    var that = this;
    var appdid = '填寫小程序的APPID';
    var appsecret = '填寫小程序的密鑰'
    var url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appdid + '&secret=' + appsecret;
    wx.request({
      url: url,
      success: function (res) {
        console.log(res) // 控制台輸出結果
        that.setData({
          access_token: res.data.access_token
        })
         console.log("*********獲取************" + res.data.access_token) // 控制台輸出結果
      }
    })
  },
//點擊發送模板,獲取formid testSubmit:
function (e) { var self = this; console.log("*********** e.detail.formId**********" + e.detail.formId) var _access_token = self.data.access_token console.log("***********控制台輸出結果**********" + _access_token) var opeid = "微信推送消息接收者的openid" let url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + _access_token; let jsonData = { access_token: _access_token, touser: opeid, template_id: '所需下發的模板消息的ID', form_id: e.detail.formId, page: "pages/index/index", data: { "keyword1": { "value": "互聯網大會科學技術", "color": "#173177" }, "keyword2": { "value": "2019年1月4日 09:00", "color": "#173177" }, "keyword3": { "value": "上海國際展中心", "color": "#173177" }, "keyword4": { "value": "務必准時參加", "color": "#173177" }, } } console.log("**********jsonData*******************" + JSON.stringify(jsonData)) wx.request({ url: url, data:jsonData, method: 'POST', success: function (res) { console.log("***" + JSON.stringify(res)) }, fail: function (err) { console.log('request fail ', err); }, }) },

記得拿起手機,真機調試模板推送

結果:

 

 

成功示例

{ "errcode": 0, "errmsg": "ok" }

errcode 的合法值

 

 

說明 最低版本
40037 template_id不正確  
41028 form_id不正確,或者過期  
41029 form_id已被使用  
41030 page不正確  
45009 接口調用超過限額(目前默認每個帳號日調用限額為100萬)

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM