Skip to content

Instantly share code, notes, and snippets.

@pimpreneil
Created January 30, 2014 09:31

Revisions

  1. pimpreneil created this gist Jan 30, 2014.
    22 changes: 22 additions & 0 deletions APNS command 3
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // token: An apple token
    // message: The message to push
    // id: The push message identifier
    // expiry: Push expiration date
    // priority: Priority (5 or 10)

    public function createPayload($token, $message, $identifier, $expiry, $priority)
    {
    $body = json_encode(array('aps' => array('alert' => $message)), JSON_UNESCAPED_UNICODE);
    $packedToken = pack("H*", $token);
    $tokenData = chr(1) . pack('n',strlen($packedToken)) . $packedToken;
    $payloadBody = chr(2) . pack('n',strlen($body)) . $body;
    $packedIdentifier = pack("N", $identifier);
    $identifier = chr(3) . pack('n',strlen($packedIdentifier)) . $packedIdentifier;
    $packedExpiry = pack("N", $expiry);
    $expiry = chr(4) . pack('n',strlen($packedExpiry)) . $packedExpiry;
    $packedPriority = chr($priority);
    $priority = chr(5) . pack('n',strlen($packedPriority)) . $packedPriority;
    $payload = $tokenData.$payloadBody.$identifier.$expiry.$priority;

    return chr(2) . pack("N", strlen($payload)) . $payload;
    }