昨天搞了许久的苹果的推送服务,看着文档不是很难。可是很少使用socket发送拼接字节一类的数据。后来找到了javapns,很方便。直接就推送成功了。
本来是想用netty做的,但是没有成功,我觉得是bytes的发送格式不对,特别是字节数。
主页:
http://code.google.com/p/javapns/
使用的前提:
http://code.google.com/p/javapns/wiki/GeneralRequirements
推送的简单例子:
http://code.google.com/p/javapns/wiki/PushNotificationBasic
主页的wiki里面有不少例子和文档。使用推送的前提是要生成证书和要推送的设备令牌。
设备令牌是客户端程序第一次安装时生成的,apple的文档中有很详细的图,并提交给自己的push server。
apple的文档有c代码的例子,下面是一段从javapns截取的推送代码。
KeyStore ts = KeyStore.getInstance("PKCS12");
ts.load(new FileInputStream("d:/spncert.p12"), "123456".toCharArray());
KeyManagerFactory tmf = KeyManagerFactory.getInstance("SunX509");
tmf.init(ts, "123456".toCharArray());
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(tmf.getKeyManagers(), null, null);
SSLSocketFactory factory = sslContext.getSocketFactory();
SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
String[] suites = socket.getSupportedCipherSuites();
socket.setEnabledCipherSuites(suites);
// start handshake
socket.startHandshake();
char[] t = token.toCharArray();
byte[] b = Arrays.toString(t).getBytes();
OutputStream outputstream = socket.getOutputStream();
byte[] deviceTokenAsBytes = new byte[token.length() / 2];
token = token.toUpperCase();
int j = 0;
try {
for (int i = 0; i < token.length(); i += 2) {
String tt = token.substring(i, i + 2);
int tmp = Integer.parseInt(tt, 16);
deviceTokenAsBytes[j++] = (byte) tmp;
}
} catch (NumberFormatException e1) {
throw new InvalidDeviceTokenFormatException(token, e1.getMessage());
}
// Create the ByteArrayOutputStream which will contain the raw interface
byte[] payloadAsBytes = payload.getBytes();
int size = (Byte.SIZE / Byte.SIZE) + (Character.SIZE / Byte.SIZE)
+ deviceTokenAsBytes.length + (Character.SIZE / Byte.SIZE)
+ payloadAsBytes.length;
ByteArrayOutputStream bao = new ByteArrayOutputStream(size);
// Write command to ByteArrayOutputStream
// 0 = simple
// 1 = enhanced
byte zeron = 0;
bao.write(zeron);
// Write the TokenLength as a 16bits unsigned int, in big endian
int tl = deviceTokenAsBytes.length;
bao.write(intTo2ByteArray(tl));
// Write the Token in bytes
bao.write(deviceTokenAsBytes);
// Write the PayloadLength as a 16bits unsigned int, in big endian
int pl = payloadAsBytes.length;
bao.write(intTo2ByteArray(pl));
// Finally write the Payload
bao.write(payloadAsBytes);
bao.flush();
outputstream.write(bao.toByteArray());
outputstream.flush();
outputstream.close();
}
static final byte[] intTo2ByteArray(int value) {
int s1 = (value & 0xFF00) >> 8;
int s2 = value & 0xFF;
return new byte[] { (byte) s1, (byte) s2 };
}
上面代码中的:
byte[] deviceTokenAsBytes = new byte[token.length() / 2];很重要。我看到这恍然大悟。。。。哎
分享到:
相关推荐
php iospush 脚本 推送 简单的PHP推送脚本 几行代码解决你的推送困难的问题。
完整的介绍了如何ios push notification的使用过程。包括工程的编写,和server的端的介绍。希望对大家会有帮助。
push notificaiton 苹果的消息推送功能
基于苹果服务器的push通知发送代码,里边有jar和maven的环境安装方式。
java ios推送 jar:javapns-jdk16-163.jar,bcprov-jdk16-145.jar
IOSPush_PHP.zipI
php代码类,用于测试iospush通知的,欢迎大家下载使用,下载后不会使用,请查看我博客 php代码类,测试push通知的,运行直接php+文件路径
从某个地方下载的,自己也加了一些东西,此推送服务器比较简单。
ios 推送 java 服务端程序 ,与苹果的Apns服务打交道,作为ios客户端程序的服务端(provider)。
ios百度推送的客户端,分享给在路上的朋友!
Java向IOS设备push消息 验证ios推送证书是否正确。
模拟iOS发送通知,验证push证书有效性的工具,iOS开发必备
当前控制器导航栏隐藏, 下个控制器导航栏出现, 解决中间push闪现的问题
PHP代码发送IOS推送消息 PUSH IOS NOTIFICATION
ios平台推送信息源码 后台server端代码
Provider是指某个iPhone软件的Push服务器。 APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,下文统一使用该缩写。 因 此,整个过程可以分为三个阶段,下面用大家常用的聊天客户端BeejiveIM来...
通过在屏幕左滑push新的控制器,当滑动结束后,自动判断是否该显示新控制器 https://github.com/xiaopin/InteractivePushGesture.git
tabBar 中间突出 push 方式 实现
IOS后台消息PUSH,VS2008实现的,包括OPENSSL配置
react-native-code-push集成之ios篇是react-native IOS端集成codepush热更新的过程,内容丰富,过程详细,非常适合新手