Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. public function testApn() {
  2.         $deviceToken = "451d545d5008c6c30a8c30612913d9a29e2c9f719893ddd34ed5753dd6d93b28";
  3.         $certificationPath = base_path().'/resources/certificates/pushCertifDev.pem';
  4.  
  5.        $ctx = stream_context_create();
  6.        // ck.pem is your certificate file
  7.        stream_context_set_option($ctx, 'ssl', 'local_cert', $certificationPath);
  8.        stream_context_set_option($ctx, 'ssl', 'passphrase', 'CL94V75877');
  9.        // Open a connection to the APNS server
  10.        $fp = stream_socket_client(
  11.            'ssl://gateway.sandbox.push.apple.com:2195', $err,
  12.            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  13.        if (!$fp)
  14.            exit("Failed to connect: $err $errstr" . PHP_EOL);
  15.  
  16.  
  17.        // Create the payload body
  18.        $body['aps'] = array(
  19.            'alert' => array(
  20.                'title' => 'title novi',
  21.                'body' => 'body novi',
  22.            ),
  23.            'sound' => 'default'
  24.        );
  25.  
  26.        // Encode the payload as JSON
  27.        $payload = json_encode($body);
  28.        // Build the binary notification
  29.        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
  30.        // Send it to the server
  31.        $result = fwrite($fp, $msg, strlen($msg));
  32.  
  33.        // Close the connection to the server
  34.        fclose($fp);
  35.        if (!$result)
  36.            return 'Message not delivered' . PHP_EOL;
  37.        else
  38.            return 'Message successfully delivered' . PHP_EOL . $result;
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement