package com.sinata.common.api.jPush;
|
|
import cn.jiguang.common.resp.APIConnectionException;
|
import cn.jiguang.common.resp.APIRequestException;
|
import cn.jpush.api.JPushClient;
|
import cn.jpush.api.push.PushResult;
|
import cn.jpush.api.push.model.PushPayload;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* <p>
|
* 极光推送
|
* </p>
|
* @author sl
|
* @since 2020/10/6 11:14
|
*/
|
public class JpushUtils {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(JpushUtils.class);
|
|
private static String APP_KEY_USER = "";
|
private static String MASTER_SECRET_USER = "";
|
|
public static final String PREFIX = "";
|
|
private static JPushClient jPushClientUser = new JPushClient(MASTER_SECRET_USER,APP_KEY_USER);
|
|
/**
|
* 推送给设备标识参数的用户
|
* @param aliasList 别名或别名组
|
* @param notification_title 通知内容标题
|
* @param msg_title 消息内容标题
|
* @param msg_content 消息内容
|
* @param extrasparam 扩展字段
|
* @return 0推送失败,1推送成功
|
*/
|
public static int sendToAliasListUser(List<String> aliasList, String notification_title, String msg_title, String msg_content, String extrasparam) {
|
int result = 0;
|
try {
|
PushPayload pushPayload= PushPayloadUtils.buildPushObject_all_aliasList_alertWithTitle(aliasList,notification_title,msg_title,msg_content,extrasparam);
|
LOGGER.info("推送给设备标识参数的用户"+pushPayload);
|
PushResult pushResult=jPushClientUser.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 notification_title 通知内容标题
|
* @param msg_title 消息内容标题
|
* @param msg_content 消息内容
|
* @param extrasparam 扩展字段
|
* @return 0推送失败,1推送成功
|
*/
|
public static int sendToTagListUser(List<String> tagsList,String notification_title, String msg_title, String msg_content, String extrasparam) {
|
int result = 0;
|
try {
|
PushPayload pushPayload= PushPayloadUtils.buildPushObject_all_tagList_alertWithTitle(tagsList,notification_title,msg_title,msg_content,extrasparam);
|
LOGGER.info(""+pushPayload);
|
PushResult pushResult=jPushClientUser.sendPush(pushPayload);
|
LOGGER.info(""+pushResult);
|
if(pushResult.getResponseCode()==200){
|
result=1;
|
}
|
} catch (APIConnectionException e) {
|
e.printStackTrace();
|
} catch (APIRequestException e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
/**
|
* 发送给所有安卓用户
|
* @param notification_title 通知内容标题
|
* @param msg_title 消息内容标题
|
* @param msg_content 消息内容
|
* @param extrasparam 扩展字段
|
* @return 0推送失败,1推送成功
|
*/
|
public static int sendToAllAndroidUser( String notification_title, String msg_title, String msg_content, String extrasparam) {
|
int result = 0;
|
try {
|
PushPayload pushPayload= PushPayloadUtils.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
|
LOGGER.info(""+pushPayload);
|
PushResult pushResult=jPushClientUser.sendPush(pushPayload);
|
LOGGER.info(""+pushResult);
|
if(pushResult.getResponseCode()==200){
|
result=1;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
/**
|
* 发送给所有IOS用户
|
* @param notification_title 通知内容标题
|
* @param msg_title 消息内容标题
|
* @param msg_content 消息内容
|
* @param extrasparam 扩展字段
|
* @return 0推送失败,1推送成功
|
*/
|
public static int sendToAllIosUser(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
int result = 0;
|
try {
|
PushPayload pushPayload= PushPayloadUtils.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
|
LOGGER.info(""+pushPayload);
|
PushResult pushResult=jPushClientUser.sendPush(pushPayload);
|
LOGGER.info(""+pushResult);
|
if(pushResult.getResponseCode()==200){
|
result=1;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
/**
|
* 发送给所有用户
|
* @param notification_title 通知内容标题
|
* @param msg_title 消息内容标题
|
* @param msg_content 消息内容
|
* @param extrasparam 扩展字段
|
* @return 0推送失败,1推送成功
|
*/
|
public static int sendToAllUser(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
int result = 0;
|
try {
|
PushPayload pushPayload= PushPayloadUtils.buildPushObject_android_and_ios(notification_title,msg_title,msg_content,extrasparam);
|
LOGGER.info(""+pushPayload);
|
PushResult pushResult=jPushClientUser.sendPush(pushPayload);
|
LOGGER.info(""+pushResult);
|
if(pushResult.getResponseCode()==200){
|
result=1;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
public static void main(String[] args) {
|
List<String> ids = new ArrayList<>();
|
ids.add(PREFIX + "26113");
|
JpushUtils.sendToAliasListUser(ids, "内容","1","1","3");
|
}
|
|
}
|