public class AppPushUtil { private final static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(AppPushUtil.class); //采用"Java SDK 快速入门", "第二步 获取访问凭证 "中获得的应用配置,用户可以自行替换 private static String appId = ""; private static String appKey = ""; private static String masterSecret = ""; static String CID = ""; //客户端的clientId static String host = "http://sdk.open.api.igexin.com/apiex.htm"; /** * 为APP推送消息 * * @param clientId 客户端clientId * @param title 推送消息title * @param message 推送消息内容 * @param ,android或ios */ public static IPushResult pushMessageToApp(List<AppMessageClient> clientId, String title, String message) { IGtPush push = new IGtPush(host, appKey, masterSecret); TransmissionTemplate transmissionTemplate = null; NotificationTemplate notyPopLoadTemplate = null; IPushResult ret = null; if (clientId != null) { //由于苹果手机推送有问题所以所以用的是 单个推送方法 for (AppMessageClient app : clientId) { SingleMessage singleMessage = new SingleMessage(); if (app.getPhoneType().equalsIgnoreCase("IOS")) { transmissionTemplate = transmissionTemplateDemo(title, message, app.getPhoneType()); singleMessage.setData(transmissionTemplate); } else if (app.getPhoneType().equalsIgnoreCase("Android")) { notyPopLoadTemplate = notyPopLoadTemplateDemo(title, message); singleMessage.setData(notyPopLoadTemplate); } singleMessage.setOffline(true); // 离线有效时间,单位为毫秒,可选 singleMessage.setOfflineExpireTime(24 * 3600 * 1000); // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发 singleMessage.setPushNetWorkType(0); Target target = new Target(); target.setAppId(appId); target.setClientId(app.getClientId()); //target.setAlias(Alias); try { ret = push.pushMessageToSingle(singleMessage, target); } catch (RequestException e) { log.error("App推送消息错误信息:"+e.getMessage()); e.printStackTrace(); ret = push.pushMessageToSingle(singleMessage, target, e.getRequestId()); } } } return ret; } //ios public static TransmissionTemplate transmissionTemplateDemo(String title, String message, String clientType) { TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(appId); template.setAppkey(appKey); // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动 template.setTransmissionType(1); //如果客户端是ios的话,需要需要设置setAPNInfo参数 APNPayload payload = new APNPayload(); //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字 payload.setAutoBadge("1"); payload.setContentAvailable(0); payload.setSound("default"); payload.setCategory("$由客户端定义"); //推送消息模板 ios payload.setAlertMsg(getDictionaryAlertMsg(title,message)); template.setAPNInfo(payload); return template; } //Android public static NotificationTemplate notyPopLoadTemplateDemo(String title, String message) { NotificationTemplate template = new NotificationTemplate(); // 设置APPID与APPKEY template.setAppId(appId); template.setAppkey(appKey); Style0 style = new Style0(); // 设置通知栏标题与内容 style.setTitle(title); style.setText(message); // 配置通知栏图标 style.setLogo("icon.png"); // 配置通知栏网络图标 style.setLogoUrl(""); // 设置通知是否响铃,震动,或者可清除 style.setRing(true); style.setVibrate(true); style.setClearable(true); template.setStyle(style); // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动 template.setTransmissionType(1); return template; } private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title, String message) { APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg(); alertMsg.setBody(message); alertMsg.setActionLocKey("ActionLockey"); alertMsg.setLocKey("LocKey"); alertMsg.addLocArg("loc-args"); alertMsg.setLaunchImage("launch-image"); // iOS8.2以上版本支持 alertMsg.setTitle(title); alertMsg.setTitleLocKey("TitleLocKey"); alertMsg.addTitleLocArg("TitleLocArg"); return alertMsg; } }
个推消息推送
最新推荐文章于 2024-07-25 20:17:04 发布