Developers.weixin.qq.com/miniprogram… On October 12, 2019, wechat opened the function of subscribing to messages with small programs. Officially, the current template messages are flawed in implementing the applets service loop:

  1. Some developers send irrelevant messages to users without expectation or service, causing harassment to users;
  2. The template message must be delivered within 7 days after users access the applet, which cannot meet the time requirements of some services

Template message of the above damage, not conducive to small program user retention and user experience. To solve these problems, wechat officially launched a user subscription message function. However, the current subscription message is not perfect, there is a lot of room for further optimization. Currently, only “one-time subscription messages” are officially available, while “long-term subscription messages” are only available for offline public services such as government affairs and people’s livelihood, medical care, education, transportation and finance.

Step 1 Obtain the template ID log in to mp.weixin.qq.com to obtain the template. Can be found in the public template library, if there is no suitable template, you can apply for adding a new template, after the review can be used (application button hidden deep, to jump to the last page will see “can’t find the template you want? Help us complete the template library “). After selecting a template, the template ID is automatically generated and then copied.

When applying for a new template, refer to the applet documentation for the keywords to be added

Step 2: Obtaining user authorization is mainly operated on the small program side, and it requires users to have substantial interaction, such as clicking events, to arouse the authorization popup.

Matters needing attention:

  1. If the user selects Cancel, the user refuses to subscribe to the message, and Allow, the user subscribes to the message once
  2. If the user does not select Always keep the above selection, do not ask again, the authorization popup will be triggered each time. Otherwise, the authorization popup will not be pulled up
  3. Check “Keep select” + cancel button, which means that every time this API is called, authorization will be rejected by default; “Keep select” + allow button to automatically authorize each time, keep select function is mainly remember your check
  4. If you want to accept subscription after automatic rejection, you need to click on the upper right corner of the small program — Settings — “Message subscription” to change Settings
  5. Subscribe to the message once click send, authorization has no time limit, as long as it is not sent. After the message is sent once, the user needs to go through the authorization process again to receive the message. Click authorization repeatedly, then the number of sending will be accumulated and deducted one by one

Front-end code:

handleSubmit() {
    this.$refs.uForm.validate(valid= > {
	if (valid) {
            const templateId = ['PBNtcz6vrUU3mrmTvBThUIxeo6bO79GuMn7y53z2qqg'.'o3KTkJtgjZY4cg8UL328s6Z0-nU8hA1WL6sjcUyd-j4']  // One or more template ids in array format
            wx.requestSubscribeMessage({
		tmplIds: templateId,
		success: (res) = > { // Accept accept reject
                    if (res[templateId[0= =]]'accept' || res[templateId[1= =]]'accept') {
			uni.showToast({
                            title: 'Subscription successful'.duration: 2000
			})
                        // Other business codes}},fail:(err) = > {
                    console.log(err)
                }
            })
        }
    })
}
               
Copy the code

Step 3: Invoking the interface to send subscription messages This step is mainly written by the back-end interface. The front-end invokes the message and sends parameters according to the interface requirements.

sendMessage(openid){
    var currentTime = new Date(a);let sendTime = `${currentTime.getFullYear()}-${currentTime.getMonth()+1}-${currentTime.getDate()} ${currentTime.getHours()}:${currentTime.getMinutes()}:${currentTime.getSeconds()}`;
    let params = {
        touser: openid, // The openID of the user receiving the message, provided by the backend
        template_id: 'PBNtcz6vrUU3mrmTvBThUIxeo6bO79GuMn7y53z2qqg'.// Send the template ID
	page: 'pages/index/index? isApproved=true'.// The user clicks the page after receiving the service notification
	data: {
            thing1: {value: 'Notification of Audit Results'}, // "thing1" is the type of the template argument, and value is the displayed content
            name3: {value: uni.getStorageSync('userName')},
            phrase5: {value: 'Passed'},
            date2: {value: sendTime}
	},
	miniprogram_state: 'formal' / / formal version
    }
    httpRequest({
	url: 'wxwms/WXInit/WXSendMsg'.params: JSON.stringify(params)
    }).then(res= > {
        console.log('Sent successfully')})}Copy the code