简体   繁体   English

未发送Apple Push通知

[英]Apple Push notification not being sent

I have generated the .cer file, provision file with the correct device ID, generated the .pem file by combining the .cer and private key files, uploaded it to the server. 我已经生成了.cer文件,具有正确设备ID的配置文件,通过将.cer和私钥文件结合在一起生成了.pem文件,并将其上传到服务器。 The app id matches. 应用ID匹配。 I have provided the passphrase too, which is correct. 我也提供了密码,这是正确的。

I tested the port and connection using telnet from the server, it connects fine. 我使用服务器上的telnet测试了端口和连接,连接正常。

I have tested the certificate by openssl command, and it returned 0 - no errors. 我已经通过openssl命令测试了证书,它返回0-没有错误。

The certificates and the application is in development/debug mode, the iPhone is set up to receive notifications, the token is received and is delivered to the server correctly and in the same length - 64. 证书和应用程序处于开发/调试模式,iPhone设置为接收通知,接收令牌并将令牌正确地以相同的长度传递给服务器-64。

When sending the message from the server, the error code is 0 - which means no errors. 从服务器发送消息时,错误代码为0-表示没有错误。

Here is the code sample from the server: 这是来自服务器的代码示例:

$options = array('ssl' => array(
  'local_cert' => 'cert.pem',
  'passphrase' => 'pass'
));

$streamContext = stream_context_create();
stream_context_set_option($streamContext, $options);
$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

if ($apns)
{
    $payload['aps'] = array('alert' => 'push test', 'badge' => 1, 'sound' => 'default');
    $payload = json_encode($payload);

    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $token)) . chr(0) . chr(strlen($payload)) . $payload;
    fwrite($apns, $apnsMessage);

    fclose($apns);
} 
else 
{ 
    echo "Connection failed";
    echo $errorString."<br />";
    echo $error."<br />";
}

What else can I possibly try? 我还能尝试什么?

The code that worked in the end is the following: 最后起作用的代码如下:

    $ctx = stream_context_create();

    stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', 'pass');

    // Create the payload body
    $body['aps'] = array(
    'alert' => array('body' => 'Message', 'action-loc-key' => 'Show'),
    'sound' => 'default'
    );

    // Encode the payload as JSON
    $payload = json_encode($body);

    // Open a connection to the APNS server
    $apns = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$apns)
    {
        echo "Failed to connect: $err $errstr" . PHP_EOL;   
    }

    echo 'Connected to APNS' . PHP_EOL;

    $imsg = chr(0) . pack('n', 32) . pack('H*', $message) . pack('n', strlen($payload)) . $payload;

            // Send it to the server
    $res = fwrite($apns, $imsg, strlen($imsg));

    if (!$res)
    {
        echo 'Message not delivered' . PHP_EOL;
    }
    else
    {
        echo 'Message successfully delivered' . PHP_EOL;
    }
    fclose($apns);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM