【问题标题】:Apple Push Notification with PHP script not working?带有 PHP 脚本的 Apple 推送通知不起作用?
【发布时间】:2013-01-07 13:32:32
【问题描述】:

当我在终端上运行下面的脚本时,它会给出错误:

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in
/Applications/MAMP/htdocs/SimplePush/simplepush.php on line 21

Warning: stream_socket_client(): Failed to enable crypto in /Applications/MAMP/htdocs/SimplePush/
simplepush.php on line 21

Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195
(Unknown error) in /Applications/MAMP/htdocs/SimplePush/simplepush.php on line 21
Failed to connect: 0 

代码如下:

<?php

// Put your device token here (without spaces):
$deviceToken = '*******';

// Put your private key's passphrase here:
$passphrase = '***';

// Put your alert message here:
$message = 'Want more credits!';


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

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

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

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);

?>

【问题讨论】:

  • 脚本第21行为空,请修复sn-p。
  • 确保您正在使用您的生产证书,因为您正在尝试连接到 APNS 生产。
  • @Joe 我正在使用开发证书。我不是为现场目的而准备的。
  • 行号21似乎还可以。我已经通过删除多余的空格进行了尝试。但错误仍然相同。
  • @bhavyakothari 你知道为什么会发生这个错误吗..

标签: ios apple-push-notifications


【解决方案1】:

因为听起来您正在使用开发证书,您可能想尝试指向:gateway.sandbox.push.apple.com:2195。 URL gateway.push.apple.com:2195 用于生产证书。

【讨论】:

    【解决方案2】:

    老问题,但我遇到了同样的问题。我通过更改以下代码解决了它:

    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    

    stream_context_set_option($ctx, 'ssl', 'local_cert', '/Users/Ohemod/Desktop/ck.pem');
    

    脚本正在寻找 ck.pem 文件,但由于没有提供实际目录,我每次都收到错误消息。如果有人遇到同样的问题,请尝试第二个。记住你的文件目录和我的代码目录不一样。放上你的 ck.pem 文件的实际目录链接。

    【讨论】:

      【解决方案3】:

      https://curl.haxx.se/ca/cacert.pem 下载 cacert.pem 并将其保存在 PHP 脚本可用的同一文件夹中。

      包含语句 stream_context_set_option($ctx, 'ssl', 'cafile', 'cacert.pem');在 $ctx = stream_context_create();

      行之后

      仅供参考:

      语法:bool stream_context_set_option(资源$stream_or_context,字符串$wrapper,字符串$option,混合$value)

      更多选项请查看http://php.net/manual/en/context.ssl.php

      【讨论】:

        【解决方案4】:

        我就这样使用了你的脚本,我找到的解决方案是在 php 脚本中从 CURL 调用中调用它:

        这是适合我的代码。记得通过 HTTPS 调用它

        脚本 1(通过 CURL 调用):

        // set post fields
            $post = [
                'mensaje' => 'THE MESSAGE TO SEND',
                'device_id' => $device_id,
            ];
        
            $ch = curl_init('https://www.DOMAIN/push-curl-script.php');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            $response = curl_exec($ch);
            curl_close($ch);
        

        push-curl-script.php:

            $certificado_push   = "*****.pem";
        
            // Put your device token here (without spaces):
            $deviceToken        = $_POST["device_id"];
        
            // Put your private key's passphrase here:
            $passphrase         = '***';
        
            // Put your alert message here:
            $message            = $_POST["mensaje"];
        
        
            $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', $certificado_push);
            stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
        
            // Open a connection to the APNS server
            $fp = stream_socket_client(
            'ssl://gateway.'.($sandbox ? 'sandbox.' : '').'push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
        
            if (!$fp) {
                    echo "Failed to connect: $err $errstr" ;
            }else{    
                    echo 'Connected to APNS'  ;
        
                    // 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' ;
                    }else{
                        echo 'Message successfully delivered' ;
                    }
        
                    // Close the connection to the server
                    fclose($fp);
             }       
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-20
          相关资源
          最近更新 更多