当前位置:   article > 正文

5行java代码实现微信小程序模版消息推送 (含springboot后台和小程序源码)_微信小程序消息推送 开发java

微信小程序消息推送 开发java

难道这样就够了吗?不,远远不够!

提前多熟悉阿里往年的面试题肯定是对面试有很大的帮助的,但是作为技术性职业,手里有实打实的技术才是你面对面试官最有用的利器,这是从内在散发出来的自信。

备战阿里时我花的最多的时间就是在学习技术上,占了我所有学习计划中的百分之70,这是一些我学习期间觉得还是很不错的一些学习笔记

我为什么要写这篇文章呢,其实我觉得学习是不能停下脚步的,在网络上和大家一起分享,一起讨论,不单单可以遇到更多一样的人,还可以扩大自己的眼界,学习到更多的技术,我还会在csdn、博客、掘金等网站上分享技术,这也是一种学习的方法。

今天就分享到这里了,谢谢大家的关注,以后会分享更多的干货给大家!

阿里一面就落马,恶补完这份“阿里面试宝典”后,上岸蚂蚁金服

阿里一面就落马,恶补完这份“阿里面试宝典”后,上岸蚂蚁金服

image.png

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

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>



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

其实到这里我们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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

看代码我们可以知道,我们需要做一些配置,需要下面信息

1,小程序appid

2,小程序AppSecret(密匙)

3,小程序推送模版id

4,用户的openid

5,用户的formid(一个formid只能用一次)

下面就是小程序部分,来教大家如何获取上面所需的5个信息。

=========================================================================================

1,appid和AppSecret的获取(登录小程序管理后台)

在这里插入图片描述

2,推送模版id

在这里插入图片描述

3,用户openid的获取,可以看下面的这篇文章,也可以看源码,这里不做具体讲解

小程序开发如何获取用户openid

4,获取formid

在这里插入图片描述

看官方文档,可以知道我们的formid有效期是7天,并且一个form_id只能使用一次,所以我们小程序端所需要做的就是尽可能的多拿些formid,然后传个后台,让后台存到数据库中,这样7天有效期内,想怎么用就怎么用了。

一线互联网大厂Java核心面试题库

image

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

ing全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号