微信小程序下发消息通知

话不多说,先熟悉微信小程序官网操作逻辑实现:(别搞错了!)

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html

步骤一:获取模板 ID (这个没什么好说的,根据界面去配置模板就好)

步骤二:获取下发权限 (前端的活,哈哈…)

let temId = 'QWERTYUOP1234567890' // 小程序配置模板信息-模板ID
wx.getSetting({
    
    
	withSubscriptions: true,//是否同时获取用户订阅消息的订阅状态,默认不获取
	success: (res)=> {
    
    
		console.log(res)
		if (res.subscriptionsSetting && res.subscriptionsSetting.itemSettings &&
			res.subscriptionsSetting.itemSettings[temId] == "reject"){
    
    
			//打开设置去设置
			this.openConfirm('检测到您没打开推送权限,是否去设置打开?')
		}else {
    
    
			wx.requestSubscribeMessage({
    
    
				tmplIds: [temId],
				success: (res)=> {
    
    
					if (res[temId] == 'accept'){
    
    
						let obj ={
    
     "thing1": {
    
     "value": '风里来雨里去' }, "name2": {
    
     "value": '张三' } ,"date6": {
    
     "value": '2021-03-29' } ,"amount7": {
    
     "value": '200' } ,"thing3": {
    
     "value": '远航' } }
						_that.$http.request({
    
    
							url:'/api/applets/xxxxxx',
							method:"post",
							data:{
    
    
								templateId: temId,
								pageUrl:'/pages/index/index',
								jsonData: JSON.stringify(obj),
							}
						}).then(res=>{
    
    
							console.log(res);
						})
					}
				},
				fail: (res)=> {
    
     console.info(res) },
			})
		}
	}
})

步骤三:调用接口下发订阅消息(用户登录时记得存储openId,后续会用到)

调用接口时最好是真机调试

下发消息通知:
POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN

特别注意参数类别与参数值限制

在这里插入图片描述

这里涉及到两个API:一个获取token另外一个下发消息

 /**
     * 返回的 JSON 数据包
     * @param appId
     * @param appSecret
     * @return
     */
    // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
    public static String getWxMiniToken(String appId, String appSecret) {
    
    
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token";
        Map<String, String> requestParam = new HashMap<>();
        requestParam.put("grant_type", "client_credential");     //默认参数
        requestParam.put("appid", appId);                        //小程序appId
        requestParam.put("secret", appSecret);                   //小程序secret
        return UrlUtils.sendGet(requestUrl, requestParam);
    }

    /**
     * 微信小程序-订阅 下发消息通知
     * @param accessToken
     * @param openId
     * @param templateId
     * @param pageUrl
     * @param jsonData
     * @return
     */
    public static String wxMiniSend(String wxModel, String accessToken, String openId, String templateId, String pageUrl, String jsonData){
    
    
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
        Map<String, Object> requestParam = new HashMap<>();
        requestParam.put("touser", openId);             //接收者(用户)的 openid
        requestParam.put("template_id", templateId);    //所需下发的订阅模板id
        requestParam.put("page", pageUrl);              //点击模板卡片后的跳转页面,仅限本小程序内的页面
        requestParam.put("data", JSONObject.parse(jsonData));       //模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
        requestParam.put("miniprogram_state", wxModel);         //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
        requestParam.put("lang", "zh_CN");                          //语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
        return UrlUtils.sendPostJson(requestUrl, JSONObject.toJSONString(requestParam));
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_16771097/article/details/115306308