2
Good night.
I have a notification system where users receive notifications from users of who they are following. For example, "João commented x posts" And it’s working when, for example, there’s only one follower. The problem is when the person has more than one follower in which the same notification has to be sent to all the users he is following.
Table notificacoes
:
CREATE TABLE IF NOT EXISTS `notificacoes` (
`notificacao_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_destino_notificacao` int(11) NOT NULL,
`notificacao` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`tipo` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`data` datetime NOT NULL,
PRIMARY KEY (`notificacao_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=15 ;
Table notificacoes_visualizacao
:
CREATE TABLE IF NOT EXISTS `notificacoes_visualizacao` (
`id_notificacao` int(11) NOT NULL,
`id_usuario_que_visualizou` int(11) NOT NULL,
PRIMARY KEY (`id_notificacao`,`id_usuario_que_visualizou`),
KEY `fk_notificacao_visualizacao_usuario` (`id_usuario_que_visualizou`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
PHP code:
<?php
header('Cache-Control: no-cache, must-revalidate');
//Alteramos o cabeçalho para que o retorno seja do tipo JSON
header('Content-Type: application/json; charset=utf-8');
session_start();
require_once("../funcoes/funcoes.php");
$user_actual = $_REQUEST['user_logado'];
$result_notificacoes = $conexao->prepare("SELECT * FROM notificacoes as noti
LEFT JOIN notificacoes_visualizacao as noti_vi ON noti_vi.id_notificacao = noti.notificacao_id
INNER JOIN users_social as user ON user.id = noti.user_id
WHERE noti.user_id IN (SELECT seg.followed FROM seguidores as seg WHERE seg.follower = :user_actual)
AND noti_vi.id_usuario_que_visualizou is NULL
OR noti.user_destino_notificacao = :user_atual2");
$result_notificacoes->bindParam(':user_actual', $user_actual, PDO::PARAM_INT);
$result_notificacoes->bindParam(':user_atual2', $user_actual, PDO::PARAM_INT);
$result_notificacoes->execute();
$return = array();
$notificacoes = $result_notificacoes->fetchAll(PDO::FETCH_ASSOC);
foreach ($notificacoes as $row_notificacoes) {
// MOSTRA TEMPO DO POST
$data_actual = date('Y-m-d H:i:s');
$data = strtotime($row_notificacoes['data']);
$data_post = strtotime($data_actual) - $data;
switch (post_data) {
// Segundos
case ($data_post < 60):
$count = $data_post;
if($count == 0)
$count = "Agora mesmo";
elseif ($count == 1)
$suffix = "segundo atrás";
else
$suffix = "segundos atrás";
break;
// Minutos
case ($data_post > 60 && $data_post < 3600):
$count = floor($data_post/60);
if($count == 1)
$suffix = "minuto atrás";
else
$suffix = "minutos atrás";
break;
// Horas
case ($data_post > 3600 && $data_post < 86400):
$count = floor($data_post/3600);
if($count == 1)
$suffix = "hora atrás";
else
$suffix = "horas atrás";
break;
// Dias
case ($data_post > 86400 && $data_post < 2629743):
$count = floor($data_post/86400);
if($count == 1)
$suffix = "dia atrás";
else
$suffix = "dias atrás";
break;
// Meses
case ($data_post > 2629743 && $data_post < 31556926):
$count = floor($data_post/2629743);
if($count == 1)
$suffix = "mês atrás";
else
$suffix = "meses atrás";
break;
}
$sql = $conexao->prepare("INSERT INTO notificacoes_visualizacao (id_notificacao, id_usuario_que_visualizou) VALUES (:id_notificacao, :id_usuario_que_visualizou)");
$sql->bindParam(':id_notificacao', $row_notificacoes['notificacao_id'], PDO::PARAM_INT);
$sql->bindParam(':id_usuario_que_visualizou', $user_actual, PDO::PARAM_INT);
$sql->execute();
$return[] = '
<table border="0" class="barra_notificacoes" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" width="10%">
<div style="margin-left: 4px; margin-top: 5px;"><img width="50" height="50" src="'.$row_notificacoes['user_foto'].'"></div>
</td>
<td valign="top" width="50%">
<div style="margin-left: 5px; margin-top: 2px;"><a href="users/'.$row_notificacoes['slug'].'">'.$row_notificacoes['fb_nome'].'</a></div>
<div style="margin-left: 5px;" class="comentario_user">'.$row_notificacoes['notificacao'].'</div>
<div style="margin-left: 5px;" class="tempo_do_post">'.$count.' '.$suffix.'</div>
</td>
</tr>
</table>
';
}
echo json_encode($return);
?>
You will always display all notifications, including those that the follower has already received?
– Papa Charlie
yes like facebook it also lists all even those that have been read
– César Sousa
Do you say the full list of notifications or the notification in the top bar? The notification bar is when you click on it.
– Papa Charlie
See how Stack notifications work, they always appear, but lose 'new' or 'unread' status'.
– Papa Charlie
The full list of user notifications currently logged in
– César Sousa