SpringBoot opens WeChat public account template message notification


Preface

I have been working on an OA project recently. One project has a business scenario: after the previous user has reviewed it, a notification needs to be sent to the user at the next node. Since we only have mini-programs and PCs, we considered using WeChat messaging.


1. Why use WeChat public account template messages?

The current project only has the WeChat applet, and there are not many options to choose from. Looking at WeChat’s official documents, we found that mini programs and official accounts have their own message subscription functions. Ordinary accounts are only allowed to activate one-time subscription functions , while only accounts in specific industries are allowed to activate long-term subscription functions. Finally, I found that the message template of the official account is more in line with the business scenario. The effect achieved is similar to that after each consumption, the Meituan official account will push the corresponding message without multiple authorizations.
Insert image description here

2. Implementation principle

Utilize the unionID of the WeChat open platform to achieve user sharing among multiple applications. After the user follows the associated official account, he can get the user's unionID and openID. At this time, if the user logs in to the mini program, he can also get the unionID and openID corresponding to the mini program, because in the WeChat system, applications under the same platform can User association is performed through unionID. Therefore, at this time, the openID corresponding to the public account can be found through the unionID of the mini program to push template messages.

3. Implementation steps

1. Create a WeChat open platform account

2. Bind mini program account

Insert image description here

3. Bind official account

Note: Only service accounts can use the message template function, so be sure to pay attention when applying for a public account.
Insert image description here

4. Official account application template message

Log in to the official account, click on the new function, find the template message, and apply for activation.

5. Official account configuration

If you are not familiar with public account development, you can Baidu the necessary configuration information by yourself, mainly the parts marked in red.
Insert image description here

6. Develop WeChat public accounts

Only the key logic code is posted here. After the user follows the official account, the WeChat server will push a message to the server interface we configured. At this time, we need to parse this part of the information and save the openID and unionID.

Get user information

Get accesstoken GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
Get user information GET https://api.weixin.qq.com/cgi-bin/user/ info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

Insert image description here

Template message push

POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

Insert image description here

7. Mini program binding

Configure business domain name

Insert image description here

Implementation ideas

The applet can call the native wx.login() method to obtain the code, and then call the WeChat jscode2session interface in exchange for openID** (this interface will also return unionID information)**. Because the call here does not require authorization from the applet, it can be called silently.

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

Insert image description here


Summarize

Now the message push function in most scenarios can be realized through WeChat public account templates. Whether it is an APP, mini program, or web application, as long as the corresponding application is bound to the same WeChat open platform account .
If you need an official account to jump to a mini program, you only need to bind the official account to the mini program, and then set the app ID and pagePath parameters of the mini program to jump to in the pushed template message.

Official document address

Get public account accessToken
Get public account user information (openID and unionID)
Template message push
Get mini program AccessToken
Get mini program openID and unionId information

Guess you like

Origin blog.csdn.net/XX777666/article/details/126038900