微信的消息订阅

一、申请消息模版

微信公众平台,申请消息模版

二、在uni-app 里面下载这个插件uni-subscribemsg

三、根据文档定义一个message.js 云函数

文档里面都有现成的代码

javascript 复制代码
'use strict';

const uidObj = require('uni-id');
const {
 Controller
} = require('uni-cloud-router');
// 引入uni-subscribemsg公共模块
const UniSubscribemsg = require('uni-subscribemsg');
// 初始化实例
let uniSubscribemsg = new UniSubscribemsg({
 dcloudAppid: "填你的应用id",
 provider: "weixin-mp",
});

module.exports = class messagesController extends Controller {

 // 发送消息 
 async send() {

  let response = { code: 1, msg: '发送消息失败', datas: {} };
  const {
   openid,
   data,
  } = this.ctx.data;
  // 发送订阅消息
  let resdata = await uniSubscribemsg.sendSubscribeMessage({
   touser: openid,// 就是用户的微信id,决定发给他
   template_id: "填你刚刚申请的消息模版id",
   page: "pages/tabbar/home", // 小程序页面地址
   miniprogram_state: "developer", // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
   lang: "zh_CN",
   data: {
    thing1: {
     value: "信息审核通知"// 消息标题
    },
    thing2: {
     value: '你有新的内容需要审核' // 消息内容
    },
    number3: {
     value: 1 // 未读数量
    },
    thing4: {
     value: '管理员' // 发送人
    },
    time7: {
     value: data.time // 发送时间
    }
   }
  });
  response.code = 0;
  response.msg = '发送消息成功';
  response.datas = resdata;

  return response;
 }
}

四、用户主动订阅消息

微信为了防止打扰用户,需要用户订阅消息,并且每次订阅只能发送一次,不过我取巧,在用户操作按钮上偷偷加订阅方法,让用户一直订阅,我就可以一直发

javascript 复制代码
// 订阅
  dingYue() {
   uni.requestSubscribeMessage({
    tmplIds: ["消息模版id"], // 改成你的小程序订阅消息模板id 
    success: (res) => {
     if (res['消息模版id'] == 'accept') {

     }

    }
   });
  },

五、

我安装了那个uni-app 的消息插件,但是一直报错找不到那个模块。原来是unicloud 云函数要主动关联公共模块,什么意思呢,直接上图。

如果你喜欢我的文章,可以关注我哦~

相关推荐
Uyker7 小时前
微信小程序动态效果实战指南:从悬浮云朵到丝滑列表加载
前端·微信小程序·小程序
happyCoder11 小时前
uniapp 微信小程序实现定时消息订阅提醒(前后端)
微信小程序
Uyker20 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
打小就很皮...1 天前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
前端缘梦2 天前
微信小程序登录方案实践-从账号体系到用户信息存储
前端·微信小程序
coding随想2 天前
2025年小程序开发全解析:技术储备、行业趋势与实战案例
微信小程序
Nueuis2 天前
微信小程序前端面经
前端·微信小程序·小程序
轩1152 天前
实现仿中国婚博会微信小程序
微信小程序·小程序
知否技术2 天前
2025微信小程序开发实战教程(一)
前端·微信小程序
喝牛奶的小蜜蜂3 天前
个人小程序:不懂后台,如何做数据交互
前端·微信小程序·小程序·云开发