import java.io.*;
import java.net.Socket;
import java.security.*;
import java.util.regex.Pattern;
import javax.net.ssl.*;
public class mainAPNS
{
public static void main(String[] args) {
String keyPath = "E:/etf_workspaces/etf/WebRoot/pushSource/aps_development_biz.p12";
String ksType = "PKCS12";
String ksPassword = "111111";
String ksAlgorithm = "SunX509";
//788701ef6c507b46bf09418757f63805a0f445c282e03d3054ecc61846d944f2
//cd2845988dba10064e11b0c38a91a2b8fff8e6604d58990c104f0b38ba7f894a
//String deviceToken = "788701ef6c507b46bf09418757f63805a0f445c282e03d3054ecc61846d944f2";
String deviceToken = "086947c12a637b18b3fe42deaee2297541dfd41a1b5922c71058df5af8d962f4";
String serverHost = "gateway.sandbox.push.apple.com"; //���Ի�����ַΪgateway.sandbox.push.apple.com
//�����ַΪgateway.push.apple.com
int serverPort = 2195;
try {
InputStream certInput = new FileInputStream(keyPath);
KeyStore keyStore = KeyStore.getInstance(ksType);
keyStore.load(certInput, ksPassword.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(ksAlgorithm);
kmf.init(keyStore, ksPassword.toCharArray());
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), null, null);
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
Socket socket = socketFactory.createSocket(serverHost, serverPort);
StringBuilder content = new StringBuilder();
String text = "您关注的房子岭南雅苑65号210室这周上涨了1.5%,现在就去看一下吧!";
content.append("{\"aps\":");
content.append("{\"alert\":\"").append(text)
.append("\",\"badge\":1,\"sound\":\"")
.append("ping1").append("\"}");
content.append(",\"cpn\":{\"t0\":")
.append(System.currentTimeMillis()).append("}");
content.append("}");
byte[] msgByte = makebyte((byte)1, deviceToken, content.toString(), 10000001);
System.out.println(msgByte);
socket.getOutputStream().write(msgByte);
socket.getOutputStream().flush();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ��װapns�涨���ֽ����� ʹ����ǿ��
*
* @param command
* @param deviceToken
* @param payload
* @return
* @throws IOException
*/
private static byte[] makebyte(byte command, String deviceToken, String payload, int identifer) {
byte[] deviceTokenb = decodeHex(deviceToken);
byte[] payloadBytes = null;
ByteArrayOutputStream boas = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(boas);
try {
payloadBytes = payload.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
try {
dos.writeByte(command);
dos.writeInt(identifer);//identifer
dos.writeInt(Integer.MAX_VALUE);
dos.writeShort(deviceTokenb.length);
dos.write(deviceTokenb);
dos.writeShort(payloadBytes.length);
dos.write(payloadBytes);
return boas.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private static final Pattern pattern = Pattern.compile("[ -]");
private static byte[] decodeHex(String deviceToken) {
String hex = pattern.matcher(deviceToken).replaceAll("");
byte[] bts = new byte[hex.length() / 2];
for (int i = 0; i < bts.length; i++) {
bts[i] = (byte) (charval(hex.charAt(2*i)) * 16 + charval(hex.charAt(2*i + 1)));
}
return bts;
}
private static int charval(char a) {
if ('0' <= a && a <= '9')
return (a - '0');
else if ('a' <= a && a <= 'f')
return (a - 'a') + 10;
else if ('A' <= a && a <= 'F')
return (a - 'A') + 10;
else{
throw new RuntimeException("Invalid hex character: " + a);
}
}
}