我正在使用
this tutorial学习推送通知.
<?PHP
// Put your device token here (without spaces):
$devicetoken = '1675ba8bb005740bb514222227f861c30230a81e6eed6bb6b8f353c57831341d';
// Put your private key's passphrase here:
$passphrase = '111134';
// 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,'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));
echo 'result =' . $result. PHP_EOL;
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
我也配置应用程序推送通知.配置为推我还会重新配置配置文件,旧的删除一个,安装新的配置文件.
我运行应用程序它给我设备ID,然后我连接服务器沙箱和生产发送推送通知与他们的相对推送配置文件,但仍然我无法在我的设备上接收推送通知.
我也在我的设备上安装ipusher,并检查推送通知.他们来自该应用程序.
我注意到一个奇怪的事情是,我更改了我的应用程序标识符并使用任何其他应用程序ID,然后设备令牌保持不变
现在我的问题是我没有收到我的设备上的推送通知.
问题不在我的个人资料中.可能是我正在使用的PHP代码的错误,因为当我使用远程服务器上的简单apns时,它会发送推送通知.
通知收到的时间是6到7个小时.我认为这是由于我的设备端的网络问题.
但现在在生产情况2天后工作正常.现在通知没有时间在我的设备上交付,但在某些设备上需要30秒到5分钟.
如果您的设备上没有从其他应用程序接收推送通知,还可能会有一个问题,那么您应该检查您的DNS连接.
首先确保您使用的是:
>应用程序使用调试/释放条款进行编译
>您的钥匙扣有开发/生产推送通知证书
然后使用以下代码(已经测试了dev& production)
<?PHP
// Comment these lines in production mode
ini_set('display_errors','on');
error_reporting(E_ALL);
// Apns config
// true - use apns in production mode
// false - use apns in dev mode
define("PRODUCTION_MODE",false);
$serverId = 1;
$serverName = 'my-server-domain.com';
if(PRODUCTION_MODE) {
$apnsHost = 'gateway.sandBox.push.apple.com';
} else {
$apnsHost = 'gateway.push.apple.com';
}
$apnsPort = 2195;
if(PRODUCTION_MODE) {
// Use a development push certificate
$apnsCert = $_SERVER['DOCUMENT_ROOT'].'/apns/apns-dominos-development.pem';
} else {
// Use a production push certificate
$apnsCert = $_SERVER['DOCUMENT_ROOT'].'/apns/apns-dominos-production.pem';
}
// --- Sending push notification ---
// Insert your device token here
$device_token = "<dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8 dc6127d8>"; // Some Device Token
// Notification content
$payload = array();
//Basic message
$payload['aps'] = array(
'alert' => 'testing 1,2,3..','badge' => 1,'sound' => 'default',);
$payload['server'] = array(
'serverId' => $serverId,'name' => $serverName
);
// Add some custom data to notification
$payload['data'] = array(
'foo' => "bar"
);
$payload = json_encode($payload);
$streamContext = stream_context_create();
stream_context_set_option($streamContext,$apnsCert);
stream_context_set_option($streamContext,"");
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort,$error,$errorString,STREAM_CLIENT_CONNECT,$streamContext);
$devicetoken = str_replace(" ","",substr($device_token,1,-1));
echo $devicetoken;
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*',str_replace(' ','',$devicetoken)) . chr(0) . chr(mb_strlen($payload)) . $payload;
fwrite($apns,$apnsMessage);
//socket_close($apns);
fclose($apns);
?>