美文网首页
iOS 利用PHP来进行消息推送

iOS 利用PHP来进行消息推送

作者: 豪冷 | 来源:发表于2019-08-15 10:27 被阅读0次

    前提:

    准备好推送证书aps_development.cer及其p12文件aps_development.p12


    开始:

    假设p12文件密码是:123456

    终端操作:进入到存放cerp12文件的文件夹

    1.利用aps_development.cer文件生成.pem文件

    openssl x509 -in aps_development.cer -inform der -out cer.pem

    2.利用aps_development.p12文件生成.pem文件:

    openssl pkcs12 -nocerts -out p12.pem -in aps_development.p12

    需要输入p12的密码:123456

    MAC verified OK

    Enter PEM pass phrase:(设置密码)654321

    Verifying - Enter PEM pass phrase:(再次输入密码)654321

    3.利用两个.pem文件生成另一个.pem文件

    cat cer.pem p12.pem > cp.pem

    4.发消息

    php push.php

    push.php文件要注意的地方:

    1.deviceToken

    2.p12.pem密码

    3.cp.pem文件。代码中:stream_context_set_option($ctx, 'ssl', 'local_cert', 'cp.pem'); 名字要对应。

    php文件代码:

    <?php  
    
    //deviceToken
    $deviceToken = 'fc766c03b0857fb08e14ecdde3bdfef80f24030cb955dd5804fe143bbc944665';  
    
    //p12.pem文件的密码 
    $passphrase = '654321';  
    
    //推送的消息
    $message = '这是一条豪冷的消息^_^';  
    
    ////////////////////////////////////////////////////////////////////////////////  
    
    $ctx = stream_context_create();  
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'cp.pem');  
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);  
    
    // Open a connection to the APNS server  
    // 上架:ssl://gateway.push.apple.com:2195 
    // 测试:ssl://gateway.sandbox.push.apple.com:2195
    $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);  
    else echo 'Connected to APNS' . PHP_EOL;  
    
    // Create the payload body  
    $body['aps'] = array(  
    'alert' => $message,  
    '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));  
    
    if (!$result)  echo 'Message not delivered' . PHP_EOL;  
    else  echo 'Message successfully delivered' . PHP_EOL;  
    
    // Close the connection to the server  
    fclose($fp);  
    ?>  
    

    要想一步一步的学,请

    参考博客:http://blog.csdn.net/showhilllee/article/details/8631734


    如果执行命令后,出现错误:

    Warning: stream_socket_client(): SSL operation failed with code 1\. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in push.php on line 21

    则需要下载证书entrust_2048_ca.cer及添加代码:

    stream_context_set_option($ctx, 'ssl', 'local_cert', 'cp.pem');  
    stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);  
    

    下载地址:https://www.entrust.com/get-support/ssl-certificate-support/root-certificate-downloads/

    参考博客:http://blog.csdn.net/heyufei/article/details/53616961?utm_source=itdadao&utm_medium=referral


    不清楚,是不是因为php版本的问题,还是因为从一台电脑上导出p12文件到另一台电脑使用,就需要加上2048那个文件。

    第一台电脑,php版本(查看命令 php -v),不需要2048文件。

    PHP 5.5.29 (cli) (built: Sep  9 2015 00:26:40) 
    
    Copyright (c) 1997-2015 The PHP Group
    
    Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
    

    另一台MAC电脑,php版本是,需要2048文件。

    PHP 5.6.25 (cli) (built: Sep 19 2016 15:45:41) 
    
    Copyright (c) 1997-2016 The PHP Group
    
    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    

    相关文章

      网友评论

          本文标题:iOS 利用PHP来进行消息推送

          本文链接:https://www.haomeiwen.com/subject/gdpajctx.html