云开发学习8--用户订阅消息推送(用于类似订阅课程到时上课提前提醒)

实现流程

  • 1.通过原生云函数的默认函数获取用户openid
  • 2.获取用户授权,获取一次授权就可以发送一次订阅消息,所以多引导用户多点击订阅消息授权。
  • 3.实现发送订阅给单个授权用户
  • 4.实现发送订阅给多个授权用户

一,获取用户openid

  • 原生云函数内容
    在这里插入图片描述
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()

  return {
    event,
    openid: wxContext.OPENID,//通过该参数来获取当前用户的openid
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}
  • 调用云函数方法获取到用户openid
  //获取用户openid
  getOpenid() {
    wx.cloud.callFunction({
      name: "yunkaifa_getopenid"
    }).then(res => {
      console.log("获取openid成功", res)
      userid = res.result.openid
    }).catch(res => {
      console.log("获取openid失败", res)
    })
  },

二,生成订阅模板并引导用户进行授权-默认对当前用户进行授权(尽量引导用户多点击授权,具体可参考美团优选小程序一开始的授权引导)

在这里插入图片描述

//获取用户授权,获取一次授权就可以发送一次订阅消息,所以多引导用户多点击订阅消息授权。
  shouquan() {
    wx.requestSubscribeMessage({
      // 1.登录小程序管理平台
      // 2.选择左侧的订阅消息
      // 3.点击同意开启该功能
      // 4.选用模板后即可获取模板id
      tmplIds: ['-t0yjI0tuZowJtpJxwQNaCH1uRQaMTMJSWhMlOjs92w'], //这里填入我们生成的模板id
      success(res) {
        console.log('授权成功', res)
      },
      fail(res) {
        console.log('授权失败', res)
      }
    })
  },

三,发送订阅消息给单个授权用户

在这里插入图片描述
在这里插入图片描述

 //发送消息给单个用户
  sendOne() {
    if (name == null || name == '') {
      wx.showToast({
        icon: "none",
        title: '请输入日程主题',
      })
      return
    }
    //这里的用户id给指定为自己的id了,如需指定可以按照注释方式指定
    // this.sendFun("oucwI5bkpw2xgXN8kUameZfbzAAE", name)
    console.log(userid)
    this.sendFun(userid, name)//发送订阅消息实现函数(云函数)
  },
  //封装的方式方法
  sendFun(openid, name) {
    wx.cloud.callFunction({
      name: "yunkaifa_dingyuetuisong",
      data: {
        openid: openid,
        name: name
      }
    }).then(res => {
      console.log("发送多条成功", res)
    }).catch(res => {
      console.log("发送多条失败", res)
    })
  }

四,发送订阅消息给多个授权用户

在这里插入图片描述

//发送订阅消息给多个用户
  sendAll() {
    if (name == null || name == '') {
      wx.showToast({
        icon: "none",
        title: '请输入日程主题',
      })
      return
    }
    //指定多个用户id
    let users = [
      "oucwI5bkpw2xgXN8kUameZfbzAAE",
      // "oc4sa0dZ-pSCu95djiLCt7jo97bY" 因为没有多个用户id,所以在这里仅演示功能,如有多个id可自己设定
    ]
    //循环调用发送函数给多个用户发送订阅消息
    users.forEach(item => {
      console.log("for循环", item)
      this.sendFun(item, name)
    })
  },
  //封装的方式方法
  sendFun(openid, name) {
    wx.cloud.callFunction({
      name: "yunkaifa_dingyuetuisong",
      data: {
        openid: openid,
        name: name
      }
    }).then(res => {
      console.log("发送多条成功", res)
    }).catch(res => {
      console.log("发送多条失败", res)
    })
  }

五,整体代码

  • 云函数yunkaifa_dingyuetuisong
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async(event, context) => {
  try {
    const result = await cloud.openapi.subscribeMessage.send({
      touser: event.openid, //要推送给哪个用户
      page: 'pages/index/index', //要跳转到那个小程序页面,点开订阅消息时。一般都是跳转到小程序首页
      data: { //推送的内容
        thing1: {
          value: event.name
        },
        time3: {
          value: '2020年3月10日 14:00'
        },
        thing4: {
          value: '杭州市浙江大学'
        },
      },
      templateId: '-t0yjI0tuZowJtpJxwQNaCH1uRQaMTMJSWhMlOjs92w' //模板id
    })
    console.log(result)
    return result
  } catch (err) {
    console.log(err)
    return err
  }
}
  • 云函数yunkaifa_getopenid
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()

  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}
  • index.wxss
