<dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-http</artifactId> <version>4.1.2.1</version> </dependency> <repositories> <repository> <id>getui-nexus</id> <url>http://mvn.gt.getui.com/nexus/content/repositories/releases/</url> </repository> </repositories>
public class AppPush {//以下参数找前端拿 private static String appId = "xxxxxxxxxxx"; private static String appKey = "xxxxxxxxxxx"; private static String masterSecret = "xxxxxxxxxxxx"; // 如果需要使用HTTPS,直接修改url即可 //private static String url = "https://api.getui.com/apiex.htm"; private static String url = "http://api.getui.com/apiex.htm"; public static void AppPush(PushMessage pushMessage, List<String> appPushList) { IGtPush push = new IGtPush(url, appKey, masterSecret); // // Style0 style = new Style0(); // // STEP2:设置推送标题、推送内容 // style.setTitle(pushMessage.getTitle()); // style.setText(pushMessage.getContent()); // style.setLogo(pushMessage.getLogo()); //// style.setLogo("push.png"); // 设置推送图标 // // STEP3:设置响铃、震动等推送效果 // style.setRing(true); // 设置响铃 // style.setVibrate(true); // 设置震动 // // // STEP4:选择通知模板 // NotificationTemplate template = new NotificationTemplate(); // template.setAppId(appId); // template.setAppkey(appKey); // template.setStyle(style); // // 点击消息打开应用 // template.setTransmissionType(1); // // 传递自定义消息 // template.setTransmissionContent(JSONUtil.toJsonStr(pushMessage)); NotificationTemplate template = buildNotificationTemplate(pushMessage); // STEP5:定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数 // 采用toList方案,定义ListMessage消息类型 // List<String> appIds = new ArrayList<String>(); // appIds.add(appId); ListMessage message = new ListMessage(); message.setData(template); // message.setAppIdList(appIds); message.setOffline(true); // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发 message.setPushNetWorkType(0); message.setOfflineExpireTime(1000 * 600); // 时间单位为毫秒 String contentId = push.getContentId(message); // 获取推送目标 List targets = new ArrayList(); for (String ap : appPushList) { Target target = new Target(); target.setAppId(appId); target.setClientId(ap); targets.add(target); } // STEP6:执行推送,不采用toApp方案,采用toList方案 // IPushResult ret = push.pushMessageToApp(message); IPushResult ret = push.pushMessageToList(contentId, targets); System.out.println(ret.getResponse().toString()); } //通知栏透传模板 public static NotificationTemplate buildNotificationTemplate(PushMessage pushMessage) { NotificationTemplate template = new NotificationTemplate(); // 设置APPID与APPKEY template.setAppId(appId); template.setAppkey(appKey); Style0 style = new Style0(); // 设置通知栏标题与内容 style.setTitle(pushMessage.getTitle()); style.setText(pushMessage.getContent()); // 配置通知栏图标 style.setLogo(pushMessage.getLogo()); // 配置通知栏网络图标 style.setLogoUrl(""); // 设置通知是否响铃,震动,或者可清除 style.setRing(true); style.setVibrate(true); style.setClearable(true); template.setStyle(style); Notify notify = new Notify(); String intent = "intent:#Intent;action=android.intent.action.com.xiaokun.jzyz;launchFlags=0x14000000;component=/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={0};S.content={1};S.payload=test;end"; intent.replace("{0}", pushMessage.getTitle()); intent.replace("{1}", pushMessage.getContent()); notify.setTitle(pushMessage.getTitle()); notify.setContent(pushMessage.getContent()); notify.setIntent(intent); template.set3rdNotifyInfo(notify); // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动 template.setTransmissionType(2); String o = JSONObject.toJSON(pushMessage).toString(); System.out.println(o); template.setTransmissionContent(o); return template; } public static Map<String, Object> sendMessage(PushMessage pushMessage, List<String> appPushList) { try { // 使用透传模板 TransmissionTemplate template = new TransmissionTemplate(); // 1:收到通知直接激活app,2:客服端自行处理 template.setTransmissionType(2); // 透传内容 template.setTransmissionContent(pushMessage.getContent()); template.setAppId(appId); template.setAppkey(appKey); APNPayload payload = new APNPayload();//设置ios payload.setAutoBadge("+1"); payload.setContentAvailable(0); payload.setSound("default"); payload.setAlertMsg(new APNPayload.SimpleAlertMsg(pushMessage.getTitle())); //在已有数字基础上加1显示,设置为-1时,在已有数 template.setAPNInfo(payload); String intent = "intent:#Intent;action=android.intent.action.com.xiaokun.jzyz;launchFlags=0x14000000;component=/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={0};S.content={1};S.payload=test;end"; intent.replace("{0}", pushMessage.getTitle()); intent.replace("{1}", pushMessage.getContent()); Notify notify = new Notify(); // 通知栏显示标题 notify.setTitle(pushMessage.getTitle()); // 通知栏内容 notify.setContent(pushMessage.getContent()); notify.setIntent(intent); // 设置第三方通知 template.set3rdNotifyInfo(notify); ListMessage message = new ListMessage(); message.setData(template); // 设置消息离线 message.setOffline(true); // 离线消息有效时间为7天 message.setOfflineExpireTime(1000 * 3600 * 24 * 7); //添加要推送的终端 List<Target> targets = new ArrayList<Target>(); for (String ap : appPushList) { Target target = new Target(); target.setAppId(appId); target.setClientId(ap); targets.add(target); } //推送前通过该接口申请“ContentID” IGtPush push = new IGtPush(url, appKey, masterSecret); String contentId = push.getContentId(message); IPushResult ret = push.pushMessageToList(contentId, targets); System.out.println(ret); return ret.getResponse(); } catch (Exception e) { e.printStackTrace(); return null; } } }
@Data public class PushMessage { private String title="故障通知"; private String content; private String logo="push.png"; private String payload; }
标签:style,String,app,消息,template,new,pushMessage,推送,message From: https://www.cnblogs.com/gujiajie/p/16968304.html