APNS推送通知未被发送

问题描述:

Actually I am new to APNS and I have been using some help from the online forums and blogs. I am using PHP to implement the server side. The following is my PHP code

<?php

// Put your device token here (without spaces):
$deviceToken = '0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78';

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

// Put your alert message here:
$message = 'My first push notification!';

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

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

echo '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)
    echo 'Message not delivered' . PHP_EOL;
else
    echo '

' . PHP_EOL;

// Close the connection to the server
fclose($fp);

I have converted the .p12 file to a PEM file and named it as ck.pem and has hosted it on the same location as the php file resides. When I execute the PHP file, the following gets printed. Is there something I am missing. I am doubtful about the certificate part.

Connected to APNS
Message successfully delivered

其实我是APNS的新手,我一直在使用在线论坛和博客的帮助。 我正在使用PHP来实现服务器端。 以下是我的PHP代码 p>

 &lt;?php 
 
 //将设备令牌放在这里(不含空格):
 $ deviceToken ='0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78'; \  n 
 //在此处输入私钥的密码:
 $ passphrase ='pushchat'; 
 
 //在此处输入您的提醒信息:
 $ message ='我的第一个推送通知!'; 
 
  //////////////////////////////////////////////////  ////////////////////////////// 
 
 $ n ctx = stream_context_create(); 
stream_context_set_option($ ctx,'ssl'  ,'local_cert','ck.pem'); 
stream_context_set_option($ ctx,'ssl','passphrase',$ passphrase); 
 
 //打开与APNS服务器的连接
 $ fp = stream_socket_client(  
'sssl://gateway.sandbox.push.apple.com:2195',$ err,
 $ errstr,60,STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT,$ ctx); 
 
if(!$ fp)
退出 (“无法连接:$ err $ errstr”。PHP_EOL); 
 
echo'连接到APNS'。  PHP_EOL; 
 
 //创建有效负载主体
 $ body ['aps'] =数组(
'alert'=&gt; $ message,
'sound'=&gt;'default'
);  
 
 //将有效负载编码为JSON 
 $ payload = json_encode($ body); 
 
 //构建二进制通知
 $ msg = chr(0)。 打包('n',32)。  pack('H *',$ deviceToken)。  pack('n',strlen($ payload))。  $ payload; 
 
 //将其发送到服务器
 $ result = fwrite($ fp,$ msg,strlen($ msg)); 
 
if(!$ result)
 echo'消息未送达 '。  PHP_EOL; 
else 
 echo'
  code>  pre> 
 
 

'。 PHP_EOL; p>

  //关闭与服务器的连接
fclose($ fp); 
  code>  pre> 
 
 

我有 将.p12文件转换为PEM文件并将其命名为ck.pem并将其托管在与php文件所在的位置相同的位置。 当我执行PHP文件时,会打印以下内容。 有什么我想念的东西。 我对证书部分表示怀疑。 p>

 连接到APNS 
Message已成功发送
  code>  pre> 
  div>

I actually replaced the UDID of the device with the Device Token and it works like a charm. You cannot use UDID anymore.

I had the exact same problem, using the exact same example code - works perfect for android but fails for IOS with no returning error.

To fix my problem I had to create the SSL cert again against the app and then run

openssl x509 -in aps_development.cer -inform der -out  aps_development.pem

openssl pkcs12 -nocerts -out  aps_developmentKey.pem -in ios_development.p12

cat aps_development.pem aps_developmentKey.pem > ck.pem

Finially tested against: openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert aps_development.pem -key aps_developmentKey.pem -CAfile entrust_2048_ca.cer

After this everything worked perfect, that was on the 29th Sept - Its now stopped working so I think its the Certs again but at least it may help you with your solution.