Tuesday, July 21, 2009

About pushing for iPhone and Blackberry

I must say apple did it a neat and easier way, BB is not :(

BB
http://na.blackberry.com/eng/developers/javaappdev/pushapi.jsp#tab_tab_works

iPhone
A nice guide to setup your server:

http://www.macoscoders.com/2009/05/17/iphone-apple-push-notification-service-apns/
another good one:

http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/

basically you need a developer's id, then apply for the iPhone Push Notification Server (PNS) service. After got the certificate, you just put it on your Apache server. Or you may write a Java or .Net program to load the certificate and send out your testing data.

package com.demo.samples;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;

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

import java.awt.image.RenderedImage;
import java.io.IOException;

public class pushdemo {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(args[0]);
if (args.length != 2) {
System.out.println("Usage: KeGenerator phonenumber ExpireDate(yyyy-MM-dd)");
System.exit(0);
}

// TODO Auto-generated method stub
try {
int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";


char []passwKey = "keypassword".toCharArray();
KeyStore ts = KeyStore.getInstance("PKCS12");
ts.load(new FileInputStream("devCert.p12"), passwKey);

KeyManagerFactory tmf = KeyManagerFactory.getInstance("SunX509");
tmf.init(ts,passwKey);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(tmf.getKeyManagers(), null, null);
SSLSocketFactory factory =sslContext.getSocketFactory();
SSLSocket socket = (SSLSocket) factory.createSocket(hostname,port); // Create the ServerSocket
String[] suites = socket.getSupportedCipherSuites();
socket.setEnabledCipherSuites(suites);
//start handshake

socket.startHandshake();


// Create streams to securely send and receive data to the server
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();



// Read from in and write to out...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(0); //The command
System.out.println("First byte Current size: " + baos.size());

baos.write(0); //The first byte of the deviceId length
baos.write(32); //The deviceId length

System.out.println("Second byte Current size: " + baos.size());

String deviceId = "02da851db4f2b5bfce1982700d3dac72bc87cd60";
baos.write(hexStringToByteArray(deviceId.toUpperCase()));
System.out.println("Device ID: Current size: " + baos.size());

//{ "aps" : { "alert" : { "body" : "Bob wants to play poker", "action-loc-key" : "PLAY" }, "badge" : 5, }, "acme1" : "bar", "acme2" : [ "bang", "whiz" ] }
String payload = "{\"aps\":{\"alert\":\"I like spoons also\",\"badge\":14}}";
System.out.println("Sending payload: " + payload);
baos.write(0); //First byte of payload length;
baos.write(payload.length());
baos.write(payload.getBytes());

out.write(baos.toByteArray());
out.flush();

System.out.println("Closing socket.." + in.read());
// Close the socket
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

}
public static byte[] hexStringToByteArray(String s) {
byte[] b = new byte[s.length() / 2];
for (int i = 0; i < b.length; i++){
int index = i * 2;
int v = Integer.parseInt(s.substring(index, index + 2), 16);
b[i] = (byte)v;
}
return b;
}

}

No comments: