微信小步伐消息订阅分2种:
1.一次性订阅:用户订阅一次就可以推送一次,如果需要多次提醒需要多次订阅。
2.长期订阅:只有公共服务领域,如政务、医疗、交通、金融和教导等。在用户订阅后,在很长一段时间内多次推送消息。
步骤:
1.微信公众平台选择消息模板,选用后,我的模板-查看详情里会有模板id,消息格式等信息,代码里会用到
2.微信开辟者工具
(1)订阅消息按钮
- // index.wxml
- <button bindtap="requestMessage" type="default" disabled="{{isAdded}}">{{isAdded ? '已订阅新任务提醒':'订阅新任务提醒'}}</button>
复制代码 (2)点击按钮变乱引导用户授权
- async requestMessage() {
- wx.requestSubscribeMessage({
- tmplIds: ['xxxxxx'], // xxxxxx填选用的模板ID
- success: (res) => {
- if (res[templateId] === 'accept') {
- // 用户同意
- this.saveOpenId() // 记录用户openId,页面可以以此判断用户有无订阅消息
-
- wx.showToast({
- title: '授权成功'
- })
- } else if (res[templateId] === 'reject'){
- // 用户拒绝,弹窗和用户确认是否取消订阅(防止用户误操作)引导用户去设置界面重新订阅
- this.setData({
- showSetModel: true // 自定义弹窗
- })
- } else {
- wx.showToast({
- title: '授权订阅信息有误'
- })
- }
-
- },
- fail: (err) => {
- // 20004:用户关闭了主开关,无法进行订阅,引导开启
- if(err.errCode === 20004) {
- this.setData({
- showSetModel: true
- })
- } else {
- wx.showModal({
- title: '提示',
- content: err.errMsg,
- showCancel: false
- })
- }
- },
- })
- },
复制代码
如果用户点击拒绝,引导用户去设置页打开开关

- // index.wxml
- <view class="jumpSetModel" wx:if="{{showSetModel}}">
- <view class="box">
- <view class="title">提示</view>
- <view class="content">您已取消订阅通知消息,如果想重新订阅该消息可以点击去设置开启</view>
- <view class="button">
- <button class="cancel" catchtap="closeSetModel">不了</button>
- <button class="confirm" open-type="openSetting" bindopensetting="openSetCallback">去设置</button>
- </view>
- </view>
- </view>
复制代码- // 用户从设置页返回会触发下面的事件,从而得知用户有没有打开开关
- openSetCallback (callback) {
- wx.getSetting({
- withSubscriptions: true,
- success: res => {
- const tempId = 'xxx' //templateId
- // 判断用户允许或拒绝总是保持是否推送消息的选择, 如果选择过的话再点击就不显示了,判断有没有itemSettings并且有没有这个订阅消息的模板id
- if (res.subscriptionsSetting.itemSettings && res.subscriptionsSetting.itemSettings[tempId]) {
- if (res.subscriptionsSetting.itemSettings[tempId] == 'accept') {
- // 打开开关
- this.saveOpenId()
- } else {
- // 未打开开关
- }
- } else {
- // 没有对应templateId的数据
- }
- }
- })
- },
复制代码 如果想表现用户是否订阅过消息,需要记载用户openid,在页面进来时查询一下数据库
- // index.js
- onLoad() {
- this.checkStatus()
- },
- async checkStatus() {
- const currentUser = await wx.cloud.callFunction({name: 'getOpenId'})
- const currentOpenId = currentUser.result // 当前用户openid
- wx.cloud.database().collection('user-list').where({_openid: getApp().globalData.currentOpenId}).get().then(res => {
- this.setData({
- isAdded: res.data.length > 0 // 如果查询到数据就代表订阅过
- })
- })
- }
复制代码 使用到的云函数:
- // getOpenId 获取用户openId
- // 云函数入口文件
- const cloud = require('wx-server-sdk')
- cloud.init({ // 初始化云开发环境
- env: cloud.DYNAMIC_CURRENT_ENV // 当前环境的常量
- })
- // 云函数入口函数
- exports.main = async (event, context) => {
- // 返回当前用户的身份信息,用于数据库记录和查询
- return cloud.getWXContext().OPENID
- }
复制代码- // saveOpenId 保存用户openId
- // 云函数入口文件
- const cloud = require('wx-server-sdk')
- cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
- // 云函数入口函数
- exports.main = async (event, context) => {
- const wxContext = cloud.getWXContext()
- const db = cloud.database()
- return await db.collection('user-list').add({
- data: {
- _openid: wxContext.OPENID
- }
- })
- }
复制代码 当满足需求,发送订阅消息,可以是按钮触发或者别的逻辑,下面列出设置订阅消息表现的云函数
- // signMessage 订阅消息设置
- // 云函数入口文件
- const cloud = require('wx-server-sdk')
- cloud.init()
- exports.main = async (event, context) => {
- try {
-
- await cloud.openapi.subscribeMessage.send({
- touser: event._openid, // 发送通知给谁的openid
- data: {
- thing1: {
- value: event.name // 调用云函数可传参进来
- },
- thing3: {
- value: event.title
- }
- },
-
- templateId: 'xxx', // 模板ID
- page: 'pages/home/index' // 这个是发送完服务通知用户点击消息后跳转的页面
- })
- // 发送完毕就清空记录用户openId的表,再次查询的时候,显示未订阅,按钮可再次点击进行订阅
- return await cloud.callFunction({name: 'clearTable', data: {tableName: 'user-list'}})
- } catch (err) {
- console.log("Error while sending message:", err);
- return err
- }
- }
复制代码- // clearTable 清空数据库中的表
- // 云函数入口文件
- const cloud = require('wx-server-sdk')
- cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
- // 云函数入口函数
- exports.main = async (event, context) => {
- const db = cloud.database()
- const _ = db.command
- return await db.collection(event.tableName).where({
- _id: _.neq(null)
- })
- .remove()
- }
复制代码 手动触发消息推送,就写一个按钮就好,如果需要定时,比如每日签到提醒就需要用到定时器
- // index.wxml
- <view class="save" bindtap="sendMessage">发送消息</view>
- // index.js
- //发送消息
- sendMessage() {
- wx.cloud.callFunction({
- name: 'signMessage',
- //data是用来传给云函数event的数据,你可以把你当前页面获取消息填写到服务通知里面
- data: {
- action: 'sendSubscribeMessage',
- title:'xxx',
- name:'xxx',
- _openid: user._id, //需要发送用户的openid
- },
- success: res => {
- wx.showToast({
- title: '发送成功',
- })
- resolve(res);
- },
- fail: err => {
- wx.showToast({
- icon: 'none',
- title: '调用失败',
- })
- reject(new Error('[云函数] [openapi] subscribeMessage.send 调用失败'))
- }
- })
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |