我想根據 laravel 中的頁面瀏覽量向帖子創建者發送通知。就像帖子獲得 1000 次瀏覽一樣,創建者應該會收到祝賀通知。這里的問題是我正在使用 cookie(2 小時)來防止多個頁面查看。因此創建者會收到多個通知,直到視圖移至 1001。
if($views == 1000){
$user = User::find($userid);
$notificationdetails = [
'subject' => 'Congratulations, You got your first '.$views. ' views on your new snippet - '.$snippet_title,
'greeting' => 'Hi, '.$user->name,
'body' => 'Congratulations, You got your first '.$views. ' views on your new
snippet - '.$snippet_title,
'body1' =>'',
'thanks' => ' ',
'actionText' => 'View snippet',
'actionURL' => url(env('FRONTEND_URL').'/snippets/'.$slug)
];
Notification::send($user, new BBBnotifications($notificationdetails));
}
uj5u.com熱心網友回復:
您可以在資料庫中創建一個表并檢查通知是否已發送或已讀取。首先,您應該創建一個模型和相關表。
if($views == 1000){
$user = User::find($userid);
$notification_check = NotificationHistory::where('user_id', $user->id)->where('notification_type', 1000)->first();
if($notification_check === null)
{
$notificationdetails = [
'subject' => 'Congratulations, You got your first '.$views. ' views on your new snippet - '.$snippet_title,
'greeting' => 'Hi, '.$user->name,
'body' => 'Congratulations, You got your first '.$views. ' views on your new
snippet - '.$snippet_title,
'body1' =>'',
'thanks' => ' ',
'actionText' => 'View snippet',
'actionURL' => url(env('FRONTEND_URL').'/snippets/'.$slug),
'type' => 1000
];
Notification::send($user, new BBBnotifications($notificationdetails));
NotificationHistory::create([
'notification_type' => 1000,
'user_id' => $user->id, //or auth()->user()->id
'read_at' => now()
]);
}
}
uj5u.com熱心網友回復:
您必須在更新視圖時實作您的邏輯,而不是在獲取計數時。你可以按照這種方法。
$cookieName = "post_".$postId; // using postId to determine each post
//check if the cookie exists
if(!isset($_COOKIE[$cookieName])) {
//increase view count on post table of `$postId`
setcookie($cookieName, true, time() (3600 * 2));
}
您可以保留當前的通知發送邏輯(如果它正在作業),因為現在只有在沒有設定 cookie 時才會更新查看次數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514639.html
