2016-03-30 53 views
1

尝试使用APN将通知推送到我的iPhone时遇到一些故障。我用我一直在互联网上看到遍地一个PHP代码,所以我不认为这个问题是关于它:连接到Apple Push Notification服务时禁止访问

<?php 

// Put your device token here (without spaces): 
$deviceToken = 'mytoken'; // Of course this is my correct token here 

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

$message = $argv[1]; 
$url = $argv[2]; 

if (!$message || !$url) 
    exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://raywenderlich.com\'' . "\n"); 

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

$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', __DIR__.'/cert.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

// Open a connection to the APNS server 
$fp = stream_socket_client(
    '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' => $message, 
    'sound' => 'default', 
    'link_url' => $url, 
); 

// 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; 

stream_set_blocking($fp, 0); 

// 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); 

我有输出“消息发送成功”,所以连接已成功建立,同意发送的消息。但是,该设备没有收到任何东西。

我使用的反馈功能,试试就知道了什么是错的,但结果是空的(数组简直是空的):

<?php 

function send_feedback_request() { 
    //connect to the APNS feedback servers 
    //make sure you're using the right dev/production server & cert combo! 
    $stream_context = stream_context_create(); 
    stream_context_set_option($stream_context, 'ssl', 'local_cert', __DIR__.'/cert3.pem'); 
    stream_context_set_option($stream_context, 'ssl', 'passphrase', 'meteor0405'); 

    $apns = stream_socket_client('ssl://feedback.sandbox.push.apple.com:2196', $errcode, $errstr, 60, STREAM_CLIENT_CONNECT, $stream_context); 
    if(!$apns) { 
     echo "ERROR $errcode: $errstr\n"; 
     return; 
    } 


    $feedback_tokens = array(); 
    //and read the data on the connection: 
    while(!feof($apns)) { 
     $data = fread($apns, 38); 
     if(strlen($data)) { 
      $feedback_tokens[] = unpack("N1timestamp/n1length/H*devtoken", $data); 
     } 
    } 
    fclose($apns); 
    return $feedback_tokens; 
} 

var_dump(send_feedback_request()); 

你有什么我可以做,以找出什么是什么想法错误?它可能是由于证书? .cer文件已从Apple网站生成并已导入到Keychain Access中以生成.p12文件。然后,我将它转换为.pem格式,这要归功于openssl命令,这是我在下面的文件中使用的格式。

谢谢!

回答

0

如果您收到成功的消息,那么这意味着您的PHP代码是正确的,但pem文件中缺少某些内容。
检出pem文件创建步骤https://www.digicert.com/ssl-support/pem-ssl-creation.htm,然后重试。
希望它会有所帮助。

+0

它真的帮了,谢谢!问题确实来自PEM文件。我为任何有同样问题的人发布了问题的答案。 –

+0

给你的荣誉.. :) – Aparna

0

问题确实来自我的PEM文件。对于有同样问题的任何人,这是我所做的。

以.p12格式从iPhone Developper Connection Portal下载证书和私钥后,我必须将它们转换为.pem格式。之后,我必须合并这两个文件(首先放置证书的内容,然后是私钥)。这个合并必须放在一个新的.pem文件中,并且这是在PHP脚本中使用的文件。

以下是完整的教程,我跟着:https://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/

此外,PHP代码是不完全一样的。我在第一篇文章中使用的不正确,请使用教程的一个。

相关问题