Iqb*_*han 8 php iphone objective-c push-notification apple-push-notifications
我正在使用本教程来学习推送通知.
<?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, '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'
);
// 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);
Run Code Online (Sandbox Code Playgroud)
我还配置推送通知的应用程序.配置推送后,我还重新创建配置文件,旧删除一个,安装新的配置文件.我运行app它给我设备ID然后我连接服务器沙箱和生产发送推送通知与他们的相对推送配置文件但仍然我无法在我的设备上接收推送通知.
我还在我的设备上安装ipusher并检查推送通知.他们来自那个应用程序.
我注意到一个奇怪的事情是我更改了我的应用程序标识符并使用任何其他应用程序ID,然后设备令牌保持不变
现在我的问题是我没有在我的设备上收到推送通知.
问题不在我的个人资料中.可能是错误是我正在使用的PHP代码,因为当我在远程服务器上使用简单的apns然后它发送推送通知.收到的通知时间为6到7个小时.我认为这是由于设备端的网络问题.但现在它在生产情况下2天后工作正常.现在,通知会立即在我的设备上发送,但在某些设备上需要30秒到5分钟.
如果您没有从其他应用程序接收设备上的推送通知,则可能还有一个问题,那么您应该检查DNS以获取连接.
小智 7
首先确保您使用的是:
然后使用以下代码(已经过开发和生产测试)
<?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, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', "");
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, 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);
?>
Run Code Online (Sandbox Code Playgroud)
好吧,我终于解决了我的问题。问题不在代码中,实际上问题出在我的 iPhone 中设置的错误 DNS 值。Iphone 会自动将路由器的 IP 地址放入 DNS 字段。现在我给出了我的服务提供商的 DNS 值,然后它就可以正常工作了。现在我一发送推送消息就收到了。
我希望它对其他人有帮助。
| 归档时间: |
|
| 查看次数: |
21481 次 |
| 最近记录: |