使用uniapp云函数如何开通微信消息推送 您所在的位置:网站首页 uniapp如何实现消息推送 使用uniapp云函数如何开通微信消息推送

使用uniapp云函数如何开通微信消息推送

2023-05-06 03:49| 来源: 网络整理| 查看: 265

》演示视频》官网文档

消息推送 | 微信开放文档

开发者通过检验 signature 对请求进行校验。若确认此次 GET 请求来自微信服务器,请原样返回 echostr 参数内容,则接入生效,成为开发者成功,否则接入失败

文档中提到开通时需要先做接口验证,并原样返回echostr

》前期准备创建一个云函数,先不用写代码,上传部署,并url化

2. 登录小程序后台,进入消息推送配置页面,填写完成相关内容,点击发送

3. 登录uniapp云开发后台,查看云函数的日志。可以看到如下参数

》编写云函数

接收到参数后,开始写验证代码,代码如下

'use strict'; const crypto = require('crypto'); function getSignature(token, timestamp, nonce, msgEncrypt) { const str = [token, timestamp, nonce, msgEncrypt].sort().join('') return crypto.createHash('sha1').update(str).digest("hex") } function PKCS7Decode(buf) { let padSize = buf[buf.length - 1] return buf.slice(0, buf.length - padSize) } function decryptMsg(encodingAESKey, msgEncrypt) { const key = Buffer.from(encodingAESKey + '=', 'base64') const iv = key.slice(0, 16) const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv) decipher.setAutoPadding(false) let deciphered = Buffer.concat([decipher.update(msgEncrypt, 'base64'), decipher.final()]) deciphered = PKCS7Decode(deciphered) const content = deciphered.slice(16) const length = content.slice(0, 4).readUInt32BE(0) return { message: JSON.parse(content.slice(4, length + 4).toString()), appId: content.slice(length + 4).toString() } } exports.main = async function(event, context) { const { signature: signature, timestamp: timestamp, nonce: nonce, echostr: echostr } = event.queryStringParameters const tmpStr = getSignature('你的token', timestamp, nonce) if (signature === tmpStr) { return echostr } else { return } }

上传部署,再次提交,可以看到配置已完成

》接收数据&调试

验证完成后,就不用返回echostr了,接下来就可以编写业务代码。

注意:正式接受数据,需要再次确认日志中body格式。不同接口回调数据可能存在差异。

调试的话,可以直接在代码中打印consolelog,日志中会显示。

或者将body数据截取下来。写死在接受的数据中。然后本地去运行云函数。可以更方便的调试问题。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有