2014-11-04 126 views
0

如果是重复问题,但我无法找到我正在寻找的解决方案,我很抱歉。以下是我的PHP服务代码在iPhone上发送通知:无法通过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)中运行以上服务发送通知,但我没有使用代码点火器。虽然上述服务运行成功,但我没有收到我手机上的通知。 任何人都可以告诉问题是什么。

回答

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

谢谢您的回答,我想这项服务,但仍以其运行没有错误,但没有显示iphone – Guneet 2014-11-05 04:59:43

+0

的任何通知,那么你必须检查密码和设备标识以确保它们对您正在测试的环境(sanbox,生产)是正确的 – matteo 2014-11-05 10:46:38