5行代碼實現微信小程序模版消息推送 (含推送後臺和小程序源碼)

咱們在作小程序開發時,消息推送是不可避免的。今天就來教你們如何實現小程序消息推送的後臺和前臺開發。源碼會在文章末尾貼出來。java

其實我以前有寫過一篇:《springboot實現微信消息推送,java實現小程序推送,含小程序端實現代碼》 可是有同窗反應這篇文章裏的代碼太繁瑣,接入也比較麻煩。今天就來給你們寫個精簡版的,基本上只須要幾行代碼,就能實現小程序模版消息推送功能。git

老規矩先看效果圖 github

這是咱們最終推送給用戶的模版消息。這是用戶手機微信上顯示的推送消息截圖。

本節知識點

1,java開發推送後臺 2,springboot實現推送功能 3,小程序獲取用戶openid 4,小程序獲取fromid用來推送web

先來看後臺推送功能的實現

只有下面一個簡單的PushController類,就能夠實現小程序消息的推送 spring

再來看下PushController類,你沒看錯,實現小程序消息推送,就須要下面這幾行代碼就能夠實現了。
因爲本推送代碼是用springboot來實現的,下面就來簡單的講下。我咱們須要注意的幾點內容。 1,須要在pom.xml引入一個三方類庫(推送的三方類庫)
pom.xml的完整代碼以下

<?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

下面就是小程序部分,來教你們如何獲取上面所需的5個信息。

1,appid和AppSecret的獲取(登陸小程序管理後臺) 編程

2,推送模版id
3,用戶openid的獲取,能夠看下面的這篇文章,也能夠看源碼,這裏不作具體講解 小程序開發如何獲取用戶openid 4,獲取formid
看官方文檔,能夠知道咱們的formid有效期是7天,而且一個form_id只能使用一次,因此咱們小程序端所須要作的就是儘量的多拿些formid,而後傳個後臺,讓後臺存到數據庫中,這樣7天有效期內,想怎麼用就怎麼用了。

因此接下來要講的就是小程序開發怎麼儘量多的拿到formid了

看下官方提供的,只有在表單提交時把report-submit設爲true時才能拿到formid,好比這樣

<form report-submit='true' >
   <button  form-type='submit'>獲取formid</button>
  </form>
複製代碼

因此咱們就要在這裏下功夫了,既然只能在form組件獲取,咱們能不能把咱們小程序裏用到最多的地方用form來假裝呢。小程序

下面簡單寫個獲取formid和openid的完整示例,方便你們學習

效果圖 微信小程序

咱們要作的就是點擊獲取formid按鈕,能夠獲取到用戶的formid和openid,正常咱們開發時,是須要把openid和formid傳給後臺的,這裏簡單起見,咱們直接用獲取到的formid和openid實現推送功能

下面來看小程序端的實現代碼

1,index.wxml

2,index.js

到這裏咱們小程序端的代碼也實現了,接下來測試下推送。

formid:  6ee9ce80c1ed4a2f887fccddf87686eb
openid o3DoL0Uusu1URBJK0NJ4jD1LrRe0
複製代碼

能夠看到咱們用了上面獲取到的openid和formid作了一次推送,顯示推送成功

到這裏咱們小程序消息推送的後臺和小程序端都講完了。

這裏有兩點須要你們注意

1,推送的openid和formid必須對應。 2,一個formid只能用一次,屢次使用會報一下錯誤。

{"errcode":41029,"errmsg":"form id used count reach limit hint: [ssun8a09984113]"}
複製代碼

編程小石頭,碼農一枚,非著名全棧開發人員。分享本身的一些經驗,學習心得,但願後來人少走彎路,少填坑。

這裏就不單獨貼出源碼下載連接了,你們感興趣的話,能夠私信我,或者在底部留言,我會把源碼下載連接貼在留言區。 單獨找我要源碼也行(微信2501902696)

源碼連接:github.com/qiushi123/w…

相關文章
相關標籤/搜索