4

First off, sorry if this is a bit of an off topic question. I felt like this was the best place to go for a legitimate response, seeing as you guys and gals have never let me down.

So, I hired an iOS APP developer to help me with a project. I've never got into programming iOS applications so I'm just doing what he tells me to. My project is a PHP application that sends out notifications to iOS users that are stored as registered users (have a APNS token) in a database. However, when it comes to sending notifications through the Apple Push Notification Service we have been running into a ton of road blocks.

The developer tells me the below code is the only way to send out notifications:

    $token = 'created From APNS  - stored on a DB';
    $message = "hello world";
    $unread_msg_count = 'x';
    $passphrase = 'xxxxxxx'; // This is the passphrase used for file ck.pem
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); // ck.pem file stored in same directory 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    stream_context_set_option($ctx, 'ssl', 'verify_peer', true);
    stream_context_set_option($ctx, 'ssl', 'allow_self_signed', true);
    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp) {
        $fds = "Failed to connect: $err $errstr" . PHP_EOL;
        return false;
    } else {        

        // Create the payload body
        $body['aps'] = array('alert' => $message, 'sound' => 'default', 'badge' => 1);
        $body['server'] = array("unread_msg_count" => $unread_msg_count);
        // Encode the payload as JSON
        $payload = json_encode($body);
        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '',$token)) . pack('n', strlen($payload)) . $payload;
        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));
        // Close the connection to the server
        fclose($fp);
        return $result; 
     }

The ck.pem file is located in the same directory as the above script and the developer ensures me that it is the correct file.

The catch here is I use godaddy to host my website, using their shared hosting platform. With shared hosting, godaddy doesn't allow any ports other than port 80 and port 443 to be used. Since the APNS method above requires use of port 2195, I ended up having to set up a local XAMPP server to try and send out notifications. This all seems really clunky and the notifications are not working.

I was researching how the APNS works and from what I can tell the method my developer provided me is considered legacy code According to Apples Documentation. It seems the preferred method now uses HTTP2 which requires port 443 instead of port 2195. If that's true I should be able to push notifications directly from godaddys server instead of needing my own local server.

I brought up the possibility of using HPPT2 to my developer and he told me the method he provided me is the only way it can work. My question to the professionals out there is, is my developer correct or should I be using HTTP2 instead? If my developer is correct, does the method he provided me seem to be coded correctly for use in a TestFlight environment?

Austin
  • 1,619
  • 7
  • 25
  • 51
  • 2
    I am no expert in PHP, but I have written same APNS in Java & we just moved to HTTP/2 mechanism for Apple's APNS, initially we used old TLS way. HTTP/2 is much better than using old TLS mechanism, it gives you more payload size, return success/failure instantly to say if Apple server received the payload or not. Not sure though HTTP/2 support has reached PHP. – Atanu Pal Aug 04 '17 at 03:41
  • @AtanuPal What are the changes that you did when migrating from legacy method to HTTP/2? – Curiosity Sep 03 '18 at 05:34
  • @Curiousity - I used a Java library to do APNS, just had to upgrade that and according code changes, there is not other step involved for migration. From Apple's side everything is same. – Atanu Pal Sep 05 '18 at 05:46

0 Answers0