pushplus推送加demo代码

Demo代码

python代码示例

使用python语言演示具体如何发送消息

  • 获取自己的token。
    可到pushplus的官网登录获取下自己的token。pushplus的网址是:http://www.pushplus.plus
  • 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)
  • 运行后效果
    效果1

效果2

  • 说明
    更多参数用法请查看pushplus接口文档

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请求方式代码
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());
            }
        }
    }
}

  • 说明
    示例用使用redis来做全局变量判断是否超过正常请求次数,以防止超额后还继续请求造成封号。您可以自行选择使用其他方式(如memoryCache)来替代全局判断。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 可以使用 WebSocket 技术向客户端推送通知,下面是一个简单的 demo: 首先,需要在 pom.xml 中添 WebSocket 相关的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 然后,在 Spring Boot 应用程序中创建一个 WebSocket 配置: ```java @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(new NotificationHandler(), "/notification").setAllowedOrigins("*"); } } ``` 在上面的代码中,我们创建了一个 WebSocket 处理程序,并将其映射到 "/notification" 路径上。 下面是 WebSocket 处理程序的实现: ```java public class NotificationHandler extends TextWebSocketHandler { private static final List<WebSocketSession> sessions = new ArrayList<>(); @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { sessions.add(session); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { for (WebSocketSession s : sessions) { s.sendMessage(message); } } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { sessions.remove(session); } } ``` 在上面的代码中,我们创建了一个静态列表 sessions,用于保存所有的 WebSocket 会话。在 afterConnectionEstablished 方法中,我们将新的会话添到 sessions 列表中;在 handleTextMessage 方法中,我们将接收到的消息广播给所有的会话;在 afterConnectionClosed 方法中,我们将会话从 sessions 列表中删除。 最后,在客户端中创建一个 WebSocket 连接: ```javascript var socket = new WebSocket("ws://localhost:8080/notification"); socket.onmessage = function(event) { var message = event.data; // 处理接收到的消息 }; ``` 在上面的代码中,我们创建了一个 WebSocket 连接,将其连接到服务器上的 "/notification" 路径,并在收到消息时处理接收到的消息。 这就是一个简单的 Spring Boot 推送通知的 demo,希望能够对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值