简体   繁体   English

在PHP上为APSN推送服务器(Apple)

[英]Push Server on Php for APSN(Apple)

I tried to send Push notification for iPhone. 我试图发送iPhone的推送通知。 I didn't put password because certificate is without it. 我没有输入密码,因为证书没有密码。 I convert device token njInRSuzfRLxdCiv8dG9JqRZLvxxTK95HRVYCvPGAUw= from Base64 to HEX But notification didn't come. 我将设备令牌njInRSuzfRLxdCiv8dG9JqRZLvxxTK95HRVYCvPGAUw =从Base64转换为HEX,但未收到通知。 What can be the reason? 可能是什么原因? I also receive message that i connected to APNS and success. 我还收到我连接到APNS并成功的消息。 as told my iPhone developer the certificates is correct because previously they was used on PARSE service Here is my code 告诉我的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);

This is my code and it work well, maybe try to pass your passphrase blank instead ignore it. 这是我的代码,效果很好,也许尝试通过您的密码短语空白而不是忽略它。 Other solution can be to put a passphrase to your cert, i've never tried without it, it can be a security issue. 其他解决方案可以是将密码短语放入您的证书,我从来没有尝试过,这可能是一个安全问题。

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

?>

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

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