Jpush极光推送的一些心得

时间:2022-05-03 14:48:52

在集成极光推送的时候,test完全正常。部署到服务器后只发送一条推送之后推送不继续发送。经排查:

  • 2018-06-28 10:24:26.394 [ThreadPoolTaskExecutor-4] INFO c.j.c.connection.NettyHttpClient - - Created instance with connectionTimeout 5,000, readTimeout 30,000, maxRetryTimes 3, SSL Version TLS

因为JPUSH在3.12.15以后的版本需要显示的调用jpushClient.close() 来关闭NettyHttpClient 

//消息gradle推送
compile 'cn.jpush.api:jpush-client:3.2.17'
/**
* 自定义按别名推送信息
* @param days
* @param extras
* @param alias
* @return
*/
public static PushPayload sendAndroidAndAlias(String days,Map<String, String> extras, String... alias) {
Builder builder = PushPayload.newBuilder();
return builder
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(alias))
.setMessage(Message.newBuilder()
.setTitle(extras.get("title"))
.setMsgContent(extras.get("msg"))
.addExtras(extras)
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)
.setTimeToLive(Long.valueOf(Integer.valueOf(days)*86400))
.build())
.setNotification(Notification.android(extras.get("msg"), extras.get("title"), extras))//后端推送。如果前端推送可以去掉
.build();
}
 
 
public static PushResult sendPush(PushPayload payload) throws APIConnectionException, APIRequestException{
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);//定义的Jpushkey等
System.out.print("jPushClient 开始调用!!!!!!!!!!!!!"+payload.toString());
PushResult pushResult = jPushClient.sendPush(payload);
jPushClient.close();
System.out.print("jPushClient 调用完毕并且关闭NativeHttpClient");
return pushResult;
}
  //推送别名推送
@Test
public void sendAndroidAndAlias() {
        Map<String,String> parms = new HashMap<>();
parms.put("id","test");
parms.put("type","test");
parms.put("msg","test");
parms.put("title","test");
    //jPush用户推送
    String[] toId = new String[1];
    toId[0] = userId.toString();
        try {
PushPayload data = MsgPushUtils.
sendAndroidAndAlias("1",parms,userIDs);
//                MsgPushUtils.sendPushTryCatch(data,log);
MsgPushUtils.sendPush(data);
} catch (APIConnectionException e) {
log.error("Connection error. Should retry later. ", e);
} 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());
}