Get the App
SLTechnology News&Howtos  ›  Development  › 

How does WeChat Mini Programs realize the function of subscribing to messages

Shulou Source: shulou.com Published: 2022-05-31 22:59:50 09月21日 Update

This article mainly introduces the relevant knowledge of "how to achieve the subscription message function of WeChat Mini Programs". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to achieve subscription message function by WeChat Mini Programs" can help you solve the problem.

Mini Program template messages are about to be discarded, so there is a new interface wx.requestSubscribeMessage

Subscribe to message documents

Steps:

1. Get the user's openid and access_token

2. Get the template ID

3. Obtain the distribution permission (api)

4. Send subscription message (api)

Template id

Mini Program Code:

Obtain the permission to issue:

/ / login.jsrequestMsg () {return new Promise ((resolve, reject) = > {wx.requestSubscribeMessage ({tmplIds: ["MUtLwsw0xCndRULTgNHiXwGDyHJ-ZwAFL-b3kALcl0c"], success: (res) = > {if (res ['MUtLwsw0xCndRULTgNHiXwGDyHJ-ZwAFL-b3kALcl0c'] =' accept') {wx.showToast ({title: 'subscribe to OK') , duration: 1000, success (data) {/ / successful resolve ()}, fail (err) {/ / failed console.error (err) Reject ()}})} / / index.wxml occurrence subscription message / / index.js / / Click occurrence subscription sendMsg:function (e) {wx.request ({url: 'https://cff.mynatapp.cc/wxXcx/sendMessage', method:' POST', data: {}, header: {'content-type':' application/json' / / default}, success (res) {console.log (res)}

Nodejs (koa2) code:

Send a subscription message:

/ / index.jsconst router = require ('koa-router') () const request = require (' superagent') let AppID = 'xxxxx'let AppSecret =' xxxxx'let access_token = 'xxxxx'let openid =' xxxxx'// Click subscription message router.post ('/ wxXcx/sendMessage', async (ctx, next) = > {let requestData = {"touser": openid, "template_id": "MUtLwsw0xCndRULTgNHiXwGDyHJ-ZwAFL-b3kALcl0c", "page": "index" "data": {"phrase1": {"value": "Wang Xiaoer"}, "date5": {"value": "15:01 on October 1st, 2019"}, "phrase4": {"value": "Miss Wang"}, "thing2": {"value": "ps introduction to mastery"} "character_string6": {"value": "1Pyro 10"} / / 2, send template message let res = await request .post (`Accept',. Send (requestData) .set ('Accept',' application/json') console.log (res) ctx.body = {code: '200, data: null Msg: 'operation succeeded'}})

This is the end of the content about "how to achieve the subscription message function of WeChat Mini Programs". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Messages subscriptions programs templates functions knowledge success code permissions industries different practical content practical practical interfaces documents articles methods more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Huawei Shulou Tech Info Redmi