Export to GitHub

javapns - How2UseJavapns.wiki



*IMPORTANT*: This page is *deprecated* and therefore contains outdated and/or invalid information. Please go back to the [http://code.google.com/p/javapns/ Project Home] to get access to the latest version and information.



Get your certificate

If you wanna use javapns on a PC, the first thing you have to do is exporting your certificate and you private key as a p12 file, using http://en.wikipedia.org/wiki/Keychain_%28Mac_OS%29'>KeyChain on a MAC.

To do this, just select both certificate and private key (associated to the application you wish to use to send notifications) in http://en.wikipedia.org/wiki/Keychain_%28Mac_OS%29'>KeyChain, and the right click on one item, and select "Export 2 elements", give a name (for example : myCertificate.p12) and password (for example : p@ssw0rd) and then export as p12.

Once you did this, you can now copy this p12 file on your PC (for example C:/temp).


Working with javapns

Send a simple notification

Download javapns, and then add it to your java project classpath. To send notifications, you just need this :


PayLoad simplePayLoad = new PayLoad();
simplePayLoad.addAlert("My alert message");
simplePayLoad.addBadge(45);
simplePayLoad.addSound("default");
Device client = PushNotificationManager.getInstance().getDevice("my_iPhone");
PushNotificationManager.getInstance().initializeConnection("gateway.sandbox.push.apple.com", 2195, "C:/temp/myCertificate.p12", "p@ssw0rd", SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
PushNotificationManager.getInstance().sendNotification(client, simplePayLoad);

The created payload looks like


{"aps":{"sound":"default","alert":"My alert message","badge":45}}

And thats it, enjoy !

Send a complex notification


// Or create a complex PayLoad with a custom alert
PayLoad complexPayLoad = new PayLoad();
PayLoadCustomAlert customAlert = new PayLoadCustomAlert();
// You can use addBody to add simple message, but we'll use
// a more complex alert message so let's comment it
// customAlert.addBody("My alert message");
customAlert.addActionLocKey("Open App");
customAlert.addLocKey("javapns rocks %@ %@%@");
ArrayList parameters = new ArrayList();
parameters.add("Test1");
parameters.add("Test");
parameters.add(2);
customAlert.addLocArgs(parameters);
complexPayLoad.addCustomAlert(customAlert);
complexPayLoad.addBadge(45);
complexPayLoad.addSound("default");
complexPayLoad.addCustomDictionary("acme", "foo");
complexPayLoad.addCustomDictionary("acme2", 42);
ArrayList values = new ArrayList();
values.add("value1");
values.add(2);
complexPayLoad.addCustomDictionary("acme3", values);
Device client = PushNotificationManager.getInstance().getDevice("my_iPhone");
PushNotificationManager.getInstance().initializeConnection("gateway.sandbox.push.apple.com", 2195, "C:/temp/myCertificate.p12", "p@ssw0rd", SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
PushNotificationManager.getInstance().sendNotification(client, complexPayLoad);

The resulted payload will look like


{"aps":{"sound":"default","alert":{"loc-args":["Test1","Test",2],"action-loc-key":"Open App","loc-key":"javapns rocks %@ %@%@"},"badge":45},"acme3":["value1",2],"acme2":42,"acme":"foo"}

http://img38.imageshack.us/img38/9242/javapns.jpg' />