/**index.wxss**/
.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #aaa;
}

.usermotto {
  margin-top: 200px;
}
  • index.wxml
<button bindtap="getOpenid">1:获取用户openid</button>
<button bindtap="shouquan">2:获取用户授权</button>
<input placeholder="输入日程主题" bindinput="getName"></input>
<button bindtap="sendOne">3:发送订阅消息给单个用户</button>
<button bindtap="sendAll">4:发送订阅消息给多个用户</button>
  • index.js
let name = ''
let userid = ""
Page({
  //获取用户openid
  getOpenid() {
    wx.cloud.callFunction({
      name: "yunkaifa_getopenid"
    }).then(res => {
      console.log("获取openid成功", res)
      userid = res.result.openid
    }).catch(res => {
      console.log("获取openid失败", res)
    })
  },
  //获取用户授权,获取一次授权就可以发送一次订阅消息,所以多引导用户多点击订阅消息授权。
  shouquan() {
    wx.requestSubscribeMessage({
      // 1.登录小程序管理平台
      // 2.选择左侧的订阅消息
      // 3.点击同意开启该功能
      // 4.选用模板后即可获取模板id
      tmplIds: ['-t0yjI0tuZowJtpJxwQNaCH1uRQaMTMJSWhMlOjs92w'], //这里填入我们生成的模板id
      success(res) {
        console.log('授权成功', res)
      },
      fail(res) {
        console.log('授权失败', res)
      }
    })
  },
  //获取用户输入的日程主题
  getName(event) {
    name = event.detail.value
  },
  //发送消息给单个用户
  sendOne() {
    if (name == null || name == '') {
      wx.showToast({
        icon: "none",
        title: '请输入日程主题',
      })
      return
    }
    //这里的用户id给指定为自己的id了,如需指定可以按照注释方式指定
    // this.sendFun("oucwI5bkpw2xgXN8kUameZfbzAAE", name)
    console.log(userid)
    this.sendFun(userid, name)
  },
  //发送订阅消息给多个用户
  sendAll() {
    if (name == null || name == '') {
      wx.showToast({
        icon: "none",
        title: '请输入日程主题',
      })
      return
    }
    //指定多个用户id
    let users = [
      "oucwI5bkpw2xgXN8kUameZfbzAAE",
      // "oc4sa0dZ-pSCu95djiLCt7jo97bY" 因为没有多个用户id,所以在这里仅演示功能,如有多个id可自己设定
    ]
    //循环调用发送函数给多个用户发送订阅消息
    users.forEach(item => {
      console.log("for循环", item)
      this.sendFun(item, name)
    })
  },
  //封装的方式方法
  sendFun(openid, name) {
    wx.cloud.callFunction({
      name: "yunkaifa_dingyuetuisong",
      data: {
        openid: openid,
        name: name
      }
    }).then(res => {
      console.log("发送多条成功", res)
    }).catch(res => {
      console.log("发送多条失败", res)
    })
  }
})
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中实现订阅消息推送功能,你可以使用第三方库requests来发送HTTP请求。以下是一个示例代码,展示了如何发送微信订阅消息推送: ```python import requests def send_wechat(msg): token = '你的那个' title = 'title1' content = msg template = 'html' url = f"https://www.pushplus.plus/send?token={token}&title={title}&content={content}&template={template}" r = requests.get(url=url) print(r.text) if __name__ == '__main__': msg = 'Life is short, I use python' send_wechat(msg) ``` 请确保你替换代码中的`token`和`title`,并将`content`设置为你要发送的消息内容。通过发送GET请求,将消息内容和其他参数作为URL的查询参数传递给PushPlus接口。最后,你可以通过`requests`库发送请求,并打印响应结果。这样,你就可以在Python服务号中订阅消息推送了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Python推送消息](https://blog.csdn.net/weixin_52051554/article/details/130301479)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [wechat-notification:通过微信公众号, 将通知信息推送至个人微信. 无需认证公众号, 可群发](https://download.csdn.net/download/weixin_42134769/18824083)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值