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?