123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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;
- public static final String PREFIX = "iamberry_";
-
- public static final HashMap<String, Platform> Platforms = new HashMap<String, Platform>();
- public static final HashMap<Integer, String> allResultCode = new HashMap<Integer, String>();
-
- static{
- Platforms.put("all", Platform.all());
- Platforms.put("android", Platform.android());
- Platforms.put("ios", Platform.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());
- }
-
-
- 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()) )
- .setOptions(Options.newBuilder()
- .setApnsProduction(APNOPTION)
- .build())
- .build();
- return SendPush(pushpayload);
- }
-
-
- public static String buildPushObject_all_notifi(Message messsage) {
- Builder builder = PushPayload.newBuilder();
-
-
- 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())
- .addPlatformNotification(AndroidNotification.newBuilder()
- .setTitle(messsage.getTitle())
- .addExtra("forword", messsage.getForword())
- .addExtra("send_type", messsage.getSend_type())
- .addExtra("url", messsage.getUrl())
- .build())
- .addPlatformNotification(IosNotification.newBuilder()
- .addExtra("forword", messsage.getForword())
- .addExtra("send_type", messsage.getSend_type())
- .addExtra("url", messsage.getUrl())
- .build())
- .build());
-
- builder.setOptions(Options.newBuilder()
- .setApnsProduction(APNOPTION)
- .build());
- return SendPush(builder.build());
- }
- }
|