剑桥哥 2015-05-06 07:49 采纳率: 0%
浏览 2695

JAVA苹果推送报错,求大神搭救,HELP!~

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javapns.Push;
import javapns.communication.ProxyManager;
import javapns.devices.Device;
import javapns.notification.AppleNotificationServer;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotifications;
import javapns.notification.transmission.NotificationProgressListener;
import javapns.notification.transmission.NotificationThread;
import javapns.notification.transmission.NotificationThreads;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.internal.ApnsConnectionImpl;
import com.sgm.common.SystemServiceConfig;
import com.sgm.dao.AutoDeployBrandActivityDao;

public class PushMessageService {
private final static Log LOG = LogFactory.getLog(PushMessageService.class);
private final static String PROD_KEY_LOCATION = "XXXAPNs_Pro_Push.p12";
private final static String DEV_KEY_LOCATION = "XXXAPNs_Dev_Push.p12";
private static String PASSWORD;
private static boolean PRODUCTION = false;
private final static int NUMBER_OF_THREADS = 10;

private AutoDeployBrandActivityDao dao =new AutoDeployBrandActivityDao();

public static void setProxy(){
    try {
        SystemServiceConfig config = SystemServiceConfig.getInstance();
        if(!StringUtils.isEmpty(config.getProxyHost())){
            ProxyManager.setProxy(config.getProxyHost(), config.getProxyPort());
            ProxyManager.setJVMProxy(config.getProxyHost(), config.getProxyPort());
            LOG.info("Set Proxy - name:"+config.getProxyHost()+" host:"+config.getProxyPort());
        }
        PASSWORD = config.getPushKeyPassword();
        PRODUCTION = !config.isDebugMode();
    } catch (Exception e) {
        LOG.error(e.getMessage());
    }
}
public void pushAllNow(String message) throws Exception {
    setProxy();
    List<PayloadPerDevice> unreadCountPerDeviceList = new ArrayList<PayloadPerDevice>();

// Map unreadCountPerDeviceMap = dao.getUnreadCountPerIOS();
//Map unreadCountPerDeviceMap = new HashMap();
//unreadCountPerDeviceMap.put("d34305d3a9b008911c6b596d4fe3740ee9c096fdb2979b0f61aa0bb02e725bd6", 2l);
Map unreadCountPerDeviceMap = new HashMap();
// unreadCountPerDeviceMap.put("58c973d9 fe5aac9a 4a38a762 cf5ae0bf fbc5b3aa 849cc21e b3424d9c 564e0c8e", 1l);
unreadCountPerDeviceMap.put("58c973d9fe5aac9a4a38a762cf5ae0bffbc5b3aa849cc21eb3424d9c564e0c8e", 1l);
if(MapUtils.isNotEmpty(unreadCountPerDeviceMap)){
String pushMessageText = message;
Iterator iterator = unreadCountPerDeviceMap.keySet().iterator();
while(iterator.hasNext()){
String deviceCode = iterator.next();
Long count = unreadCountPerDeviceMap.get(deviceCode);
PushNotificationPayload payload = PushNotificationPayload.combined(pushMessageText,count.intValue(),"default");
unreadCountPerDeviceList.add(new PayloadPerDevice(payload,deviceCode));
LOG.info("PUSH TOKEN:"+deviceCode+" payload:"+ payload);
}
}
if(CollectionUtils.isNotEmpty(unreadCountPerDeviceList)){
// 1. clean unused devices from db
cleanDevices();
// 2. start to push messages
LOG.info("starting push all message...");
PushedNotifications pns = Push.payloads(PushMessageService.class.getClassLoader().getResourceAsStream(getKeyLocation()), PASSWORD, PRODUCTION, NUMBER_OF_THREADS, unreadCountPerDeviceList);
LOG.info("starting push all message completed!");
if(pns!=null){
LOG.info("Success response from Apple:" + pns.getSuccessfulNotifications());
LOG.info("Failed response from Apple:" + pns.getFailedNotifications());
}
}

}

public void cleanDevices() throws Exception {
    List<Device> invalidDevices = Push.feedback(PushMessageService.class.getClassLoader().getResourceAsStream(getKeyLocation()), PASSWORD, PRODUCTION);
    if(CollectionUtils.isNotEmpty(invalidDevices)){
        List<String> tokenList = new ArrayList<String>();
        Iterator<Device> iterator = invalidDevices.iterator();
        while(iterator.hasNext()){
            tokenList.add(iterator.next().getToken());
        }
        LOG.info("tokenList"+tokenList);
        //dao.deleteByCodes(tokenList);
    }
}

public static String getKeyLocation(){
    if(PRODUCTION){
        return PROD_KEY_LOCATION;
    }else{
        return DEV_KEY_LOCATION;
    }
}

public  static void main(String[] args){
    PushMessageService  service = new PushMessageService();
    try {
        service.pushAllNow("Bobo");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}


以下是报的异常:
javapns.communication.exceptions.CommunicationException: Communication exception: java.io.IOException: Unexpected EOF from proxy
at javapns.communication.ConnectionToAppleServer.getSSLSocket(ConnectionToAppleServer.java:158)
at javapns.feedback.FeedbackServiceManager.getDevices(FeedbackServiceManager.java:70)
at javapns.Push.feedback(Push.java:353)
at com.sgm.service.PushMessageService.cleanDevices(PushMessageService.java:93)
at com.sgm.service.PushMessageService.pushAllNow(PushMessageService.java:79)
at com.sgm.service.PushMessageService.main(PushMessageService.java:116)
Caused by: java.io.IOException: Unexpected EOF from proxy
at javapns.communication.ConnectionToAppleServer.doTunnelHandshake(ConnectionToAppleServer.java:215)
at javapns.communication.ConnectionToAppleServer.tunnelThroughProxy(ConnectionToAppleServer.java:171)
at javapns.communication.ConnectionToAppleServer.getSSLSocket(ConnectionToAppleServer.java:153)
... 5 more

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?