微信小程序后台添加订阅消息,目前大多数服务类目下仅支持一次性订阅,持续更新中。若没有符合的消息模板,申请自定义模板即可,审核周期是挺长的。
填写模板时,一定要注意参数类型。
小程序发送订阅消息必须要获取用户接收消息的授权
小程序需调用
wx.requestSubscribeMessage({ tmplIds: ['RYdO0OyVwOmFJ5M9whapadK55agtmvWuumc_utFr7D4', 'bgEvkAITRuYbPET0IOPn8QPurVkXP7V6vjfPl7XDzrM'], success(res) { console.log(res); if (res.RYdO0OyVwOmFJ5M9whapadK55agtmvWuumc_utFr7D4 == "accept") { console.log('已授权接收订阅消息'); } } })
服务器端
public void SendNewCouponRemind(string openid, string shopName, string couponTitle, string AddTime, int EndDays) { sub_postdata pdm = new sub_postdata(); pdm.touser = openid; pdm.template_id = "bgEvkAITRuYbPET0IOPn8QPurVkXP7V6vjfPl7XDzrM"; pdm.page = "/pages/discover/discover?p=3"; sub_dataValue dv1 = new sub_dataValue(); dv1.value = shopName; sub_dataValue dv2 = new sub_dataValue(); dv2.value = couponTitle; sub_dataValue dv3 = new sub_dataValue(); dv3.value = AddTime.ToString(); sub_dataValue dv4 = new sub_dataValue(); dv4.value = "领取后" + EndDays.ToString() + "日内"; data_NewCouponRemind dm1 = new data_NewCouponRemind(); dm1.thing7 = dv1; dm1.thing8 = dv2; dm1.date9 = dv3; dm1.thing10 = dv4; pdm.data = dm1; string pd = JsonConvert.SerializeObject(pdm, Formatting.None); SendMsg(pd, 2, 0, openid, couponTitle, shopName); }
1 public void SendMsg(string contentJson, int Type, int UserGetCouponID, string openid, string CouponTitle, string ShopName) 2 { 3 // Hxzs.Common.LogUtil.WriteLog(contentJson); 4 var l = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getaccess_token(); 5 StringBuilder result = Hxzs.Common.HttpRequest.PostPageContent(l, "", contentJson, "UTF-8"); 6 if (!string.IsNullOrEmpty(result.ToString())) 7 { 8 returnjson json = JsonConvert.DeserializeObject<returnjson>(result.ToString()); 9 if (json.errcode == 0) 10 { 11 new Honszeal.Business.classes.Shop_Coupon.userBusiness().SaveSendWxMsgInfo(UserGetCouponID, openid, Type, CouponTitle, ShopName, contentJson); 12 } 13 Hxzs.Common.LogUtil.WriteLog(result.ToString()); 14 } 15 }
不分时间,允许一次可发送一次推送。