记录第一次整合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端能不能接收到了,拜拜。

相关文章
相关标签/搜索