Java发送ios推送消息(APN)的代码示例

Java发送ios推送消息(APN)的代码示例


实际项目应用中,应该考虑使用开源项目java-apns:https://github.com/notnoop/java-apns

千万不要用一个叫JAVAPNS的项目。这个开源项目的代码非常烂,每次发送消息都重新建立socket连接。

在apple的文档中,都明确的说了会把这种行为当作dos攻击行为。性能差就更不用说了。

ios手机上要安装对应的应用。该应用与.p12证书文件应该匹配。

apple官方的,关于APN服务,和apn的feedback的文档在这个地方:https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3

java版本发送apn推送代码示例:


package test;
 
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.security.KeyStore;
import java.util.regex.Pattern;
 
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
 
public class TestAPN {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
         
        String keyPath = "/data/tmp/proj.apns.p12";
        String ksType = "PKCS12";
        String ksPassword = "111111";
        String ksAlgorithm = "SunX509";
         
        String deviceToken = "1404c88dbb0adb92c0a85f4cd09be9707f251ae5bbecdb0a6a3e572aeb337d73";
         
        String serverHost = "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 = "this is a test.";
             
            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);
        }
    }
}


你可能感兴趣的:(Java发送ios推送消息(APN)的代码示例)