项目用到推送,研究了一下极光的api
ps:推送到达率普遍是一个难题,不能作为唯一判断条件,极光推送到达率在50%左右.如果必须推送的到,可自己研究erlang搭建服务器
下面上代码:
private static final Logger logger = LoggerFactory.getLogger(Jdpush.class);
// demo App defined in resources/jpush-api.conf
public static final String APPKEY = "注册极光帐号可得";
public static final String MASTERSECRET = "添加应用可得";
public static JPushClient jpushClient = null;
public static void sendPush(String msg) {
jpushClient = new JPushClient(MASTERSECRET, APPKEY, 3);
PushPayload payload = buildPushObjectByMessage(msg);
try {
PushResult result = jpushClient.sendPush(payload);
} catch (Exception e) {
e.printStackTrace();
}
}
public static PushPayload buildPushObjectByMessage(String msg) {
return PushPayload.newBuilder().setPlatform(Platform.all())// 设置接受的平台
.setAudience(Audience.all())// Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
.setMessage(Message.newBuilder()
.setMsgContent(msg)
.build())
.build();
}
/**
* 根据别名发送
*/
public static void sendByAlias(String content, Map<String, String> params, String userId) {
ClientConfig clientConfig = ClientConfig.getInstance();
JPushClient jpushClient = new JPushClient(MASTERSECRET, APPKEY, null, clientConfig);
List<String> users = new ArrayList<>();
users.add(userId);
PushPayload payload = buildPushObjectByAlias(content, params, users);
try {
PushResult result = jpushClient.sendPush(payload);
logger.info("Got result - " + result);
} catch (APIConnectionException e) {
logger.error("连接错误. 请过会儿重试. ", e);
} catch (APIRequestException e) {
logger.error("Error response from JPush server. Should review and fix it. ", e);
logger.info("HTTP Status: " + e.getStatus());
logger.info("Error Code: " + e.getErrorCode());
logger.info("Error Message: " + e.getErrorMessage());
logger.info("Msg ID: " + e.getMsgId());
}
}
private static PushPayload buildPushObjectByAlias(String content, Map<String, String> params, List<String> userIds) {
PushPayload.Builder builder = PushPayload.newBuilder();
builder.setPlatform(Platform.all());
builder.setAudience(Audience.alias(userIds));
Notification.Builder notificationBuilder = Notification.newBuilder();
notificationBuilder.setAlert(content);
notificationBuilder.addPlatformNotification(IosNotification.newBuilder().setAlert(content).setSound("happy").addExtras(params).build());
notificationBuilder.addPlatformNotification(AndroidNotification.newBuilder().setTitle("系统推送").setAlert(content).addExtras(params).build());
builder.setNotification(notificationBuilder.build());
return builder.build();
}
/**
* 根据别名集合发送
*/
public static void sendByAliasList(String content, Map<String, String> params,List<String> users) {
ClientConfig clientConfig = ClientConfig.getInstance();
JPushClient jpushClient = new JPushClient(MASTERSECRET, APPKEY, null, clientConfig);
PushPayload payload = buildPushObjectByAlias(content, params, users);
try {
PushResult result = jpushClient.sendPush(payload);
logger.info("Got result - " + result);
} catch (APIConnectionException e) {
logger.error("连接错误. 请过会儿重试. ", e);
} catch (APIRequestException e) {
logger.error("Error response from JPush server. Should review and fix it. ", e);
logger.info("HTTP Status: " + e.getStatus());
logger.info("Error Code: " + e.getErrorCode());
logger.info("Error Message: " + e.getErrorMessage());
logger.info("Msg ID: " + e.getMsgId());
}
}
public static void main(String[] args) {
Jdpush j = new Jdpush();
Map<String, String> params = new HashMap<String, String>();
//map里面可以放一些你需要展示的或者你需要传递的数据
params.put("type", "1");
j.sendByAlias("66666", params, "4");
}