taro实现订阅消息推送

文档: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html

获取模板 ID

https://mp.weixin.qq.com
在这里插入图片描述

获取订阅消息的权限

Taro.requestSubscribeMessage({
  tmplIds: [模板id],
  success: function (res) {
    console.log(res)
  },
  fail: function (err) {
    console.log(err)
  }
})

会显示当前弹框 如果点击总是保持以上选择 不在询问 则表示下次订阅调用 Taro.requestSubscribeMessage 不会弹窗,保持之前的选择,修改选择需要打开小程序设置进行修改
在这里插入图片描述

调用接口下发订阅消息 这一步应在服务器端进行调用

  1. 首先需要通过Taro.login中的code获取openid
Taro.login({
    success: function (res) {
      console.log(res.code, 'componentDidMount')
      Taro.request({
	      url: `https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code=${res.code}&grant_type=authorization_code`,
	      success: function (res) {
	        console.log(res.data.openid, 'getOpenId')
	      }
    	})
    }
  })
  1. 获取access_token
Taro.request({
	url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret',
	success: function (res) {
  		console.log(res.data.access_token, 'getAccessToken')
	}
})
  1. 发送订阅消息
Taro.request({
  url: `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${accessToken}`,
  method: 'POST',
  data: {
    "touser": openid,
    "template_id": "ge5myAM61zTeWk8Uwzzw8101WYaB3aj9Idrrf-H1bMg", // 模板id
    "page": "pages/routeNav/index", // 点击要跳转的页面
    // "miniprogram_state": "developer", // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
    "lang": "zh_CN", // 进入小程序查看”的语言类型
    "data": { // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
      "thing1": {
        "value": "打卡"
      },
      "time2": {
        "value": '2015年01月05日'
      },
      "character_string3": {
        "value": "80%"
      },
      "thing4": {
        "value": "拍照打卡"
      }
    }
  },
  success: function (res) {
    console.log(res)
  }
})

注意: 正确填写value的格式 否则会报错 具体查看文档 链接

会用到的接口文档链接

  1. auth.getAccessToken | 微信开放文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
  2. login 接口 可获取openid uniconid session_key等信息: https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
  3. 发送订阅消息: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
  4. 开通订阅消息权限: https://developers.weixin.qq.com/miniprogram/dev/api/open-api/subscribe-message/wx.requestSubscribeMessage.html
  5. 后端处理 消息推送: https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html#option-url
  6. openid 获取: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值