问题描述
我使用的是ApnsPHP LIB为我的项目发送推送通知到我的用户:
I'm using the ApnsPHP lib for my project to send push notifications to my users:
private static function fnSendToIos($tokens, $text, $config)
{
set_time_limit(200);
// Adjust to your timezone
date_default_timezone_set('Europe/Moscow');
// Using Autoload all classes are loaded on-demand
require_once 'ApnsPHP/Autoload.php';
// Instantiate a new ApnsPHP_Push object
$push = new ApnsPHP_Push(
ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
$config['sert']
);
// Set the Root Certificate Autority to verify the Apple remote peer
$push->setRootCertificationAuthority($config['RootCertificat']);
// Connect to the Apple Push Notification Service
$push->connect();
// Instantiate a new Message with a single recipient
$message = new ApnsPHP_Message();
//Put all tokens as recipients in one message
foreach ($tokens as $token) {
$message->addRecipient($token);
}
// Set a custom identifier. To get back this identifier use the getCustomIdentifier() method
// over a ApnsPHP_Message object retrieved with the getErrors() message.
$message->setCustomIdentifier("Message-Badge-1");
// Set badge icon to "1"
$message->setBadge(1);
// Play the default sound
$message->setSound();
// Set a simple welcome text
$message->setText($text);
// Add the message to the message queue
$push->add($message);
// Send all messages in the message queue
$push->send();
// Disconnect from the Apple Push Notification Service
$push->disconnect();
// Examine the error message container
$aErrorQueue = $push->getErrors();
if (!empty($aErrorQueue)) {
log2file('iosPushNotifications', 'Send push notifications err:' . print_r($aErrorQueue));
}
}
但它的工作原理很慢。跑得快的第一条消息,但每下一次需要1分钟以上。
日志文件:
But it works very slow. The first message run fast, but every next needs 1 min or more.log file:
2015-05-01 17:58:30 UTC 192.168.2.1 a3d05958a5f7acdede098a2369ea7782
INFO: Trying ssl://gateway.sandbox.push.apple.com:2195...
2015-05-01 17:58:31 UTC 192.168.2.1 749386767ecdb42b51961dc90cfce4c4
INFO: Connected to ssl://gateway.sandbox.push.apple.com:2195.
2015-05-01 17:58:31 UTC 192.168.2.1 aacaa098ebce25abd2848d37962f0ae8
INFO: Sending messages queue, run #1: 2 message(s) left in queue.
2015-05-01 17:58:31 UTC 192.168.2.1 bde685d96b4309af2d9b8d592576f48d
STATUS: Sending message ID 1 [custom identifier: Message-Badge-1] (1/3): 150 bytes.
2015-05-01 17:59:31 UTC 192.168.2.1 1bdbc022e1037b8be36e40bb883c364c
STATUS: Sending message ID 2 [custom identifier: Message-Badge-1] (1/3): 150 bytes.
2015-05-01 18:00:32 UTC 192.168.2.1 7a81d80022327339521d4ba54b64c4cf
INFO: Disconnected.
有是消息之间太多的时间。什么是错的?
There is too much time between messages. What is wrong?
推荐答案
在一分钟的延迟造成的,因为图书馆正在等待来自APNS服务器的每个消息发送后的响应。
我发现在不使用的解决方案ApnsPHP库。随着错误处理和反馈就可以在我的情况下使用。
Delays in one minute caused because the library is waiting for a response from the APNS server after each message sending.I found a solution without use the ApnsPHP library. With error handler and feedback it can be used in my case.
code,这对我的作品(也许是帮助别人):
Code, that works for me (maybe it help someone):
/**
*@param array $tokens - device tokens
*@param string $text - push notification message text
*@param array $config - contains paths to certificates
*/
private function fnSendIosUseSockets($tokens, $text, $config)
{
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $config['sert']);
stream_context_set_option($ctx, 'ssl', 'passphrase', '');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
$err,
$errstr,
60,
STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
$ctx);
if (!$fp) {
log2file('iosPushNotifications', "Filed to connect push.apple.com. Err: $err $errstr " . PHP_EOL);
return false; //exit
}
echo 'Connected to APNS' . PHP_EOL;
foreach($tokens as $token) {
// Create the payload body
$body['aps'] = array(
'badge' => +1,
'alert' => $text,
'sound' => 'default'
);
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $token) . 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 'Message successfully delivered amar' . $text . PHP_EOL;
}
// Close the connection to the server
fclose($fp);
echo "Disconnect.";
}
这篇关于使用ApnsPHP推通知是缓慢的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!