Skip to content

Instantly share code, notes, and snippets.

@samvermette
Created December 30, 2010 07:40
Show Gist options
  • Select an option

  • Save samvermette/759564 to your computer and use it in GitHub Desktop.

Select an option

Save samvermette/759564 to your computer and use it in GitHub Desktop.

Revisions

  1. samvermette renamed this gist Dec 30, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.php → apn-server.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    $apnsHost = 'gateway.sandbox.push.apple.com';
    $apnsCert = 'apns-dev.pem';
    $apnsPort = 2195;
  2. samvermette renamed this gist Dec 30, 2010. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. samvermette created this gist Dec 30, 2010.
    17 changes: 17 additions & 0 deletions apn-server.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    $apnsHost = 'gateway.sandbox.push.apple.com';
    $apnsCert = 'apns-dev.pem';
    $apnsPort = 2195;

    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

    $payload['aps'] = array('alert' => 'Oh hai!', 'badge' => 1, 'sound' => 'default');
    $output = json_encode($payload);
    $token = pack('H*', str_replace(' ', '', $token))
    $apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) . $output;
    fwrite($apns, $apnsMessage);

    socket_close($apns);
    fclose($apns);