概述
pom:
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.8</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.7.3</version>
</dependency>
配置文件:
@Configuration
public class JPushConfig implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Bean
public JPushUtil jPushUtil() {
String masterSecret = environment.getProperty("jpush.AppSecret");
String appKey = environment.getProperty("jpush.AppKey");
Boolean production = Boolean.valueOf(environment.getProperty("jpush.isProduction"));
return new JPushUtil( masterSecret, appKey, production);
}
}
yml:
jpush: AppKey: f54*******************92 AppSecret: d6*****************055 isProduction: true
类型定义:
public enum JPushType { Notification("通知"), Customize("自定义消息"), Both("两者"); private String label; JPushType(String label) { this.label = label; } public String getLabel() { return label; } }
调用组件封装:
public class JPushUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(JPushUtil.class);
private Boolean ApnsProduction;
private JPushClient jPushClient;
public JPushUtil(String appSecret, String appKey, Boolean production) {
this.ApnsProduction = production;
jPushClient = new JPushClient(appSecret, appKey);
}
//
public static void main(String[] args) {
//
JPushUtil jPushUtil = new JPushUtil("2b3e77f579efcdb4fd430610",
//
"ac9dd6a81230a63e68abdf26",true);
//Notice
//
List<String> alias = new ArrayList<>();
//
alias.add("31e79470_598a_49ae_bae2_59cc355440f0");
//
//
Map<String, String> extrasParam = new HashMap();
//
extrasParam.put("companyId", "2");
//
for (int i = 0; i < 1; i++) {
//
jPushUtil.sendToAliasList(alias,"测试标题","测试内容",
//
new HashMap(){{put("companyId","2");}},JPushType.Notification);
//
}
//
}
/**
* 指定多个别名(Alias)进行多个平台进行推送 (推送给设备标识参数的用户)
*
* @param aliasList
别名或别名组
* @param msg_content
消息内容
* @param extrasParam
扩展字段(额外参数)
* @param notification_title 通知标题
* @return 0推送失败,1推送成功
*/
public int sendToAliasList(List<String> aliasList, String notification_title, String msg_content, Map<String, String> extrasParam, JPushType jPushType) {
if (BaseUtil.isEmpty(aliasList)) return 0;
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.all(), Audience.alias(aliasList), msg_content, extrasParam, notification_title, jPushType);
LOGGER.info("推送给设备标识参数的用户" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("推送结果" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 指定多个别名(Alias)进行多个平台进行推送 (推送给设备标识参数的用户)
*
* @param aliasList
别名或别名组
* @param msg_content
消息内容
* @param notification_title 通知标题
* @return 0推送失败,1推送成功
*/
public int sendToAliasList(List<String> aliasList, String notification_title, String msg_content, JPushType jPushType) {
if (BaseUtil.isEmpty(aliasList)) return 0;
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.all(), Audience.alias(aliasList), msg_content, new HashMap<String, String>(), notification_title, jPushType);
LOGGER.info("推送给设备标识参数的用户" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("推送结果" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 指定别名(Alias)进行多个平台进行推送 (推送给设备标识参数的用户)
*
* @param alias
别名
* @param msg_content
消息内容
* @param extrasParam
扩展参数
* @param notification_title 通知标题
* @return 0推送失败,1推送成功
*/
public int sendToAlias(String alias, String notification_title, String msg_content, Map<String, String> extrasParam, JPushType jPushType) {
System.err.println("------");
if (BaseUtil.isEmpty(alias)) return 0;
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.all(), Audience.alias(alias), msg_content, extrasParam, notification_title, jPushType);
LOGGER.info("推送给设备标识参数的用户" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("推送结果" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 指定别名(Alias)进行多个平台进行推送 (推送给设备标识参数的用户)
*
* @param alias
别名或别名组
* @param msg_content
消息内容
* @param notification_title 通知标题
* @return 0推送失败,1推送成功
*/
public int sendToAlias(String alias, String notification_title, String msg_content, JPushType jPushType) {
System.err.println(jPushClient);
if (BaseUtil.isEmpty(alias)) return 0;
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.all(), Audience.alias(alias), msg_content, new HashMap<String, String>(), notification_title, jPushType);
LOGGER.info("推送给设备标识参数的用户" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("推送结果" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 指定多个标签(Tag)进行多个平台进行推送
*
* @param tagsList
Tag或Tag组
* @param msg_content
消息内容
* @param extrasParams 扩展字段
* @return 0推送失败,1推送成功
*/
public int sendToTagList(List<String> tagsList, String notification_title, String msg_content, Map<String, String> extrasParams, JPushType jPushType) {
if (BaseUtil.isEmpty(tagsList)) return 0;
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.all(), Audience.tag(tagsList), msg_content, extrasParams, notification_title, jPushType);
LOGGER.info("" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 指定多个标签(Tag)进行多个平台进行推送
*
* @param tagsList
Tag或Tag组
* @param msg_content 消息内容
* @return 0推送失败,1推送成功
*/
public int sendToTagList(List<String> tagsList, String notification_title, String msg_content, JPushType jPushType) {
if (BaseUtil.isEmpty(tagsList)) return 0;
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.all(), Audience.tag(tagsList), msg_content, new HashMap<String, String>(), notification_title, jPushType);
LOGGER.info("" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 指定标签(Tag)进行多个平台进行推送
*
* @param tag
Tag或Tag组
* @param msg_content
消息内容
* @param extrasParams 扩展字段
* @return 0推送失败,1推送成功
*/
public int sendToTag(String tag, String notification_title, String msg_content, Map<String, String> extrasParams, JPushType jPushType) {
if (BaseUtil.isEmpty(tag)) return 0;
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.all(), Audience.tag(tag), msg_content, extrasParams, notification_title, jPushType);
LOGGER.info("" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 指定标签(Tag)进行多个平台进行推送
*
* @param tag
Tag或Tag组
* @param msg_content 消息内容
* @return 0推送失败,1推送成功
*/
public int sendToTag(String tag, String notification_title, String msg_content, JPushType jPushType) {
if (BaseUtil.isEmpty(tag)) return 0;
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.all(), Audience.tag(tag), msg_content, new HashMap<String, String>(), notification_title, jPushType);
LOGGER.info("" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有用户
*
* @param msg_content
消息内容
* @param extrasParam
扩展字段
* @param notification_title 通知标题
* @return 0推送失败,1推送成功
*/
public int sendToAll(String msg_content, String notification_title, Map<String, String> extrasParam, JPushType jPushType) {
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.android_ios(), Audience.all(), msg_content, extrasParam, notification_title, jPushType);
LOGGER.info("" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有用户
*
* @param msg_content
消息内容
* @param notification_title 通知标题
* @return 0推送失败,1推送成功
*/
public int sendToAll(String msg_content, String notification_title, JPushType jPushType) {
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.android_ios(), Audience.all(), msg_content, new HashMap<String, String>(), notification_title, jPushType);
LOGGER.info("" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有安卓用户
*
* @param notification_title 通知内容标题
* @param msg_content
消息内容
* @param extrasParam
扩展字段
* @return 0推送失败,1推送成功
*/
public int sendToAllAndroid(String notification_title, String msg_content, Map<String, String> extrasParam, JPushType jPushType) {
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.android(), Audience.all(), msg_content, extrasParam, notification_title, jPushType);
LOGGER.info("" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有IOS用户
*
* @param notification_title 通知内容标题
* @param msg_content
消息内容
* @param extrasParam
扩展字段
* @return 0推送失败,1推送成功
*/
public int sendToAllIos(String notification_title, String msg_content, Map<String, String> extrasParam, JPushType jPushType) {
int result = 0;
try {
PushPayload pushPayload = buildPushObject(Platform.ios(), Audience.all(), msg_content, extrasParam, notification_title, jPushType);
LOGGER.info("" + pushPayload);
PushResult pushResult = jPushClient.sendPush(pushPayload);
LOGGER.info("" + pushResult);
if (pushResult.getResponseCode() == 200) {
result = 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 推送消息
*
* @param audience
* @param msg_content
* @param extrasParam
* @param notification_title 通知标题
* @return
*/
private PushPayload buildPushObject(Platform platform, Audience audience, String msg_content, Map<String, String> extrasParam, String notification_title, JPushType jPushType) {
LOGGER.info("----------推送消息中......");
PushPayload.Builder builder = PushPayload.newBuilder().setPlatform(platform).setAudience(audience);
if (JPushType.Both.equals(jPushType) || JPushType.Notification.equals(jPushType)) {
builder = builder
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert(msg_content)
.setTitle(notification_title)
.addExtras(extrasParam)
.build())
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(IosAlert.newBuilder().
setTitleAndBody(notification_title, null, msg_content)
.build()
)
.incrBadge(1)
.setSound("default")
.addExtras(extrasParam)
.build())
.build());
}
if (JPushType.Both.equals(jPushType) || JPushType.Customize.equals(jPushType)) {
builder = builder.setMessage(Message.newBuilder()
.setMsgContent(msg_content)
.setTitle(notification_title)
.addExtras(extrasParam)
.build());
}
builder = builder.setOptions(Options.newBuilder()
.setApnsProduction(ApnsProduction)
.setSendno(1)
.setTimeToLive(86400)
.build());
return builder.build();
}
}
service调用:
@Resource private JPushUtil jPushUtil;
jPushUtil.sendToAliasList(aliasList, "title" , "content", extrasParam, JPushType.Notification);
最后
以上就是明理红牛为你收集整理的springBoot集成极光推送的全部内容,希望文章能够帮你解决springBoot集成极光推送所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复