| | |
| | | @Resource(name = "myPushApi") |
| | | private PushApi myPushApi; |
| | | |
| | | private final String APP_PACKAGE_NAME = "com.hubei.jingchuReservoir"; |
| | | |
| | | /** |
| | | * 单点推送(离线不推送) |
| | | * |
| | |
| | | IosDTO iosDTO = new IosDTO(); |
| | | iosDTO.setAps(aps); |
| | | iosDTO.setType("notify"); |
| | | |
| | | |
| | | PushChannel pushChannel = new PushChannel(); |
| | | pushChannel.setIos(iosDTO); |
| | | //安卓离线厂商通道推送消息体 |
| | |
| | | System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 自定义消息批量推送,支持离线和在线推送 |
| | | * @param cids |
| | | * @param title |
| | | * @param content |
| | | */ |
| | | public Boolean myPushMessage(List<String> cids,String title,String content){ |
| | | List<PushDTO<Audience>> list = new ArrayList<>(cids.size()); |
| | | cids.forEach(cid -> { |
| | | PushDTO<Audience> pushDTO = new PushDTO<>(); |
| | | /**设置个推通道**/ |
| | | // 唯一标识 |
| | | pushDTO.setRequestId(UUID.randomUUID().toString()); |
| | | // 消息内容 |
| | | PushMessage pushMessage = new PushMessage(); |
| | | //0无限制、1仅wifi |
| | | pushMessage.setNetworkType(0); |
| | | |
| | | GTNotification gtNotification = new GTNotification(); |
| | | pushMessage.setNotification(gtNotification); |
| | | gtNotification.setTitle(title); |
| | | gtNotification.setBody(content); |
| | | gtNotification.setClickType("none"); |
| | | pushDTO.setPushMessage(pushMessage); |
| | | |
| | | // 消息接受人 |
| | | Audience audience = new Audience(); |
| | | audience.addCid(cid); |
| | | pushDTO.setAudience(audience); |
| | | /**设置个推通道结束**/ |
| | | |
| | | //配置推送条件 |
| | | // 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道; |
| | | // 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线; |
| | | // 3: 表示该消息只通过个推通道下发,不考虑用户是否在线; |
| | | // 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。 |
| | | Strategy strategy = new Strategy(); |
| | | strategy.setDef(1); |
| | | Settings settings = new Settings(); |
| | | settings.setStrategy(strategy); |
| | | pushDTO.setSettings(settings); |
| | | settings.setTtl(3600000);//消息有效期,走厂商消息需要设置该值 |
| | | /**设置厂商通道**/ |
| | | PushChannel pushChannel = new PushChannel(); |
| | | /**设置Android配置**/ |
| | | AndroidDTO androidDTO = new AndroidDTO(); |
| | | Ups ups = new Ups(); |
| | | ThirdNotification notification1 = new ThirdNotification(); |
| | | ups.setNotification(notification1); |
| | | notification1.setTitle(title);//安卓离线展示的标题 |
| | | notification1.setBody(content);//安卓离线展示的内容 |
| | | notification1.setClickType("intent"); |
| | | notification1.setIntent("intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;component=com.hubei.jingchuReservoir/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title="+title +";S.content="+content+";S.payload=test;end"); |
| | | |
| | | //对不同厂商进行配置 |
| | | // ups.addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry "); |
| | | ups.addOption("VV","/networkType",-1); |
| | | |
| | | androidDTO.setUps(ups); |
| | | pushChannel.setAndroid(androidDTO); |
| | | /**设置Android配置结束**/ |
| | | |
| | | /**设置IOS配置**/ |
| | | //推送苹果离线通知标题内容 |
| | | Alert alert = new Alert(); |
| | | alert.setTitle(title);//苹果离线通知栏标题 |
| | | alert.setBody(content);//苹果离线通知栏内容 |
| | | Aps aps = new Aps(); |
| | | //1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。 |
| | | //苹果建议1小时最多推送3条静默消息 |
| | | aps.setContentAvailable(0); |
| | | aps.setSound("default"); |
| | | aps.setAlert(alert); |
| | | IosDTO iosDTO = new IosDTO(); |
| | | iosDTO.setAps(aps); |
| | | iosDTO.setType("notify"); |
| | | pushChannel.setIos(iosDTO); |
| | | /**设置IOS配置结束**/ |
| | | pushDTO.setPushChannel(pushChannel); |
| | | /**设置厂商通道结束**/ |
| | | |
| | | |
| | | list.add(pushDTO); |
| | | }); |
| | | |
| | | PushBatchDTO pushBatchDTO = new PushBatchDTO(); |
| | | pushBatchDTO.setAsync(true); |
| | | pushBatchDTO.setMsgList(list); |
| | | |
| | | |
| | | //推送消息 |
| | | ApiResult<Map<String, Map<String, String>>> mapApiResult = myPushApi.pushBatchByCid(pushBatchDTO); |
| | | |
| | | return mapApiResult.isSuccess(); |
| | | } |
| | | } |