在自己编写程序的基础上,对于某些特定的功能需要实现在自编程序端,进行某些开发消息,推送到用户绑定的微信上,对此的开发较为麻烦,且维护的工程量较为大,投入的工作也会较多;pushplus即是一个较为简单的消息推送的功能,可以在写在服务器端,通过发送的消息,了解服务器的相关的运行情况和登录情况;
pushplus(推送加)是一个集成了微信、短信、邮件、企业微信、HiFlow连接器、钉钉、飞书等实时消息推送平台。只需要调用简单的API,即可帮您迅速完成消息的推送,使用简单方便。
pushplus的目的就是大幅简化消息类推送功能的开发。像是微信公众号的主动推送技术上并不复杂,但是需要认证服务号,备案的域名。这就导致了个人用户与模板消息无缘。而很多时候开发者也只需要一个简单的提醒功能,单独去维护一个推送项目,成本太大,所以pushplus就是为了解决这些用户的痛点,为帮助普通用户和开发者而来的。
Python代码示例:
- 获取自己的token。
可到pushplus的官网登录获取下自己的token。pushplus的网址是:http://www.pushplus.plus(opens new window) - GET请求方式代码
-
# encoding:utf-8 import requests token = '你的token' #在pushplus网站中可以找到 title= '标题' #改成你要的标题内容 content ='内容' #改成你要的正文内容 url = 'http://www.pushplus.plus/send?token='+token+'&title='+title+'&content='+content requests.get(url)
POST请求方式代码:
-
# encoding:utf-8 import requests import json token = '你的token' #在pushpush网站中可以找到 title= '标题' #改成你要的标题内容 content ='内容' #改成你要的正文内容 url = 'http://www.pushplus.plus/send' data = { "token":token, "title":title, "content":content } body=json.dumps(data).encode(encoding='utf-8') headers = {'Content-Type':'application/json'} requests.post(url,data=body,headers=headers)
Java代码:
-
GET请求方式代码
使用的hutools工具类,自定义了redis操作类,ResultT请求响应对象。正式使用的时候改成自己的封装的。 -
//写在SpringBoot项目中的测试类,演示通过get方式发送消息 @SpringBootTest public class PushControllerTest { //自己写的redis操作类 @Autowired private RedisService redisService; @Test public void send(){ //redis的key,可以自己随便命名 String redisKey= "pushplus:canSend"; //读取redis里面的值,是否为1,不为1的才能请求pushplus接口 Integer limit = redisService.get(redisKey)!= null ? (Integer) redisService.get(redisKey):0; if(limit!=1){ String token= "您的token"; //您的token String title= "标题"; //消息的标题 String content= "内容<br/><img src='http://www.pushplus.plus/doc/img/push.png' />"; //消息的内容,包含文字、换行和图片 String url = "https://www.pushplus.plus/send?title="+ title +"&content="+ content +"&token=" + token; //服务器发送Get请求,接收响应内容 String response = HttpUtil.get(url); //把返回的字符串结果变成对象 ResultT resultT = JSONUtil.toBean(response,ResultT.class); //判断返回码是否为900(用户账号使用受限),如果是就修改redis对象,下次请求不在发送 if(resultT.getCode()==900){ //使用redis缓存做全局判断,设置到第二天凌点自动失效 redisService.set(redisKey,1, TimeUtil.getSecondsNextEarlyMorning()); } } } }
POST请求方式代码
//写在SpringBoot项目中的测试类,演示通过get方式发送消息
@SpringBootTest
public class PushControllerTest {
//自己写的redis操作类
@Autowired
private RedisService redisService;
@Test
public void send(){
//redis的key,可以自己随便命名
String redisKey= "pushplus:canSend";
//读取redis里面的值,是否为1,不为1的才能请求pushplus接口
Integer limit = redisService.get(redisKey)!= null ? (Integer) redisService.get(redisKey):0;
if(limit!=1){
String token= "您的token"; //您的token
String title= "标题"; //消息的标题
String content= "内容<br/><img src='http://www.pushplus.plus/doc/img/push.png' />"; //消息的内容,包含文字、换行和图片
String url = "https://www.pushplus.plus/send?title="+ title +"&content="+ content +"&token=" + token;
//服务器发送Get请求,接收响应内容
String response = HttpUtil.get(url);
//把返回的字符串结果变成对象
ResultT resultT = JSONUtil.toBean(response,ResultT.class);
//判断返回码是否为900(用户账号使用受限),如果是就修改redis对象,下次请求不在发送
if(resultT.getCode()==900){
//使用redis缓存做全局判断,设置到第二天凌点自动失效
redisService.set(redisKey,1, TimeUtil.getSecondsNextEarlyMorning());
}
}
}
}
复制代码
POST请求方式代码
public class PushControllerTest {
//自己写的redis操作类
@Autowired
private RedisService redisService;
@Test
public void send(){
//redis的key,可以自己随便命名
String redisKey= "pushplus:canSend";
//读取redis里面的值,是否为1,不为1的才能请求pushplus接口
Integer limit = redisService.get(redisKey)!= null ? (Integer) redisService.get(redisKey):0;
if(limit!=1){
String token= "您的token"; //您的token
String title= "标题"; //消息的标题
String content= "内容<br/><img src='http://www.pushplus.plus/doc/img/push.png' />"; //消息的内容
String url = "https://www.pushplus.plus/send/";
Map<String,Object> map = new HashMap<>();
map.put("token",token);
map.put("title",title);
map.put("content",content);
//服务器发送POST请求,接收响应内容
String response = HttpUtil.post(url,map);
//把返回的字符串结果变成对象
ResultT resultT = JSONUtil.toBean(response,ResultT.class);
//判断返回码是否为900(用户账号使用受限),如果是就修改redis对象,下次请求不在发送
if(resultT.getCode()==900){
//使用redis缓存做全局判断,设置到第二天凌点自动失效
redisService.set(redisKey,1, TimeUtil.getSecondsNextEarlyMorning());
}
}
}
}