title: Problem sending Apple Push Notification using Java and REST
body: <p>This is a follow up to my previous <a href="https://stackoverflow.com/questions/1355837/help-trying-to-use-apple-push-notification-service-through-java">posting</a> on StackOverflow.</p>
  
  <p>Figured its better to start a new post (since I made more progress than before) rather than appending a new question on a previous thread.</p>
  
  <p>Am using the Javapns library on Google Code to send an Apple Push Notification through a REST based web service...</p>
  
  <p>Here are the steps that I have completed:</p>
  
  <p><strong>iPhone Developer Program Portal (IDPP)</strong>:</p>
  
  <p>(1) Created the App ID and APNS based SSL Certificate and Keys.</p>
  
  <p>(2) Created and installed the provisioning profile.</p>
  
  <p>(3) Installed the SSL Certificate and Key on the server.</p>
  
  <p>(4) Set up my iPhone app to register for remote notifications.</p>
  
  <p><strong>XCode</strong>:</p>
  
  <p>Was able to obtain my device token when I built and deployed my app onto my device.</p>
  
  <p>As soon as my iPhone app deployed, the dialog came up on my iPhone indicating that my app would like to send push notifications and also asked for permission to allow them.</p>
  
  <p>When I invoked my web service, through my Log4J statements, I was able to see that my REST based web service was indeed invoked but I never received a push notification on my iPhone app!</p>
  
  <p><strong>ApnsManager class</strong>:</p>
  
  <pre><code>public class ApnsManager {
  
      /** APNs Server Host **/
      private static final String HOST = "gateway.sandbox.push.apple.com";
  
      /** APNs Port */
      private static final int PORT = 2195;
  
      public void sendNotification(String deviceToken) 
      throws Exception {
         try {
             PayLoad payLoad = new PayLoad();
             payLoad.addAlert("My alert message");
             payLoad.addBadge(45);
             payLoad.addSound("default");
  
             PushNotificationManager pushManager = 
                PushNotificationManager.getInstance();
  
             pushManager.addDevice("iPhone", deviceToken);
  
             log.warn("Initializing connectiong with APNS...");
  
             // Connect to APNs
             pushManager.initializeConnection(HOST, PORT,
             "/etc/Certificates.p12", "password", 
             SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
  
             Device client = pushManager.getDevice("iPhone");
  
             // Send Push
             log.warn("Sending push notification...");
             pushManager.sendNotification(client, payLoad);
             pushManager.stopConnection();
         } 
         catch (Exception e) {
             e.printStackTrace("Unable to send push ");
         }   
      }
  }
  </code></pre>
  
  <p><strong>RESTful Web Service</strong>:</p>
  
  <pre><code> @Path(ApnService.URL)
   @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
   public class ApnService {
      public static final String URL = "/apns";
  
      @GET
      @Path("send")
      @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
      public String send() throws JSONException, IOException {
          String msg = "";
  
          try {
              log.debug("Inside ApnService.send() method.");
              log.debug("Sending notification to device");
              ApnManager.sendNotification("32b3bf28520b977ab8eec50b482
              25e14d07cd78 adb69949379609e40401d2d1de00000000738518e5c
              000000003850978c38509778000000000000000000398fe12800398f
              e2e0398fe1040000");
           } catch(Exception e ) {
                 e.printStackTrace();
                 msg = "fail";
           }
           msg = "success";
  
           StringWriter sw = new StringWriter();
           JsonFactory f = new JsonFactory();
           JsonGenerator g = f.createJsonGenerator(sw);
  
           g.writeStartObject();
           g.writeStringField("status", msg);
           g.writeEndObject();
           g.close();
  
           return sw.toString();
       }
  }
  </code></pre>
  
  <p>Now, when I deploy my app to my app server and open up a rest client and type in:</p>
  
  <p><strong>http: // localhost:8080/myapp/apns/send</strong></p>
  
  <p>The rest client returns this:</p>
  
  <p><strong>HTTP/1.1 200 OK</strong></p>
  
  <p>The following log messages are outputted to my console:</p>
  
  <pre><code>01:47:51,985 WARN  [ApnsManager] Initializing connectiong with APNS...
  01:47:52,318 WARN  [ApnsManager] Sending push notification...
  </code></pre>
  
  <p><strong>MyAppDelegate.m</strong> </p>
  
  <pre><code>- (void) applicationDidFinishLaunching : 
    (UIApplication*) application 
  {   
    NSLog( @"LAUNCH" );
  
    // Configure REST engine
    RESTAPI* api = [RESTAPI getInstance];
    [api setNetworkAddress:kLocalAddress port:kDefaultPort];
  
    UIRemoteNotificationType notificationTypes 
       = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
    if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] 
        != notificationTypes) {
         NSLog(@"Registering for remote notifications...");
         [[UIApplication sharedApplication]
         registerForRemoteNotificationTypes:notificationTypes];
     } else {
         NSLog(@"Already registered for remote notifications...");
         // Uncomment this if you want to unregister
         // NSLog(@"Unregistering for remote notifications...");
         // [[UIApplication sharedApplication] unregisterForRemoteNotifications];
     }
  
   mainWindow = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] retain];
   toolsNav = [[[ToolsNav alloc] init] retain];
  
   [mainWindow addSubview:toolsNav.view];
   [mainWindow makeKeyAndVisible];
  } 
  </code></pre>
  
  <p>However, I don't receive the push notification on my app (residing on my iPhone)!</p>
  
  <p>Am really stumped at this point... </p>
  
  <p>What could I possibly be doing wrong? :(</p>
  
  <p>Is it a problem with the way I set up my RESTful web service (sorry I am a newbie to REST)?</p>
  
  <p>Would really appreciate it if someone could assist me with this...</p>
  
  <p>Thank you for taking the time to read this...</p>