咱们在作小程序开发时,消息推送是不可避免的。今天就来教你们如何实现小程序消息推送的后台和前台开发。源码会在文章末尾贴出来。java
其实我以前有写过一篇:《springboot实现微信消息推送,java实现小程序推送,含小程序端实现代码》 可是有同窗反应这篇文章里的代码太繁琐,接入也比较麻烦。今天就来给你们写个精简版的,基本上只须要几行代码,就能实现小程序模版消息推送功能。git
老规矩先看效果图 github
1,java开发推送后台 2,springboot实现推送功能 3,小程序获取用户openid 4,小程序获取fromid用来推送web
只有下面一个简单的PushController类,就能够实现小程序消息的推送 spring
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.qcl</groupId>
<artifactId>wxapppush</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wxapppush</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--微信小程序模版推送-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
复制代码
其实到这里咱们java后台的推送功能,就已经实现了。咱们只须要运行springboot项目,就能够实现推送了。 下面贴出完整的PushController.java类。里面注释很详细了。数据库
package com.qcl.wxapppush;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import me.chanjar.weixin.common.error.WxErrorException;
/**
* Created by qcl on 2019-05-20
* 微信:2501902696
* desc: 微信小程序模版推送实现
*/
@RestController
public class PushController {
@GetMapping("/push")
public String push(@RequestParam String openid, @RequestParam String formid) {
//1,配置小程序信息
WxMaInMemoryConfig wxConfig = new WxMaInMemoryConfig();
wxConfig.setAppid("wx7c54942dfc87f4d8");//小程序appid
wxConfig.setSecret("5873a729c365b65ab42bb5fc82d2ed49");//小程序AppSecret
WxMaService wxMaService = new WxMaServiceImpl();
wxMaService.setWxMaConfig(wxConfig);
//2,设置模版信息(keyword1:类型,keyword2:内容)
List<WxMaTemplateData> templateDataList = new ArrayList<>(2);
WxMaTemplateData data1 = new WxMaTemplateData("keyword1", "获取老师微信");
WxMaTemplateData data2 = new WxMaTemplateData("keyword2", "2501902696");
templateDataList.add(data1);
templateDataList.add(data2);
//3,设置推送消息
WxMaTemplateMessage templateMessage = WxMaTemplateMessage.builder()
.toUser(openid)//要推送的用户openid
.formId(formid)//收集到的formid
.templateId("eDZCu__qIz64Xx19dAoKg0Taf5AAoDmhUHprF6CAd4A")//推送的模版id(在小程序后台设置)
.data(templateDataList)//模版信息
.page("pages/index/index")//要跳转到小程序那个页面
.build();
//4,发起推送
try { wxMaService.getMsgService().sendTemplateMsg(templateMessage);
} catch (WxErrorException e) {
System.out.println("推送失败:" + e.getMessage());
return e.getMessage();
}
return "推送成功";
}
}
复制代码
看代码咱们能够知道,咱们须要作一些配置,须要下面信息 1,小程序appid 2,小程序AppSecret(密匙) 3,小程序推送模版id 4,用户的openid 5,用户的formid(一个formid只能用一次)apache
1,appid和AppSecret的获取(登陆小程序管理后台) 编程
<form report-submit='true' >
<button form-type='submit'>获取formid</button>
</form>
复制代码
因此咱们就要在这里下功夫了,既然只能在form组件获取,咱们能不能把咱们小程序里用到最多的地方用form来假装呢。小程序
效果图 微信小程序
1,index.wxml
到这里咱们小程序端的代码也实现了,接下来测试下推送。
formid: 6ee9ce80c1ed4a2f887fccddf87686eb
openid o3DoL0Uusu1URBJK0NJ4jD1LrRe0
复制代码
到这里咱们小程序消息推送的后台和小程序端都讲完了。
1,推送的openid和formid必须对应。 2,一个formid只能用一次,屡次使用会报一下错误。
{"errcode":41029,"errmsg":"form id used count reach limit hint: [ssun8a09984113]"}
复制代码
编程小石头,码农一枚,非著名全栈开发人员。分享本身的一些经验,学习心得,但愿后来人少走弯路,少填坑。
这里就不单独贴出源码下载连接了,你们感兴趣的话,能够私信我,或者在底部留言,我会把源码下载连接贴在留言区。 单独找我要源码也行(微信2501902696)