WeChat applet push message


foreword

WeChat applet pushes messages to designated users


1. Push message: Mini Program "Subscribe to Message" function

1. The original "template message" has been deprecated, so you need to use the latest news push interface provided by the official WeChat applet. 2. Why can't you use the
"customer service message" interface: customer service messages, as the name suggests, are given to you as customer service Users answer questions, if your users haven’t spoken for a long time, they are actually offline, and there is no point in sending your messages, so WeChat will give an error of 45015. Therefore, the direction to solve this problem is to change the push message interface.
3. [Recommendation] Use the "Subscribe to Message" function of the Mini Program to achieve the purpose of pushing messages and receive messages in the form of "Service Notifications"
insert image description here

2. Development steps

WeChat mini-program official reference address: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html#%E8%AE%A2%E9%98%85%E6% B6%88%E6%81%AF%E8%AF%AD%E9%9F%B3%E6%8F%90%E9%86%92

1. The applet calls wx.requestSubscribeMessage to authorize message subscription users

WeChat mini-program official reference address: https://developers.weixin.qq.com/miniprogram/dev/api/open-api/subscribe-message/wx.requestSubscribeMessage.html
Subscribe message template configuration: https://mp.weixin .qq.com/wxamp/newtmpl/mytmpl?start=0&limit=10&token=1241600784&lang=zh_CN

The code is as follows (example):

wx.requestSubscribeMessage({
    
    
   tmplIds: ['订阅消息模板ID'],
    success (res) {
    
    
      console.log(res);
    }
  })

2. The applet calls wx.login to get the code

The code is as follows (example):

// 登录
wx.login({
    
    
   success: res => {
    
    
     // 发送 res.code 到后端换取 openId, sessionKey, unionId
     // 后端访问请求获取用户openId
     console.log(res.code);
   },
   fail: res => {
    
    
     // 登录失败
     console.log("登录失败!");
   }
 })

User openid: the same user accesses different applets, and the obtained openid is different, that is, the openid is related to the applet.
User unionId: the same user, for different applications under the same WeChat open platform (the same WeChat open platform account The UnionID is the same for mobile applications, website applications and public accounts (including applets)). The uniqueness of users can be distinguished by UnionID. Official reference address: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html
When verifying user information, [recommended] use unionId as the unique user identifier

3. Backend access request, get user openId

WeChat mini-program official reference address: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html

Use Postman to simulate access requests (example):
insert image description here

4. Back-end access request, obtain the globally unique back-end interface call credential access_token of the applet

WeChat mini-program official reference address: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html

Use Postman to simulate access requests (example):
insert image description here

5. Backend access request, push subscription message

WeChat mini-program official reference address: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html

Use Postman to simulate an access request (example):
insert image description here
subscribe message template:insert image description here

6. The push subscription message ends, and the user successfully receives the message

insert image description here


Summarize

  1. The WeChat Mini Program recommends using the "Subscribe to Message" function to push messages
  2. Preliminary preparation: WeChat Mini Program Management Platform
    • Activate the "Subscribe to News" function of the mini program
    • Configure subscription message "templates"
      • Subscription message templates are divided into two types: "one-time subscription" and "long-term subscription".
      • A one-time subscription template can only be authorized to be sent once at a time.
      • Long-term subscription templates can be authorized once and sent all the time, with no limit on the number of times.
      • Long-term subscription templates are related to the categories configured in the Mini Program, and some categories do not have long-term subscription templates. At present, long-term subscription news is only open to offline public services such as government affairs and people's livelihood, medical care, transportation, finance, and education.
      • If you do not find a subscription template that meets your needs, you can customize and create a new subscription template
  3. Development steps:
    • User authorization to "subscribe to newsletter"
    • Call wx.login to get the code
    • Get user openid
    • Obtain the globally unique background interface call credential access_token of the applet
    • push subscription message

Guess you like

Origin blog.csdn.net/qq_28581175/article/details/124720749