APNS(4)Recall the Process and Learn Java APNS

APNS(4)Recall the Process and Learn Java APNS
1. Process about the Certificate File
Generate the Certificate Signing Request(CSR)  ---  xxxx.certSigningRequest

Export the p12 private key                                ---  xxxx.p12
Upload my xxxx.certSigningRequest file to Apple and generate the certificate
                                                                      ---  xxxx.cer
Based on the certSigningRequest file and xxxx.p12 and  xxxx.cer I will generate the PEM file
>xxxx.cer -----> xxxx1.pem
>xxxx,p12 -----> xxxx2.pem

Make the PEM file together
xxxx1.pem + xxxx2.pem ----> xxxx.pem

That is the whole process.

2. Process to Deal directly with the p12
Open the keychain Access ---> Request a Certificate Information from Authority Certificate ---> Output: CertificateSigningRequest.certSigningRequest

Open the keychain Access ---> login ----> Keys ---> [Command Name] private key ----> Export to p12 file Output:
[Command Name].p12

Logon to Apple ----> identifiers -----> App IDs ----> Settings ----> Create Certificate ---- Upload my xxxxx.certSigningRequest file
aps_development.cer

That is right. Then based on p12 and CER, I will generate 2 PEM files. And make 2 PEM together into 1. That is done.

3. JavaPNS
Basic Push Notification
Configure the pom.xml to be
<dependency>      <groupId>com.google.code</groupId>      <artifactId>javapns</artifactId>     <version>2.2</version>

</dependency>

And the basic simple implementation is as follow:
package com.sillycat.easyrestserver.main;
import javapns.Push;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;

publicclass PushTestMain {
publicstaticvoid main(String[] args) {
     try {           Push.alert(                "Hello World!",                "[file directory and name].p12",                "password", false,                "APNS token from the device");      } catch (CommunicationException e) {           e.printStackTrace();      } catch (KeystoreException e) {           e.printStackTrace();      } } }


Error Message:
04-17 11:20:08 [ERROR] javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:426) - Attempt to send Notification failed and beyond the maximum number of attempts permitted
04-17 11:20:08 [ERROR] javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:429) - Delivery error
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)

at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)

Solution:
>openssl x509 -in aps_development.cer -inform DER -out developer_identity.pem -outform PEM
>openssl pkcs12 -nocerts -in EasyiOSSampleKey.p12 -out mykey.pem
>openssl pkcs12 -export -inkey mykey.pem -in developer_identity.pem -out EasyiOSSampleKey_final.p12

The means do not directly use the p12 from the private key, In my PHP example, I used PEM file based on the CER from apple and p12 from my private key.

Almost the same process to generate the P12, also based on the CER from apple and p12 from my private key.

In the class Push, there are methods alert, badge, sound, combined and etc.

Advanced Push Notification
PushNotificationPayload payload = PushNotificationPayload.complex();
playload.addBadge(int badge)
playload.addSound(String sound)
playload.addAlert(String message)
List<PushedNotification]]> notifications = Push.payload(payload, keystore, password, production, devices);

Send Multiple Message
private static void sendMultiple(){
PushNotificationPayload payload = PushNotificationPayload.complex(); try {      payload.addAlert(DEFAULT_MESSAGE);      payload.addSound("default");      payload.addBadge(1); } catch (JSONException e1) {      e1.printStackTrace(); } int threads = 2; List<Device> devices = new ArrayList<Device>(); try {      devices.add(new BasicDevice(DEFAULT_TOKEN)); } catch (InvalidDeviceTokenFormatException e1) {      e1.printStackTrace(); } try {      List<PushedNotification> notifications = Push.payload(payload,      P12_FILE, DEFAULT_PASSWORD, false, threads, devices);      System.out.println(notifications); } catch (Exception e) {      e.printStackTrace(); }

}

Creating a push queue
PushNotificationPayload payload = PushNotificationPayload.alert("Hello World!");
int threads = 30;
PushQueue queue = Push.queue(keystore, password, production, threads);
queue.start();

queue.add(payload, token);

private static void sendQueue() {
PushNotificationPayload payload = PushNotificationPayload.complex(); try {      payload.addAlert(DEFAULT_MESSAGE);      payload.addSound("default");      payload.addBadge(1); } catch (JSONException e1) {      e1.printStackTrace(); } int threads = 2; PushQueue queue = null; try {      queue = Push.queue(P12_FILE, DEFAULT_PASSWORD, false, threads);      queue.start();      queue.add(payload, DEFAULT_TOKEN); } catch (KeystoreException e) {      e.printStackTrace(); } catch (InvalidDeviceTokenFormatException e) {      e.printStackTrace(); } try {      Thread.sleep(10* 1000); } catch (InterruptedException e) {      e.printStackTrace(); }

}

References:
http://sillycat.iteye.com/blog/1769158
http://sillycat.iteye.com/blog/1769163
http://sillycat.iteye.com/blog/1769781

javaPNS
https://code.google.com/p/javapns/
https://code.google.com/p/javapns/wiki/PushNotificationBasic
https://code.google.com/p/javapns/wiki/PushNotificationAdvanced

http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2

http://stackoverflow.com/questions/12585858/cannot-send-push-notifications-using-javapns-javaapns-ssl-handshake-failure

猜你喜欢

转载自sillycat.iteye.com/blog/1849217