IOS消息推送探究socket

苹果开发者网站:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1


package messagetest;


import java.io.FileInputStream;
import java.io.OutputStream;
import java.security.KeyStore;


import javax.net.SocketFactory;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;


public class MessageUtilSingle {
public static String payLoadStr ="{\"aps\":{\"alert\":\"Message received from Bob\",\"sound\":\"chime.aiff\"}}";

public static void main (String[] args) {
try {
// String pushId = "f751e85890d36c7f9627c1852dcc026d78b2209ed2a530f1a8030b60c41574d8";
String pushId = "2212b2b46f83f9b83421b587ff2afc791cce5e9b4979af566ba23ac9fcdeb4e0";
pushMessage(pushId);
} catch (Exception e) {
e.printStackTrace();
}
}

public static byte[] generateTokenByte(String tokenID){

byte[] arrayOfByte = new byte[tokenID.length()/2];
 
tokenID = tokenID.toUpperCase();

int i = 0;

for (int j = 0; j < tokenID.length(); j += 2){
 String localObject = tokenID.substring(j, j + 2);
 int k = Integer.parseInt((String)localObject, 16);
 arrayOfByte[(i++)] = ((byte)k);
 }

return arrayOfByte;
}

private static byte[] generateData (String pushId){
byte command = (new Integer(0)).byteValue();
byte[] tokenLength = intToByte2(32);

byte[] deviceToken = generateTokenByte(pushId);
byte[] payload = payLoadStr.getBytes();
byte[] payloadLength = intToByte2(payload.length);

byte[] rs = new byte[37+payload.length];
rs[0] = command;
System.arraycopy(tokenLength, 0, rs, 1, 2);
System.arraycopy(deviceToken, 0, rs, 3, 32);
System.arraycopy(payloadLength, 0, rs, 35, 2);
System.arraycopy(payload, 0, rs, 37, payload.length);
return rs;
}

public static String pushMessage(String pushId) throws Exception{

String appleHost = "gateway.sandbox.push.apple.com";
int applePort = 2195;
String certificatePassword = "123456";
String certificatePath = "zn_server.p12";

SSLSocket sslSocket = null;
OutputStream out = null;
FileInputStream fileInputStream = null;

try{
byte[] bs = generateData(pushId);
// String fullPath = MessageUtil.class.getClassLoader().getResource(certificatePath).getPath();
fileInputStream = new FileInputStream("D:/store/"+certificatePath);

KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(fileInputStream, certificatePassword.toCharArray());

KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("Sunx509");
keyManagerFactory.init(keyStore, certificatePassword.toCharArray());

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);

SocketFactory socketFactory = sslContext.getSocketFactory();
sslSocket = ((SSLSocket) socketFactory.createSocket(appleHost,applePort));
out = sslSocket.getOutputStream();

out.write(bs);
out.flush();
out.flush();
return new String("success");
}catch(Exception e){
throw new Exception(e);
}finally{
if(fileInputStream != null){
fileInputStream.close();
}
if(out != null){
out.close();
}
if(sslSocket != null){
sslSocket.close();
}
}
}

public static byte[] intToByte4(int i){
byte[] targets = new byte[4];
targets[3] = (byte) (i & 0xFF);
targets[2] = (byte) (i >> 8 & 0xFF);
targets[1] = (byte) (i >> 16 & 0xFF);
targets[0] = (byte) (i >> 32 & 0xFF);
return targets;
}

public static byte[] intToByte2(int s){
byte[] targets = new byte[2];
targets[0] = (byte) (s >> 8 & 0xFF);
targets[1] = (byte) (s & 0xFF);
return targets;
}
}


--command=1的实现方式

int command = 1;
int tokenLength = 32;
byte[] deviceToken = generateTokenByte(pushId);
byte[] payload = payLoadStr.getBytes("utf-8");

ByteArrayOutputStream bas = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bas);

dos.writeByte(command);//command=1
dos.write("7890".getBytes());//Identifier
dos.write("zero".getBytes());//Expiry
dos.writeShort(tokenLength);//Token length
dos.write(deviceToken);//Device token
dos.writeShort(payload.length);//Payload length
dos.write(payload);//Payload
return bas.toByteArray();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值