package com.iamberry.app.api.util; import java.util.HashMap; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.jpush.api.JPushClient; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.Options; import cn.jpush.api.push.model.Platform; import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.push.model.PushPayload.Builder; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.notification.AndroidNotification; import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification; import com.iamberry.app.core.dto.JpushMessageDTO; import com.iamberry.app.core.entity.Message; public class Jdpush { protected static final Logger LOG = LoggerFactory.getLogger(Jdpush.class); public static final String JpushAppkey ="4aed8013f682a80294510d00"; public static final String JpushSecret ="b7228c86effa5468c04983f5"; public static final boolean APNOPTION = true; //True 表示推送生产环境,False 表示要推送开发环境 public static final String PREFIX = "iamberry_"; //别名,前缀 public static final HashMap Platforms = new HashMap(); //平台 public static final HashMap allResultCode = new HashMap(); //结果集 /* platform 必填 推送平台设置 audience 必填 推送设备指定 notification 可选 通知内容体。是被推送到客户端的内容。与 message 一起二者必须有其一,可以二者并存 message 可选 消息内容体。是被推送到客户端的内容。与 notification 一起二者必须有其一,可以二者并存 sms_message 可选 短信渠道补充送达内容体 options 可选 推送参数 */ static{ Platforms.put("all", Platform.all()); //all Platforms.put("android", Platform.android()); //android Platforms.put("ios", Platform.ios()); //ios allResultCode.put(200, "发送成功!");allResultCode.put(400, "错误的请求!"); allResultCode.put(401, "未验证");allResultCode.put(403, "被拒绝"); allResultCode.put(404, "无法找到");allResultCode.put(504, "代理超时"); } public static String SendPush(PushPayload payload) { JPushClient jpushClient = new JPushClient(JpushSecret,JpushAppkey); //推送客户端 //生成推送的内容, PushResult result = null; try { System.out.println(payload.toString()); result = jpushClient.sendPush(payload); LOG.info("Got result - " + result); LOG.info("Got result code " + result.getResponseCode()); String responseString = allResultCode.get(result.getResponseCode()); if(StringUtils.isNotBlank(responseString)){ return responseString; } } 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()); LOG.info("Msg ID: " + e.getMsgId()); } return String.valueOf(result.getResponseCode()); } /** *手动发送消息到-----> 某平台,某消息,某通知 * @param jpushmessage 参数类 * @return 消息封装类 */ public static String sendmessage_jpushmessage(JpushMessageDTO jpushmessage) { PushPayload pushpayload = PushPayload.newBuilder() .setPlatform(Platforms.get(jpushmessage.getPlatform())) .setAudience(Audience.alias(PREFIX+jpushmessage.getTag())) .setNotification(Notification.alert(jpushmessage.getContent()) )//通知消息是 content .setOptions(Options.newBuilder() .setApnsProduction(APNOPTION) //True 表示推送生产环境,False 表示要推送开发环境 .build()) .build(); return SendPush(pushpayload); } /** * 程序触发,手动触发 * 若别名存在-->则表示对某个人发送,不存在,则表示对所有人 * @return 调用结果 */ public static String buildPushObject_all_notifi(Message messsage) { Builder builder = PushPayload.newBuilder(); //所有平台 //备注中,all,ios,android,其他表示 all, if(Platforms.containsKey(messsage.getRemark())){ builder.setPlatform(Platforms.get(messsage.getRemark())); }else{ builder.setPlatform(Platform.all()); } if(StringUtils.isNoneEmpty(Long.toString(messsage.getUser()))){ builder.setAudience(Audience.alias(PREFIX+messsage.getUser())); //某个别名 } builder.setNotification(Notification.newBuilder() .setAlert(messsage.getContent()) //通知消息是alert content .addPlatformNotification(AndroidNotification.newBuilder() .setTitle(messsage.getTitle()) .addExtra("forword", messsage.getForword()) //是否在内部打开1内部2外部 .addExtra("send_type", messsage.getSend_type())//推送的类型1文本 2url 3 内部页面 .addExtra("url", messsage.getUrl())//消息URL .build()) .addPlatformNotification(IosNotification.newBuilder() .addExtra("forword", messsage.getForword()) //是否在内部打开1内部2外部 .addExtra("send_type", messsage.getSend_type())//推送的类型1文本 2url 3 内部页面 .addExtra("url", messsage.getUrl())//消息URL .build()) .build()); //设置是开发环境还是,生产环境 builder.setOptions(Options.newBuilder() .setApnsProduction(APNOPTION) .build()); return SendPush(builder.build()); } }