2015-12-25 51 views
0

我正在使用以下代码向ios设备发送推送通知。我在PHP端获得成功传递的消息,但未收到iOS设备中的推送。PHP - APNs消息已发送但未在iOS设备上收到

我已经检查替换.pem文件nd也测试与发送箱和生产两者。它不能同时工作。请为我提供调试此问题的方法。

function iosTest($tToken) 
 
{ 
 
\t // Provide the Host Information. 
 
//$tHost = 'gateway.sandbox.push.apple.com'; 
 
$tHost = 'gateway.push.apple.com'; 
 
$tPort = 2195; 
 
//echo "hi"; 
 
// Provide the Certificate and Key Data. 
 
\t 
 
$tCert = $_SERVER['DOCUMENT_ROOT'].'/N****d.pem'; 
 
// Provide the Private Key Passphrase (alternatively you can keep this secrete 
 
// and enter the key manually on the terminal -> remove relevant line from code). 
 
// Replace XXXXX with your Passphrase 
 
$tPassphrase = 'harsh'; 
 
// Provide the Device Identifier (Ensure that the Identifier does not have spaces in it). 
 

 
// The message that is to appear on the dialog. 
 
$tAlert = 'Testing..'; 
 
// The Badge Number for the Application Icon (integer >=0). 
 
$tBadge = 1; 
 
// Audible Notification Option. 
 
$tSound = 'default'; 
 
// The content that is returned by the LiveCode "pushNotificationReceived" message. 
 
$tPayload = 'APNS Message Handled by LiveCode'; 
 
// Create the message content that is to be sent to the device. 
 
$tBody['aps'] = array (
 
'alert' => $tAlert, 
 
'badge' => $tBadge, 
 
'sound' => $tSound, 
 
); 
 
//$tBody ['payload'] = $tPayload; 
 
// Encode the body to JSON. 
 
$tBody = json_encode ($tBody); 
 
echo $tBody; 
 
// Create the Socket Stream. 
 
$tContext = stream_context_create(); 
 
stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert); 
 
// Remove this line if you would like to enter the Private Key Passphrase manually. 
 
stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase); 
 
// Open the Connection to the APNS Server. 
 
$tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext); 
 
// Check if we were able to open a socket. 
 
if (!$tSocket) 
 
exit ("APNS Connection Failed: $error $errstr" . PHP_EOL); 
 
// Build the Binary Notification. 
 
$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody; 
 
// Send the Notification to the Server. 
 
$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg)); 
 
if ($tResult) 
 
echo 'Delivered Message to APNS' . PHP_EOL; 
 
else 
 
echo 'Could not Deliver Message to APNS' . PHP_EOL; 
 
// Close the Connection to the Server. 
 
fclose ($tSocket); 
 
//send_feedback_request(); 
 
}

在此先感谢。

回答

0

请使用下面的代码,因为它的工作在我结束

<?php 
    // Put your device token here (without spaces): 
    $deviceToken = '17b2f31afd5c9736ad48ffca8d1936046431c6fc1f3f3ac661bcbfbf97ca60cc'; 


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

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

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

    echo 'Connected to APNS' . PHP_EOL; 

    // Create the payload body 
    $body['aps'] = array(
     'alert' => 'Breaking News', 
     'sound' => 'default', 
     'link_url' => $url, 
     'category' => 'NEWS_CATEGORY', 
    ); 

    // 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); 
?> 
相关问题