简体   繁体   中英

In creating notifications/alerts, how to know when to mark the notification as read

I'm creating a website with PHP and MySQL, and currently working on a page that deals with notifications (eg When someone comments on a post by you, you get a notification to that effect). The notification is sent not as an email, but as an alert in the website when you login to your account (Facebook style).

Now my problem is in determining when to mark a notification as read. In short: what is that event (such as a click, a view) that after it happens you can be sure the notification has been read, and you can change it to read?

but I already got a way around it. Let me just share may be it might help someone: Every alert ought to direct the user to a certain URL eg if someone commented on your post, on clicking the alert you should be directed to eg "www.example.com/posts/?post_id=5". I cal this the target URL. when generating the alert, i store its target URL together with the alert details in the alerts table, and get the AI unique field(alert_id), which i will use to generate the href part of any link on the alert eg <a href="changetoread.php?alert_id=56">G Thuo</a> commented on your post. this is my approach: on clicking any alert, it first takes you somewhere where its status is changed to read(basing on the alert_id in the link). After it's changed to read, then we fetch the target URL from the table and redirect the user there. here's my code:

$alert_id=$_GET['alert_id'];
$user_id=$_SESSION['user_id'];
//create the SQL to update the read_status
$sql="UPDATE alerts_log SET read_status=1 WHERE alert_id=$alert_id AND user_id=$user_id";
$res=mysql_query($sql) or die(mysql_error());
//we can now extract the targer_url from the alerts table and redirect the user automatically to that URL
$sql="SELECT target_url FROM alerts WHERE alert_id=$alert_id";
$res=mysql_query($sql) or die(mysql_error());
$row=mysql_fetch_assoc($res);
$url=$row['target_url'];
//redirect the user to that URL
header("Location: $url");

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM