php - APNS 不再适用于 PHP

标签 php ios xcode ubuntu apple-push-notifications

几天前,APNS 服务运行良好。唯一改变的是我将服务器从 Ubuntu 12.10 升级到 13.04。升级后我的 apns 停止工作。我不知道为什么,但这是我在服务器上唯一更改的地方。

我还尝试使用从我的 mac 生成的证书远程登录 gateway.push.apple.com:2195 一切正常。

但是当我尝试将我的服务器与 php 代码一起使用时,我得到了这个响应:

连接失败:0

          <?php

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

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

    // Put your alert message here:
      $message = 'My first push notification!';

    ////////////////////////////////////////////////////////////////////////////////

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

最佳答案

尝试将正确的证书路径放在 php 脚本中,即

stream_context_set_option($ctx, 'ssl', 'local_cert',APPPATH.'../assets/apns.pem');

关于php - APNS 不再适用于 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966029/

相关文章:

ios - 在加载时显示等待警报 View

ios - 将图像保存到核心数据?

iOS 应用程序(xCode 替代品)

ios - 如何转换音频以便可以跨设备传输

javascript - 基于分层(父子)数据的数据驱动面包屑

php - 使用 sqlite/php 获取两个不同行/列中两个数字的差异

html - Apple FaceTime URL 方案未发起调用

objective-c - iPad 3 应用程序开发的图像大小(分辨率)是多少?

javascript - 单击 <a> 标签时加载 div 内的 PHP 文件

php - symfony 2 在 url 末尾的斜杠