How does the uniapp applet push subscription messages to users? And the subscription message parameter value content restriction rules

How does the uniapp applet subscribe to messages and push messages to users?

The template push of the mini program is divided into "one-time subscription" and "long-term subscription"
One-time subscription: After the user subscribes to the mini program, the program can only push the specified OpenId once Template messages cannot be pushed multiple times
Long-term subscription: Users subscribe for a long time and can push template messages multiple times (long-term subscription templates need to apply to WeChat official)

In order to understand this article more comprehensively, it is all written on the front-end page for debugging. During development, after the front-end obtains user authorization, it is processed by the back-end.

<template>
	<view>
		<view class="">
			<button @click="openid">获取openid</button>
		</view>
		<view class="">
			<button @click="getToken">获取token</button>
		</view>
		<view class="">
			<button @click='notification'>推送消息</button>
		</view>
	</view>
</template>

Look for the mini program APPID and mini program key in the WeChat public platform,
Among them, thing1, time2, number3, thing4, and thing6 are defined according to the template in the mini program, Pay attention to the template usage rules
The background also needs to add fields corresponding to the template content
Insert image description here

return:{
    
    
		useropenid:'',
		mytoken:'',
		tmplIdsone:'', // 要发送订阅的模板id
		wxappid:'',  // 小程序appID
		wxsecret:'',  // 小程序密钥
		pushmsg: {
    
    
			"touser": 'xxx',		// 获取到的用户openid
			"template_id": "xxxx", // 对应的模板id(微信公众平台中的订阅消息,选择对应模板)
			"data": {
    
    
				"thing1": {
    
    
					"value": "李四"
				},
				"time2": {
    
    
					"value": "2021年4月14日 14:05"
				},
				"number3": {
    
    
					"value": "10"
				},
				"thing4": {
    
    
					"value": "20"
				},
				"thing6": {
    
    
					"value": "张三"
				}
			},
			"page":""  //进入哪个页面
		},
}

Calling API to obtain the openid of the logged-in user
Among them, appid and secret can be found on the public platform

async openid() {
    
    
	wx.login({
    
    
		success(res) {
    
    
			if (res.code) {
    
    
				wx.request({
    
    
					url: `https://api.weixin.qq.com/sns/jscode2session?appid=${
      
      this.wxappid}&secret=${
      
      this.wxsecret}&js_code=${
      
      res.code}&grant_type=authorization_code`,
					success(data) {
    
    
						this.useropenid = data.data.openid
						this.pushmsg.touser = this.useropenid
					}
				})
			} else {
    
    
				console.log('获取失败!' + res.errMsg)
			}
		}
	})
},

wx.requestSubscribeMessage(), obtain user authorization
Obtain the token of the push message template
Call the development document Api according to AppId and Secret to obtain the token a>

getToken() {
    
    
	wx.requestSubscribeMessage({
    
    
		tmplIds: [this.tmplIdsone],   // 要发送订阅的模板id
		success(res) {
    
    
			for (let key in res) {
    
    
				if (res[key] == "accept") {
    
    
					uni.showToast({
    
    
						title:'已允许消息推送'
					})
					wx.request({
    
    
						url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${
      
      this.wxappid}&secret=${
      
      this.wxsecret}`,
						data: {
    
    },
						success: function(res) {
    
    
							this.mytoken = res.data.access_token
						}
					})
				} else{
    
    
				}
			}
		},
		fail: function(res) {
    
    
		}
	})
},

Call message push

notification() {
    
    
		wx.request({
    
    
			url: 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + this.mytoken,
			data: JSON.stringify(this.pushmsg),
			method: 'POST',
			success: function(res) {
    
    
				console.log(res)
			},
		})
	}

Push subscription message results
Insert image description here

Mini program subscription message parameter value content restriction rules

Insert image description here


If it is useful to you, please add it to your collection, follow it and give it a like

Guess you like

Origin blog.csdn.net/m0_57611486/article/details/128478307