Wechat applet’s message push has been updated from the previous template message to the current subscription message. This article will teach you how to use the applet to subscribe to the message.

1. Start by adding a subscription message template

Log in the background of wechat mini program and select subscribe message

Choose a template that works for you

And then we get our template ID

2. Add access code to the small program

It’s very clear in the documentation here, adding access code at some point in the click event

Official documentation link: Click here

wx.requestSubscribeMessage({
  tmplIds: [' '],
  success (res) { }
})
Copy the code

When the user clicks, it looks like this: the user clicks once and gets permission to send a subscription message once

3. Send the subscription message

Official documentation link: Click here

1. Let’s take a look at the official API
POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
Copy the code

ACCESS_TOKEN = ACCESS_TOKEN = ACCESS_TOKEN = ACCESS_TOKEN = ACCESS_TOKEN

2. Obtain ACCESS_TOKEN
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
Copy the code

We need to fill in the appID and secret of the applet and send a GET request to the link, which will return us the ACCESS_TOKEN

We can test this API with Postman

It can be seen that access_token has been successfully obtained. We put it into the API required for sending subscription messages. This API is a POST request, and the data to be sent is also provided by the official document

3. Test sending subscription messages

We get the subscription message template ID, access_token, and can send the subscription based on the data given by the document, using the Postman test

This allows you to send a subscription message

Finally, I write a periodic send small program subscription message code: github.com/zhongjidala…

On GitHub, please feel free to send Issues if you have any questions or click star if you think it’s a good one