title: Using PHP to send iOS Push Notifications via APNs
body: <p>I have implemented Push-Notifications into my Project and everything works fine so far. I've tried sending Notifications through Pusher and this worked out just fine.
  But I have to send them through PHP, which isn't working yet.
  I found many old explanations on how to make this happen, but none of them seem to work for me.</p>
  
  <p>This is what I'm trying to work with:</p>
  
  <pre><code>    // APNs Push testen auf Token
  $deviceToken = $_GET['key']; // Device-Token
  
  // Payload erstellen und JSON codieren
  $payload['aps'] = array('alert' =&gt; 'TitleText', 'badge' =&gt; 1, 'sound' =&gt; 'default');
  $payload = json_encode($payload);
  
  $apnsHost = 'gateway.sandbox.push.apple.com';
  $apnsPort = 2195;
  $apnsCert = 'certificate.pem';
  
  // Stream erstellen
  $streamContext = stream_context_create();
  stream_context_set_option($streamContext, 'ssl', 'certificate.cer', $apnsCert);
  
  $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
  if ($apns)
  {
    // Nachricht erstellen und senden
    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
    fwrite($apns, $apnsMessage);
  
    // Verbindung schliessen
    fclose($apns);
  }
  else
  {
    echo "Fehler!";
    var_dump($error);
    var_dump($errorString);
  }
  </code></pre>