APNSSender.java
JavaPNS_2.2.jar
import java.util.LinkedList;
import java.util.ListIterator;
import javapns.communication.ConnectionToAppleServer;
import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.feedback.AppleFeedbackServerBasicImpl;
import javapns.feedback.FeedbackServiceManager;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;
import javapns.notification.ResponsePacket;
import kr.pe.deverexpert.common.util.PropertiesUtil;
import org.apache.log4j.Logger;
public class APNSSender {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
APNSSender apns = new APNSSender();
String targetToken = "140581b37023153f40262612e7bd3216cc050b393b47d7e88700ee48f2ca055e";
String strMessage = "테스트 메세지 입니다.";
try {
apns.sender(targetToken, strMessage);
} catch ( Exception e ) {
e.printStackTrace();
}
}
protected final Logger logger = Logger.getLogger(getClass());
// 인증서
private final PropertiesUtil prou = new PropertiesUtil();
private final String certi = prou.getContextPath() + "kr/co/javalove/exemple/push/APNS.p12";
private final String certiPwd = "1234";
private final String gateway = "gateway.sandbox.push.apple.com";
private final int gatewayPort = 2195;
private final String feedback = "feedback.sandbox.push.apple.com";
private final int feedbackPort = 2196;
public void sender(String targetToken, String message) {
PushNotificationManager pushManager = null;
try {
PushNotificationPayload payLoad = new PushNotificationPayload();
payLoad.addAlert("새로운 메세지가 있습니다.");
payLoad.addCustomDictionary("acme", message);
pushManager = new PushNotificationManager();
BasicDevice device = new BasicDevice(targetToken);
device.setDeviceId("iPhone");
AppleNotificationServerBasicImpl appleNoti = new AppleNotificationServerBasicImpl(certi, certiPwd, ConnectionToAppleServer.KEYSTORE_TYPE_PKCS12, gateway, gatewayPort);
//Connect to APNs
pushManager.initializeConnection(appleNoti);
logger.debug("SEND : " + payLoad.getPayload().toString());
//Send Push
PushedNotification pushed = pushManager.sendNotification(device, payLoad);
ResponsePacket resultPacket = pushed.getResponse();
if (resultPacket != null) {
logger.debug("resultPacket.isErrorResponsePacket() [" + resultPacket.isErrorResponsePacket() + "]");
logger.debug("resultPacket.isValidErrorMessage() [" + resultPacket.isValidErrorMessage() + "]");
logger.debug("RESPONSE : " + resultPacket.getMessage() + " [" + resultPacket.getStatus() + "]");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pushManager != null) {
try {
pushManager.stopConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 피드백
*
* @return
* @throws Exception
*/
public ListIterator<device> feedback(int count) throws Exception {
// Get FeedbackServiceManager Instance
FeedbackServiceManager feedbackManager = new FeedbackServiceManager();
// Apple Feedback Server
AppleFeedbackServerBasicImpl appleFeedBackServer = new AppleFeedbackServerBasicImpl(certi, certiPwd, ConnectionToAppleServer.KEYSTORE_TYPE_PKCS12, feedback, feedbackPort);
// Initialize connection
LinkedList<device> devices = feedbackManager.getDevices(appleFeedBackServer);
ListIterator<device> itr = devices.listIterator();
return itr;
}
}