0

我尝试为 iPhone 发送推送通知。我没有输入密码,因为证书没有它。我将设备令牌 njInRSuzfRLxdCiv8dG9JqRZLvxxTK95HRVYCvPGAUw= 从 Base64 转换为 HEX 但没有收到通知。可能是什么原因?我还收到我连接到 APNS 并成功的消息。正如我告诉我的 iPhone 开发人员的那样,证书是正确的,因为以前它们被用于 PARSE 服务这是我的代码

$deviceToken = '9E3227452BB37D12F17428AFF1D1BD26A4592EFC714CAF791D15580AF3C6014C';
$message = 'TEST!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
$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;
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );
$payload = json_encode($body);
$msg =chr(0).pack('n',32).pack('H*', $deviceToken) . pack('n',strlen($payload)).$payload;
$result = fwrite($fp, $msg);
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

socket_close($fp);
fclose($fp);
4

1 回答 1

0

这是我的代码,它运行良好,也许尝试将您的密码短语传递给空白而不是忽略它。其他解决方案可以是为您的证书设置密码,我从来没有尝试过没有它,这可能是一个安全问题。

    <?php

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

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

// Put your alert message here:
$message = 'TEST!';

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

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


// 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 'Messaggio consegnato!' . PHP_EOL;

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

?>
于 2013-10-16T10:42:42.890 回答