面对ios推送通知php代码中的问题

我在php中使用这个代码…

function pushnotificationios( $deviceToken, $message, $badges){ $passphrase = "12345"; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT'].'/include/ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $fp = stream_socket_client( "ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); $body['aps'] = array( //'badge' => $badges, 'badge' => "+1", 'alert' => $message['message'], 'sound' => 'default', 'content-available' => '1' ); $body['msg'] =$message; $payload = json_encode($body); $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; $result = fwrite($fp, $msg, strlen($msg)); //echo "<pre>"; print_r($result); fclose($fp); return $result; } 

.pem文件和他们的密码是正确的。 但是当我打这个function,那么只有在生产模式,它给我返回false;

 $result = pushnotificationios( $deviceToken, $message, $badges); echo "<pre>"; print_r($result); 

这需要太多的时间来回应。

目前我没有find任何解决scheme..我的API是去苹果,我的通知不工作。 有趣的是它是一个聊天应用程序,整个应用程序是基于通知。 对我来说,这是糟糕的一天。

请帮助,如果有什么事情发生对我的应用程序。

生产:

SSL://gateway.push.apple.com:2195

发展:

SSL://gateway.sandbox.push.apple.com:2195

在我的应用程序。 我直接获取NSData deviceToken

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken]; // this is producing something like: // <xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb> // // and i am directly saving that to my servers database 

这是我在我的服务器。

 $from_database = "<xxxxxxx 9b0f527f xxxxxxxx 2727ed28 xxxxxxxx 4e693a61 xxxxxxx ac2f7dbb>"; // this removes the '<' and '>' to make the device token valid // $token_string = substr($from_database, 1, -1); $token_array[] = $token_string; // you can also add multiple 'token_string' to the 'token_array' for push notification broadcasting, i am currently doing that and it is working properly. ... public function __push_notification($message_input, $token_array) { $message = stripslashes($message_input); $passphrase = 'xxxxxx'; $cert = realpath('xxx.pem'); $payload = '{ "aps" : { "alert" : "'.$message.'", "badge" : 1, "sound" : "bingbong.aiff" } }'; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $cert); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) { // echo "Failed to connect $err $errstr <br>"; return; } else // echo "Post notification sent<br>"; $dev_array = array(); $dev_array = $token_array; foreach ($dev_array as $device_token) { $msg = chr(0) . pack("n", 32) . pack('H*', str_replace(' ', '', $device_token)) . pack("n", strlen($payload)) . $payload; // echo "sending message :" . $payload . "n"; fwrite($fp, $msg); } fclose($fp); } 

如果您仍然无法发送通知,请检查此故障排除推送通知

编辑

我一直在看你的推送通知推,前一阵子,那么我已经注意到你的'badge' => "+1"你不能像这样增加徽章。

唯一的select是在您的应用程序中pipe理它,并使用数据库保持正轨。

也许这是造成问题..和badge必须是一个数字,在这里: 表3-1

而且检查这个讨论可能也会对你有所帮助。

在生产模式下不要使用沙箱url

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

是的,最后我得到了解决办法。

问题是港口。 我使用Bluehost服务器。 Bluehost阻止推送通知使用的端口。 现在我改变了我的服务器和通知为我工作.. 感谢所有budy