我是一个PHP菜鸟,需要一些帮助。我已经建立了推送服务,将推送消息发送到iphone。它在评论中说,将多个消息组合成一个数据包更有效。此外,我现在打电话删除每条消息。如何将多个消息组合到一个包中?
谢谢
function start()
{
writeToLog('Connecting to ' . $this->server);
if (!$this->connectToAPNS())
exit;
while (true)
{
// Do at most 20 messages at a time. Note: we send each message in
// a separate packet to APNS. It would be more efficient if we
// combined several messages into one packet, but this script isn't
// smart enough to do that. ;-)
$stmt = $this->pdo->prepare('SELECT * FROM push_queue WHERE time_sent IS NULL LIMIT 20');
$stmt->execute();
$messages = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($messages as $message)
{
if ($this->sendNotification($message->message_id, $message->device_token, $message->payload))
{
//$stmt = $this->pdo->prepare('UPDATE push_queue SET time_sent = NOW() WHERE message_id = ?');
//$stmt->execute(array($message->message_id));
$stmt = $this->pdo->prepare('DELETE FROM push_queue WHERE message_id = ?');
$stmt->execute(array($message->message_id));
}
else // failed to deliver
{
$this->reconnectToAPNS();
}
}
unset($messages);
sleep(5);
}
}
答案 0 :(得分:4)
您可以将已删除的邮件ID分组到数组
中$deletedIds = array()
foreach ($messages as $message)
{
if ($this->sendNotification())
{
$deletedIds[] = $message->message_id;
}
}
之后,您可以使用查询方法
删除此类消息$this->pdo->query('DELETE FROM push_queue WHERE message_id IN ('.implode(',', $deletedIds).')');