推送通知的iPhone不工作 [英] Push Notification not working in Iphone
问题描述
我用这个PHP code
$查询=SELECT * FROM iphone_register
$ result_iphone =的mysql_query($查询);
$ fetres = mysql_num_rows($ result_iphone);而($行= mysql_fetch_object($ result_iphone)){ $ deviceToken = $ rows-> DEVICE_ID; $密码='******';
$消息='新推送通知!'; $徽章= 1; $ CTX = stream_context_create();
stream_context_set_option($ CTXSSL,的local_cert','ck.pem');
stream_context_set_option($ CTX,'SSL','密码',$密码); $计划生育=在stream_socket_client('SSL://gateway.sandbox.push.apple.com:2195',$犯错,$ errstr,60,STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT,$ CTX); 如果(!$ FP){
退出(无法连接:$犯错$ errstrPHP_EOL);
} $身体['APS'] =阵列(
'警报'=> 新通知到来,
身体=> $消息,
'徽章'=> $徽章,
声音= GT; newMessage.wav
); $有效载荷= json_en code($体); $味精= CHR(0)。包('N',32)。包(H *',$ deviceToken)。包('N',strlen的($有效载荷))。 $有效载荷; $结果=的fwrite($计划生育,$味精,strlen的($味精));
}如果(!$结果){
回声错误,通知不发送。 PHP_EOL;
}其他{
回声通知发送! 。 PHP_EOL;
}FCLOSE($ FP);
当我从服务器发送推送通知到设备,然后我得到了下面的错误。
我试图改变 ck.pem
文件的解决方案。
但它不工作。因此,我不这么认为它与 ck.pem(证书)
文件的问题。
警告:在stream_socket_client():无法连接到
SSL://gateway.sandbox.push.apple.com:2195(连接被拒绝)的
/home/sunstate/public_html/sunstate_api/gcm_message.php在线55
无法连接:111连接被拒绝
块引用>解决方案尝试像低于硬编码您的设备令牌code。
< PHP
$ deviceToken ='yourdevicetoken'; //屏蔽出于安全原因//密码短语私人密钥(ck.pem文件)
$传球='XXXXX';//从获得的参数HTTP GET或命令行
$消息= $ _GET [消息]或$消息= $ ARGV [1]或$消息='从BRL服务器接收到的信息';
$徽章=(int)的$ _ GET ['徽章']或$徽章=(int)的$ ARGV [2];
$声音= $ _GET ['声音']或$声音= $ ARGV [3];
// $ deviceToken = $ _GET ['deviceToken'];//构造通知有效载荷
$身体=阵列();
$身体['APS'] =阵列('警报'=> $消息);
如果($徽章)
$身体['APS'] ['徽章'] = $徽章;
如果($声音)
$身体['APS'] ['音'] = $的声音;/ *配置项目结束* /$ CTX = stream_context_create();
stream_context_set_option($ CTXSSL,的local_cert','ck_dev.pem');//假设去除私钥passphase。
stream_context_set_option($ CTX,'SSL','密码',$通行证);$计划生育=在stream_socket_client('SSL://gateway.sandbox.push.apple.com:2195',$犯错,$ errstr,60,STREAM_CLIENT_CONNECT,$ CTX);
如果(!$ FP){
打印无法连接$犯错$ errstr \\ N的;
返回;
}
其他{
打印连接OK \\ N的;
}$有效载荷= json_en code($体);
$味精= CHR(0)。包(N,32)。包(H *',str_replace函数('','',$ deviceToken))。包(N的strlen($有效载荷))。 $有效载荷;
打印发送消息。 $有效载荷。 \\ n;
FWRITE($ FP,$味精);
FCLOSE($ FP);
?>复制此code以.php文件,并将其上传到服务器,并通过增加实际设备令牌运行的浏览器文件。
此外,如果你正在使用任何托管服务器,那么你需要在该服务器上安装SSL,并要求他们打开相关通知端口这是阻止提供商。
I have used this php code
$query = "SELECT * FROM iphone_register"; $result_iphone = mysql_query($query); $fetres = mysql_num_rows($result_iphone); while ($rows = mysql_fetch_object($result_iphone)) { $deviceToken = $rows->device_id; $passphrase = '******'; $message = 'New Push Notification!'; $badge = 1; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $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); } $body['aps'] = array( 'alert' => 'new notification is arrived', 'body'=> $message, 'badge' => $badge, 'sound' => 'newMessage.wav' ); $payload = json_encode($body); $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; $result = fwrite($fp, $msg, strlen($msg)); } if (!$result) { echo 'Error, notification not sent' . PHP_EOL; } else { echo 'notification sent!' . PHP_EOL; } fclose($fp);
When I send push notification to device from server then I got following error.
I try to change the
ck.pem
file for solution.but it's not working. So I don't think so it has problem with
ck.pem(Certificate)
file.Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/sunstate/public_html/sunstate_api/gcm_message.php on line 55 Failed to connect: 111 Connection refused
解决方案Try like below code by hard coding you device token.
<?php $deviceToken = 'yourdevicetoken'; // masked for security reason // Passphrase for the private key (ck.pem file) $pass = 'xxxxx'; // Get the parameters from http get or from command line $message = $_GET['message'] or $message = $argv[1] or $message = 'Message received from BRL server'; $badge = (int)$_GET['badge'] or $badge = (int)$argv[2]; $sound = $_GET['sound'] or $sound = $argv[3]; //$deviceToken = $_GET['deviceToken']; // Construct the notification payload $body = array(); $body['aps'] = array('alert' => $message); if ($badge) $body['aps']['badge'] = $badge; if ($sound) $body['aps']['sound'] = $sound; /* End of Configurable Items */ $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_dev.pem'); // assume the private key passphase was removed. stream_context_set_option($ctx, 'ssl', 'passphrase', $pass); $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr,60,STREAM_CLIENT_CONNECT, $ctx); if (!$fp) { print "Failed to connect $err $errstr\n"; return; } else { print "Connection OK\n"; } $payload = json_encode($body); $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload; print "sending message :" . $payload . "\n"; fwrite($fp, $msg); fclose($fp); ?>
Copy this code in .php file and upload it to your server and run this file from browser by adding actual device token.
Also if you are using any hosting server then you need to install SSL on that server and ask them to open port related to notification which was blocked by provider.
这篇关于推送通知的iPhone不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!