在向多个iphone设备发送推送通知时收到警告消息

时间:2013-06-20 05:27:55

标签: php apple-push-notifications

当我使用php webservices向多个iphone设备发送推送通知时,我收到警告消息,如:

Warning: stream_socket_client() [function.stream-socket-client]: SSL: crypto enabling timeout in /home/sample_app/pushnotification.php on line 66

Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /home/sample_app/pushnotification.php on line 66

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in /home/sample_app/pushnotification.php on line 66

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/sample_app/pushnotification.php on line 79

这是我的代码:

$message='testing';
$q1="select devicetoken from tbl_devicetokens";
$re1 = mysql_query($q1);
while($row1=mysql_fetch_row($re1))
{
    pushMessage($row1[0],$message,'myapp');
}


function pushMessage($deviceToken,$message,$app) {
    echo "Sending iPhone Push Notifications to " . $deviceToken . "<br /><br />";
    echo "Your Message: " . $message . "<br /><br />";
    $time = time();
    $apnsHost = 'gateway.push.apple.com';
    $apnsPort = 2195;
    $apnsCert = 'apns-myapp.pem';
    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
    if($apns) {
            $payload = array();
            $payload['aps'] = array('alert' => $message, 'badge' => 0, 'sound' => 'default');
            $payload = json_encode($payload);
            $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
            fwrite($apns, $apnsMessage);
    } else { 
            echo "Connection Failed - iPhone Push Notifications Server";
            echo $errorString."<br />";
            echo $error."<br />";
    }
    //socket_close($apns);
    fclose($apns);
}

那么,任何人都可以告诉我如何解决这个问题

2 个答案:

答案 0 :(得分:1)

似乎是一个愚蠢的答案,但你尝试过防火墙/ iptables吗?没有进一步信息的那些错误看起来像我的连接问题。首先需要解决的是超时问题。

答案 1 :(得分:1)

将单个通知推送到单个设备时,是否仍会发生这种情况?

若是:

  • 您确定自己拥有正确的证书路径吗?
  • 确保证书文件名不包含字母字符以外的其他字符。

如果不是:

  • 确保您使用正确的证书以获得正确的设备环境(开发,生产)
  • 如果设备被越狱,则不会收到推送通知。