0% found this document useful (0 votes)
37 views2 pages

JPush Util

Uploaded by

876897608
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

JPush Util

Uploaded by

876897608
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public class JPushUtil {

private static final Logger log = LoggerFactory.getLogger(JPushUtil.class);

/**
* 极光 push- AppKey 测试
*/
private static String APP_KEY = "";
/**
* 极光 push- MasterSecret 测试
*/
private static String MASTER_SECRET = "";
/**
* 极光 push- APNS 开发环境(TRUE-生产环境,FALSE-开发环境)
*/
private static String APNS_PRODUCTION = "false";

// /** 极光 push- AppKey 生产 */


// private static String APP_KEY = "";
// /**极光 push- MasterSecret 生产*/
// private static String MASTER_SECRET = "";
// /** 极光 push- APNS 开发环境(TRUE-生产环境,FALSE-开发环境) */
// private static String APNS_PRODUCTION = "true";

/**
* 极光 push- 调用推送消息时,设置保留的时长(格式:60 * 60 * 24 one day)
*/
private static long TIME_TO_LIVE = 0L;

private static JPushClient jPushClient = null;

public static long sendCount = 0;

/**
* 初始化 JPushClient
*
* @return
*/
public static void createCustomClient() {
ClientConfig clientConfig = ClientConfig.getInstance();
clientConfig.setApnsProduction(Boolean.valueOf(APNS_PRODUCTION)); //
development env
clientConfig.setTimeToLive(TIME_TO_LIVE);
jPushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
}

public static boolean sendPushes(String title, String content, String pendId,


String[] alias) throws Exception {
if (jPushClient == null) {
createCustomClient();
}
final PushPayload payload = buildPushObject(title, content, pendId, alias);
Thread thread = new Thread() {
public void run() {
long start = System.currentTimeMillis();
try {
PushResult result = jPushClient.sendPush(payload);
log.info("JPush success and got result - " + result);
} catch (APIConnectionException e) {
log.error("JPush Connection error. Should retry later. ",
e.getMessage());
log.error("JPush Sendno: " + payload.getSendno());
} catch (APIRequestException e) {
log.info("JPush Error:HTTP Status: " + e.getStatus() + "Error
Code: " + e.getErrorCode() +
"Error Message: " + e.getErrorMessage() + "Msg ID: " +
e.getMsgId() +
"Sendno: " + payload.getSendno());
log.error(" JPush Error response from JPush server. Should
review and fix it. ", e);
e.printStackTrace();
}
}
};
thread.start();
return false;
}

public static PushPayload buildPushObject(String title, String content, String


pendId, String[] alias) throws Exception {
PushPayload.Builder pushBuilder = PushPayload.newBuilder();
//设置推送平台
pushBuilder.setPlatform(Platform.android_ios());
pushBuilder.setAudience(Audience.alias(alias));

//设置推送通知
HashMap<String, String> map = new HashMap<String, String>();
map.put("pendId", pendId);// 事件 id
pushBuilder.setNotification(Notification.newBuilder().setAlert(title)
.addPlatformNotification(AndroidNotification.newBuilder().setTitle(
content).addExtras(map).build())
.addPlatformNotification(IosNotification.newBuilder().addExtras(map
).incrBadge(1).autoBadge()
.setAlert(IosAlert.newBuilder().setTitleAndBody(title,
null, content).build()).build())
.build());

return pushBuilder.build();
}

public static void main(String[] args) {

try {
String[] alias = {"1"};
JPushUtil.sendPushes("测试推送", "内容是有人分派一项任务给你了,快去处理吧",
"422", alias);
} catch (Exception e) {
e.printStackTrace();
}

}
}

You might also like