Android怎么实现小程序发送模板消息

发布时间:2021-12-27 09:50:10 作者:iii
来源:亿速云 阅读:127

本篇内容主要讲解“Android怎么实现小程序发送模板消息”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android怎么实现小程序发送模板消息”吧!


一、准备工作

    首先,在微信公众平台开通消息推送功能,并添加消息模板。可以从模板库选择模板也可以创建一个模板,模板添加之后,模板ID我们接下来要用的。

    发送模板消息需要用到accesstoken、formId和openID。formID就是消息模板ID,openID我们最好在获取用户信息或用户登录时储存到全局变量里。

二、获取formID。

 在需要触发消息推送的页面添加提交表单的事件。目的是得到formID,formID是消息推送时必须的参数。

<form name='pushMsgFm' report-submit='true' bindsubmit='getFormID'>
<button form-type="submit" class="zan-btn zan-btn--large zan-btn--danger payButton">立即支付</button>
</form>

以上代码中“getFormID”是提交表单时触发的事件。

getFormID: function (e) {
this.setData({
formId: e.detail.formId }) }


以上方法是获取formId。

三、配置消息模板参数,并传给后台。

var config = require('../config.js')
var app = getApp();
function pushMsg(formID, access_token){
var openId = app.globalData.userInfo.openId;
var messageDemo = {
touser: openId,//openId
template_id: 'PjtLeqq-UeF49r5jr88s27HBzBDobijr6QfiwJwIkPg',//模板消息id,
page: 'pages/index/index',//点击详情时跳转的主页
form_id: formID,//formID
data: {//下面的keyword*是设置的模板消息的关键词变量

"keyword1": {
"value": "keyword1",
"color": "#4a4a4a"
},
"keyword2": {
"value": "keyword2",
"color": "#9b9b9b"
},
"keyword3": {
"value": "keyword3",
"color": "red"
}
},
color: 'red',//颜色
emphasis_keyword: 'keyword3.DATA'//需要着重显示的关键词
}
wx.request({
url: config.service.sendMsgUrl,
data: { value: messageDemo, access_token: access_token},
method: 'POST',
success: function (res) {
console.log("push msg");
console.log(res);
},
fail: function (err) {
console.log("push err")
console.log(err);
}
});
}
module.exports = { pushMsg: pushMsg }

四、推送消息。
  


const request = require('../tools/ih_request');
var conf = require('../config.js')
module.exports = async (ctx, next) => {
var body = ctx.request.body.value
await request.postJson({
url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + ctx.request.body.access_token,
body: body,
success: function (res) {
ctx.body = {
result: res
}
console.log('res=',res);
},
error: function (err) {
ctx.body = {
result: err
}
console.log(err);
}
});}
ih_request.js
const request = require('request');
var ih_request = {};
module.exports = ih_request;
ih_request.postJson = async function (option) {
var res = await request({
url: option.url,
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(option.body),
}, function (err, res, body) {
res ? option.success(body) : option.error(res.msg);
console.log('MSGresult=', body);
});
}

到此,相信大家对“Android怎么实现小程序发送模板消息”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. C#开发之微信小程序发送模板消息功能的实现方法
  2. Django后端发送小程序微信模板消息的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:Visual Studio 2010中测试功能的示例分析

下一篇:Command对象怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》