Personal Project

Thursday, June 16, 2016

Tutorial (2) : How to send Push Notifications to iPhone using Java ?



Generate aps_developer.p12 for JavaPNS

Download and Upzip openssl-0.9.8k_WIN32.zip
Goto windows dos command mode

// Convert aps_developer.cer to aps_developer.pem
openssl x509 -in aps_developer.cer -inform DER -out aps_developer.pem -outform PEM 

// Generate Push_Noenc.pem from PushChatCert.p12
openssl pkcs12 -nocerts -out Push_Noenc.pem -in PushChatCert.p12

// Output aps_developer.p12 for JavaPNS
openssl pkcs12 -export -in aps_developer.pem -inkey Push_Noenc.pem -certfile PushChat.certSigningRequest -name "aps_developer" -out aps_developer.p12


Obtain Device token from iOS

Add the following code in the method of AppDelegate.didFinishLaunchingWithOptions

IOS Code:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];  

To setup registerForRemoteNotificationTypes for app to receive notification message.

To extract device token by the following codes:

IOS Code: 

 - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {   
    NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
    NSLog(@"My token is:%@", token);
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {    
    NSString *error_str = [NSString stringWithFormat: @"%@", error];
    NSLog(@"Failed to get token, error:%@", error_str);

 
iOS device needs to implement two features: one is to register app as receiving notification type, the other is to send its token to backend JavaPNS via HTTP or TCP protocol.


Setup JavaPNS Sample code

Please add the following .jar library in your project:
1. bcprov-jdk15-146.jar
2. log4j-1.2.15.jar
3. JavaPNS_2.2.jar ( unzip JavaPNS_2.2_complete.zip )
4. Compile the source code : PushTest.java

(PS: Or simply import the testSample code by eclipse and run it)
  
JavaPNS needs certificate aps_developer.p12, and iOS token id to accomplish the function of sending Push Messages. The following codes below detail how to make it work.

public static void main(String[] args) {
                       
                        // iOS Private key as a p12 file exported by MAC
                        String keynotePath = "aps_developer.p12";
                       
                        // iOS developer certificate password exported by MAC
                        String password = "Your Pasword";
                       
                        // iOS device Token id
                        String token = "Your Device Token";
                       
                        // Test environment: Production or Sandbox(simulation)
                        String environment = "simulation";
                       
                        // Push message mode: Simple or complex or thread
    // Simple: Simple msg, complex: complex message, thread: Add multiple threads support
                        String pushMode = "complex";
                       
                        // Number of received devices
                        String deviceNum = "1";
                       
                        // Number of sender threads
                        String threadNum = "1";
                       
                        /* Initialize Log4j to print logs to console */
                        configureBasicLogging();

                        /* Push an alert */
                        try {
pushTest(keynotePath, password, token, environment, pushMode, deviceNum, threadNum);
                                   
                        } catch (CommunicationException e) {
                                    e.printStackTrace();
                        } catch (KeystoreException e) {
                                    e.printStackTrace();
                        }
            }




How to generate iOS development certificate for Push Server ?

// For Development Environment
// Convert aps_developer.cer to aps_developer.pem
openssl x509 -in aps_development.cer -inform DER -out aps_developer.pem -outform PEM

// Generate Push_Noenc.pem from PushChatCert.p12
openssl pkcs12 -nocerts -out Push_Noenc.pem -in PushChatCert.p12   // Input your password

// Output aps_developer.p12 for JavaPNS
openssl pkcs12 -export -in aps_developer.pem -inkey Push_Noenc.pem -certfile Push.certSigningRequest -name "aps_developer" -out aps_developer.p12


How to generate iOS Production certificate for Push Server?

// For Production
// Convert aps_developer.cer to aps_developer.pem
openssl x509 -in aps_production.cer -inform DER -out aps_production.pem -outform PEM

// Generate Push_Noenc.pem from PushChatCert.p12
openssl pkcs12 -nocerts -out Push_Noenc.pem -in PushChatCert.p12 

// Output aps_developer.p12 for JavaPNS
openssl pkcs12 -export -in aps_production.pem -inkey Push_Noenc.pem -certfile Push.certSigningRequest -name "aps_production" -out aps_production.p12


Next  - Tutorial(1) : How to send Push Notifications to Android using Java ?

No comments:

Post a Comment