記錄第一次整合ios推送遇到的坑

測試環境

OSXphp

證書生成

這部分不少教程,基本沒什麼坑,直接跳過了git

嘗試鏈接

function send(){
    	$deviceToken = 'xx=';
        $ctx = stream_context_create();
        // ck.pem is your certificate file
        stream_context_set_option($ctx, 'ssl', 'local_cert', '/cert/push.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);

        // Open a connection to the APNS server
        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp)
            exit("Failed to connect: $err $errstr" . PHP_EOL);

        // Create the payload body
        $body['aps'] = array(
            'alert' => array(
                'title' => $data['mtitle'],
                'body' => $data['mdesc'],
             ),
            'sound' => 'default'
        );

        // Encode the payload as JSON
        $payload = json_encode($body);

        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));

        // Close the connection to the server
        fclose($fp);

        if (!$result)
            return 'Message not delivered' . PHP_EOL;
        else
            return 'Message successfully delivered' . PHP_EOL;
    }

第一次測試:失敗

返回錯誤:json

1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

缺乏本機的驗證證書 cd 進去 /etc/ssl/certs 發現空空如也,而後app

curl http://curl.haxx.se/ca/cacert.pem -o ./cacert.pem

把默認的cacert.pem加進去,而後再加到php.ini openssl.cafile=/etc/ssl/certs/cacert.pem 裏,再跑代碼curl

第二次測試:失敗

返回錯誤:socket

1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure

握手失敗最早想到應該是證書錯誤了,估計沒讀到push證書,把證書地址改爲以下測試

stream_context_set_option($ctx, 'ssl', 'local_cert', __DIR__.'/cert/push.pem');

再跑一次ui

第三次測試:失敗

返回錯誤:url

pack(): Type H: illegal hex digit z

證書的問題是過了,不過這個應該是代碼的問題,代碼是網上copy過來的,還沒細看。 改爲以下:code

$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))) . pack('n', strlen($payload)) . $payload;

再跑一次

第四次測試:成功

最終代碼:

function send(){
    	$deviceToken = 'xxx';
		$ctx = stream_context_create();
        // ck.pem is your certificate file
        stream_context_set_option($ctx, 'ssl', 'local_cert', __DIR__.'/cert/push.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);
        // stream_context_set_option($ctx, 'ssl', 'extensions', 'ssl_client');

        // Open a connection to the APNS server
        $fp = stream_socket_client(
            'ssl://gateway.sandbox.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp)
            exit("Failed to connect: $err $errstr" . PHP_EOL);

        $data['mtitle'] = 'test';
        $data['mdesc'] = 'hello world';

        // Create the payload body
        $body['aps'] = array(
            'alert' => array(
                'title' => $data['mtitle'],
                'body' => $data['mdesc'],
             ),
            'sound' => 'default'
        );

        // Encode the payload as JSON
        $payload = json_encode($body);

        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))) . pack('n', strlen($payload)) . $payload;

        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));

        // Close the connection to the server
        fclose($fp);

        if (!$result)
            var_dump('Message not delivered' . PHP_EOL);
        else
            var_dump('Message successfully delivered' . PHP_EOL);
    }

Testcase的代碼就不貼出來了,而後去看看IOS端能不能接收到了,拜拜。

相關文章
相關標籤/搜索