溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序服務(wù)通知功能如何實(shí)現(xiàn)

發(fā)布時(shí)間:2022-03-16 09:27:33 來(lái)源:億速云 閱讀:1009 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“微信小程序服務(wù)通知功能如何實(shí)現(xiàn)”,在日常操作中,相信很多人在微信小程序服務(wù)通知功能如何實(shí)現(xiàn)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”微信小程序服務(wù)通知功能如何實(shí)現(xiàn)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

下面直接上代碼:

wxml:

<form name='pushMsgFm' report-submit bindsubmit='form'>

<button form-type="submit">submit</button>

</form>

js:

// pages/index/index.js

Page({

data: {

},

/*

微信公眾平臺(tái)測(cè)試連接 https://mp.weixin.qq.com/debug/

全局返回碼說(shuō)明:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433747234

*/

onLoad: function (options) {

var that = this

wx.login({

success: function (data) {

console.log(data.code, data)

// 獲取openid

wx.request({

url: 'https://api.weixin.qq.com/sns/jscode2session?appid=你的自己的appid&secret=你自己的session_key&js_code=' + data.code ,

header:  {  "Content-Type":  "application/x-www-form-urlencoded"  },

method: "post",

success: function (res) {

console.log(res, "opind")

that.setData({

openid: res.data.openid,

session_key: res.data.session_key,

})

}

})

}

})

// 獲取access_token

wx.request({

url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的自己的appid&secret=你自己的session_key',

method: "GET",

success: function (res) {

console.log(res, "res")

console.log(res.data.access_token, "access_token")

that.setData({

access_token: res.data.access_token,

})

}

})

},

// 點(diǎn)擊執(zhí)行方法

form: function (e) {

var that = this;

var fId = e.detail.formId;

// 網(wǎng)絡(luò)請(qǐng)求

var l = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.access_token;

// 需要傳的參數(shù)

var d = {

touser: that.data.openid, //用戶的openid

template_id: 'XX1hceIwI1XiQaUc5Z4qIrZnYQkYEHElSq5m6yIa0M8',//這個(gè)是申請(qǐng)的模板消息id,位置在微信公眾平臺(tái)/模板消息中添加并獲取

page: '/pages/index/index', //點(diǎn)擊通知跳轉(zhuǎn)的頁(yè)面

form_id: fId, //表單提交場(chǎng)景下,為 submit 事件帶上的 formId

//此處必須為data,只有人說(shuō)value也可以,可能官方已經(jīng)修復(fù)這個(gè)bug

data: {

"keyword1": {

"value": "酒店",

"color": "#4a4a4a"

},

"keyword2": {

"value": "2018-03-22",

"color": "#9b9b9b",

},

"keyword3": {

"value": "$300",

"color": "#9b9b9b"

},

"keyword4": {

"value": "中國(guó)",

"color": "#9b9b9b"

},

},

color: '#ccc',

emphasis_keyword: 'keyword1.DATA'

}

wx.request({

url: l,

data: d,

method: 'POST', //此處不能有請(qǐng)求頭

success: function (res) {

console.log(res, "push msg");

},

fail: function (err) {

console.log(err, "push err");

}

});

},

})

      最后編譯,點(diǎn)擊提交發(fā)起請(qǐng)求,這里只能手機(jī)調(diào)試,我用開(kāi)發(fā)工具打印出來(lái)的formId: "the formId is a mock one"并不是數(shù)字串。此處需要真機(jī)測(cè)試才能成功

到此,關(guān)于“微信小程序服務(wù)通知功能如何實(shí)現(xiàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI