博客
关于我
极光推送Springboot微服务搭建教程
阅读量:667 次
发布时间:2019-03-15

本文共 4329 字,大约阅读时间需要 14 分钟。

极光推送服务配置与使用指南

前言

本文将详细介绍如何在Spring Boot项目中集成极光(JPush)推送服务,并提供三种推送方式的实现方案。

服务端配置文件

在项目中添加极光推送的相关配置,安装完成后即可正常使用。

application.properties 配置

application.properties文件中添加以下配置项:

jpush.appKey=12340856031dc012a7d456
jpush.masterSecret=123493e8a9be718f023023
jpush.liveTime=300000

##Spring Boot配置

添加依赖

在项目的POM文件中添加JPush客户端依赖:

cn.jpush.api
jpush-client
3.3.4

配置文件处理

通过Spring注入配置信息,基本无需修改即可使用。配置示例如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("jpushConfig")
public class JpushConfig {
@Value("${jpush.appKey}")
private String appkey;
@Value("${jpush.masterSecret}")
private String masterSecret;
@Value("${jpush.liveTime}")
private String liveTime;
public String getAppkey() { return appkey; }
public String getMasterSecret() { return masterSecret; }
}

推送服务类封装

宽布包推送

提供多种推送方式的封装实现,确保推送过程高效且可靠。

推送方式

服务端控制推送内容方式

public PushResult sendPush(String title, String content, Map
extrasMap, String... alias) {
ClientConfig clientConfig = ClientConfig.getInstance();
clientConfig.setTimeToLive(Long.valueOf(jpushConfig.getLiveTime()));
JPushClient jpushClient = new JPushClient(jpushConfig.getMasterSecret(), jpushConfig.getAppkey(), null, clientConfig);
PushPayload payload = buildPushPayload(title, content, extrasMap, alias);
try {
return jpushClient.sendPush(payload);
} catch (APIConnectionException e) {
LOG.error("极光推送连接错误,请稍后重试", e);
} catch (APIRequestException e) {
LOG.error("极光服务器响应出错,请修复!", e);
LOG.info("HTTP Status: %d", e.getStatus());
LOG.info("Error Code: %d", e.getErrorCode());
LOG.info("Error Message: %s", e.getErrorMessage());
}
}

服务端控制推送方式(带返回值)

public PushResult sendPushWithCallback(String title, String content, Map
extrasMap, String... alias) {
ClientConfig clientConfig = ClientConfig.getInstance();
clientConfig.setTimeToLive(Long.valueOf(jpushConfig.getLiveTime()));
String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(jpushConfig.getAppkey(), jpushConfig.getMasterSecret()), null, clientConfig);
try {
URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
PushPayload payload = buildPushPayload(title, content, extrasMap, alias);
client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
@Override
public void onSucceed(ResponseWrapper responseWrapper) {
if (200 == responseWrapper.responseCode) {
LOG.info("极光推送成功");
} else {
LOG.info("极光推送失败,返回结果: %s", responseWrapper.responseContent);
}
}
});
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
client.close();
}
}

客户端自定义显示推送方式

public PushResult sendCustomPush(String title, String content, Map
extrasMap, String... alias) {
ClientConfig clientConfig = ClientConfig.getInstance();
clientConfig.setTimeToLive(Long.valueOf(jpushConfig.getLiveTime()));
JPushClient jpushClient = new JPushClient(jpushConfig.getMasterSecret(), jpushConfig.getAppkey(), null, clientConfig);
PushPayload payload = buildCustomPushPayload(title, content, extrasMap, alias);
try {
return jpushClient.sendPush(payload);
} catch (APIConnectionException e) {
LOG.error("极光推送连接错误,请稍后重试", e);
} catch (APIRequestException e) {
LOG.error("极光服务器响应出错,请修复!", e);
LOG.info("HTTP Status: %d", e.getStatus());
LOG.info("Error Code: %d", e.getErrorCode());
LOG.info("Error Message: %s", e.getErrorMessage());
}
}

使用方法

一键启动

通过简单配置即可快速集成极光推送服务。

推送示例

@Autowired
JpushService jpushService;
String title = "推送标题";
String content = "推送内容";
Map
extrasMap = new HashMap<>();
extrasMap.put("extraKey", "extraValue");
// 使用方式一:默认推送
jpushService.sendPush(title, content, extrasMap);
// 使用方式二:带返回值
jpushService.sendPushWithCallback(title, content, extrasMap);

应用场景

  • 系统消息推送
  • 行程通知
  • этим等其他需要及时通知的场景

注意事项

  • 确保极光官网申请的密钥有效
  • 推送内容保持在22字以内以便展示
  • 调用前确保依赖库已加入项目
  • 高并发场景时需考量推送背压

如有疑问,请到极光官网或社区求助。

转载地址:http://xmfqz.baihongyu.com/

你可能感兴趣的文章
netfilter应用场景
查看>>
netlink2.6.32内核实现源码
查看>>
Netpas:不一样的SD-WAN+ 保障网络通讯品质
查看>>
NetScaler的常用配置
查看>>
netsh advfirewall
查看>>
NETSH WINSOCK RESET这条命令的含义和作用?
查看>>
Netty WebSocket客户端
查看>>
netty 主要组件+黏包半包+rpc框架+源码透析
查看>>
Netty 异步任务调度与异步线程池
查看>>
Netty中集成Protobuf实现Java对象数据传递
查看>>
Netty事件注册机制深入解析
查看>>
Netty原理分析及实战(四)-客户端与服务端双向通信
查看>>
Netty客户端断线重连实现及问题思考
查看>>
Netty工作笔记0006---NIO的Buffer说明
查看>>
Netty工作笔记0007---NIO的三大核心组件关系
查看>>
Netty工作笔记0011---Channel应用案例2
查看>>
Netty工作笔记0013---Channel应用案例4Copy图片
查看>>
Netty工作笔记0014---Buffer类型化和只读
查看>>
Netty工作笔记0020---Selectionkey在NIO体系
查看>>
Vue踩坑笔记 - 关于vue静态资源引入的问题
查看>>