public static PushPayload bulidAndroidCustomMsg(String content,String... userids) {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.newBuilder()
.addAudienceTarget(AudienceTarget.alias(userids))
.build())
.setMessage(Message.newBuilder()
.setMsgContent(content)
.addExtra("from", "JPush")
.build())
.build();
}
public static void pushCustomMsg(String content,String... userids) {
ClientConfig clientConfig = ClientConfig.getInstance();
final JPushClient jpushClient = new JPushClient(MasterSecret, AppKey, null, clientConfig);
final PushPayload payload = bulidAndroidCustomMsg(content,userids);
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
LOG.error("Sendno: " + payload.getSendno());
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
LOG.error("Sendno: " + payload.getSendno());
}
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.newBuilder()
.addAudienceTarget(AudienceTarget.alias(userids))
.build())
.setMessage(Message.newBuilder()
.setMsgContent(content)
.addExtra("from", "JPush")
.build())
.build();
}
public static void pushCustomMsg(String content,String... userids) {
ClientConfig clientConfig = ClientConfig.getInstance();
final JPushClient jpushClient = new JPushClient(MasterSecret, AppKey, null, clientConfig);
final PushPayload payload = bulidAndroidCustomMsg(content,userids);
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
LOG.error("Sendno: " + payload.getSendno());
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
LOG.error("Sendno: " + payload.getSendno());
}
}
测试:
pushCustomMsg("/自定义内容/",推送的人员)
添加相关配置请查看 极光推送官网 https://docs.jiguang.cn/jpush/server/sdk/java_sdk/