OGeek|极客世界-中国程序员成长平台

标题: php - 尝试通过 php 文件执行 Apple 推送通知时出错 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:15
标题: php - 尝试通过 php 文件执行 Apple 推送通知时出错

我正在使用 php 服务器在 IOS 中处理推送通知,并且我生成了应用程序的证书和 key ,我还确保从 ssl://gateway.sandbox.push.apple.com:2196 和2195,但在尝试连接 ssl 时,我总是收到此错误,我也确信来自所有关键文件的许可

Warning: stream_socket_client(): SSL: crypto enabling timeout in /Users/samahahmaed/Desktop/CER/newspush.php on line 24

Warning: stream_socket_client(): Failed to enable crypto in /Users/samahahmaed/Desktop/CER/newspush.php on line 24

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/samahahmaed/Desktop/CER/newspush.php on line 24
Failed to connect: 0

已编辑:

当我尝试这个命令时

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushCertificate.pem -key PushKey.pem -CApath /etc/ssl/certs/Entrust_Root_Certification_Authority.pem

我收到此错误

CONNECTED(00000003)
write:errno=54

php 文件:

  <?php

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

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

 $message = $argv[1];
 $url = $argv[2];

 if (!$message || !$url)
exit('Example Usage: $php newspush.php \'Breaking News!\'   \'https://raywenderlich.com\'' . "\n");



$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert',  'apple_push_notification_production.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $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);

  echo 'Connected to APNS' . PHP_EOL;

  // Create the payload body
   $body['aps'] = array(
   'alert' => $message,
   'sound' => 'default',
   'link_url' => $url,
  );

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

我对这个问题进行了很多搜索,并尝试了所有可能的解决方案,但没有任何结果我现在能做什么?

已编辑:

在 openssl 命令中添加 -debug 后最奇怪的是以下几行: enter image description here



Best Answer-推荐答案


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

此行看起来您正在使用生产证书连接到沙盒 APNS。尝试使用开发证书。您可以从苹果开发者仪表板中获得相同的信息。

关于php - 尝试通过 php 文件执行 Apple 推送通知时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38945342/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4