无法通过php服务在iphone上接收Apple推送通知

时间:2014-11-04 10:24:51

标签: php iphone service

如果这是一个重复的问题,我很抱歉,但我找不到我想要的解决方案。这是我的php服务代码,可以在Iphone上发送通知:

  <?php
    $data = array();
  // Put your device token here (without spaces):
    $deviceToken = 'eb9ea0d12eb9a0bae159c7e54fa59baee22329df';
  // Put your private key's passphrase here:
    $passphrase = 'dell';
    $message = 'Push notification service';
  ////////////////////////////////////////////////////////////////////////////////
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'application/controllers/ck.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);

    $data['response'] = '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)
       $data['response'] = 'Message not delivered' . PHP_EOL;
    else
        $data['response'] = 'Message successfully delivered' . PHP_EOL;

   // Close the connection to the server
    fclose($fp);
    $response->skills = $data;
    $this->response($response, 200);

 ?>

我在php(代码Ignitor)上运行服务以发送通知但我没有使用代码点火器。虽然上述服务运行成功但我没有收到手机通知。 任何人都可以告诉我们这个问题。

1 个答案:

答案 0 :(得分:0)

这对我有用:

        $ctx = stream_context_create();

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

        $webServiceLocation = 'tls://gateway.push.apple.com:2195';
        $fp = stream_socket_client(
                $webServiceLocation, $err,
                $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

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

        $body['aps'] = array('alert' => $message, 'sound' => 'default');

        $payload = json_encode($body);

        $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

        $result = fwrite($fp, $msg, strlen($msg));

        fclose($fp);