How to notify the person based on his post in laravel - php

I was able to create a Notification table using migration. Also I can save notification if a user(X) replies to the post of user(Y). Now, the problem is, I want to send the notification to user(Y), but then, the notifiable_id saves the id of user(X). How can user(Y) receive the notification then?
Auth::user()->notify(new PostNotif());
Example:
user(X) ID is : 12 and,
user(Y) ID is : 24
The notifications table looks like this:
type notifiable_id notifiable_type
App\Notifications\PostNotif 12 App\User
I expected that the notifiable_id should be the ID of user(Y) which is 24 and not 12.
Since, the user(Y) submitted a post and user(X) responded to that post, how can user(Y) be notified in this case?
EDIT
Also, I expect two IDs to be inserted into the database notification. One for the user who created the post and the other for the one who responded to the post.
Does anybody know about this?

I am using this table structure to send notification,
type Notification_id sender_id receiver_id
you can store userX in sender_id and userY in receiver_id, wise a versa.
I hope it works.

Related

stuck with notification system

I am developing a notification system in PHP, MySql, jquery ajax on a website that i am working on. I have a post which you follow if you comment on it. My problem is that i am stuck i don't know how can i send notification to all the users following the post. I did successfully send notification to the creator or the post though.
in the code i will be showing the query that enters a notification into notifications table, i will post the notifications_table structure and discussion_followers table which include the users following a post
here is the query that enters the notification into the notifications_table
// the board user_id is not the same as the logged in user which means the logged in user is not commenting on his post
// which means we insert notification in table
$query_not = "INSERT INTO notifications ( ";
$query_not .= "from_user, to_user, post_type, post_id, type, comment_id, notification_timestamp";
$query_not .= ") VALUES (";
$query_not .= " $user_id, $discussion_user_id_check, 'discussion', $discussion_id, 'comment', $fetched_comment_id, $discussion_comment_timestamp";
$query_not .= ")";
$result_not = mysqli_query($connection, $query_not);
so what i already achieved is : a user which is not the logged in user comments on a discussion post if he hasn't already followed the post he will will follow by entering his id with the post id into the following table, a new notification is inserted into the notifications_table. The user who created the post will receive this notification. What i want to achieve more is send notifications to all following this post that a new comment has been entered.
The notifications table structure is as follow:
notification_id, from_user, to_user, post_type, post_id, type, comment_id, reply_id, notification_timestamp, viewed
example of notification:
121, 20, 30, discussion, 31, comment, 39, 0, 1534184319, 0
discussions_followers table which follow the discussion post structure is :
follow_id, user_id, discussion_id
Honestly if i want to do what you want, i do not create my data structure like that to fill the purpose. but what ever, you can do a query:
select from_user from notifications where post_id={discussion_id} group by from_user;
then all ids fetched are users you want to push notification from new user. (you can skip $from_user==$user_id). lets say you have $result, then (simple code):
for($i=0, $i<count($result); $i++){
insert into notifications (from_user, to_user) values ($user_id, $result[$i]
['from_user'])
}
what i prefer:
for system like push notification. i always prefer to implement whole system as microservice and outside my project. (just you know not related). but what i do, i would use one of those message queue systems and not store such data in database FOR READ. your table is more like a log table which you can log you notification and write what ever you want to it for further process. but for read from it just to know who should push who, its not efficient. so have table post with 1 unique id and users id involved with that post. on each comment create queue with id "push-$id" and push comment and user id to it one by one. background manager of queues will know how to handle queues with id "push-*". creating queue may be a problem here so you can create empty one on each post created. (its not doing anything cause by that you just create pointer nothing else).

How to display push message in firefox when mysql table has a new record

I have a mysql table named notification with this structure
id user_id time name
I want to display a push message when this table has been updated if user_id is same width user id of that user has been logged in
but i don't know how to do this
I tried to do that with this tutorial https://wiki.mozilla.org/WebAPI/SimplePush but i failed
Is there any other way to play a sound and show the notification ? please help me about this problem

Private Messaging Notification - Database Table Design

I have have created a Private messaging system using PHP and mySQL with notification a bit like Facebook.
The database table has following fields( not all listed):
MessageID
senderUserID
RecUserID
Message
Subject
DateTime
Status - whether read or not
RepliedStatus - how should i use this?
DeleteRec - delete from inbox
DelSender - delete sender inbox
RepliedUserId - When user reply to orginal message this is change to receiver's id
All replies are stored in a second table, since each message create a thread. The second table looks a bit like this:
messageID - FK
repuserID
Mesage
DateTime
At the moment when a new message is sent out to a user i change the 'status' of the message to unread, From this can run a count query to list all of the unread messages in notification.
But if the users replies back to that message i cant set the original 'status' field to unread, since this will appear on both users notification. so i created another field called 'RepliedStatus ' but i am unsure how would i use this to show notification on message reply?
thanks guys.
If you have a replies table then you don't need a replied status column on your first status. By virtue of there existing a record in the replies table you know that a user has replied to a message
Why dont you add an INT and nullable column to the first table (let's say, "messages" table) named "previous_message"?
ALTER TABLE messages ADD COLUMN previous_message INT DEFAULT NULL;
So every message will have in the same table the previous one, and you can work out the sequence. If it helps you can have a "next_message" column with same definition and update the relevant record on reply.
Doing so you can use the status column on each reply.
If you want to keep the same DB organisation I would suggest to add a column status on the second table (let's say "replies").
Hope this helps
Me I'll put deleted column once and the same things for the read or not-read like:
[{"0":"both", "1":"Sender", "2":"receiver"}];
And then fetching a tread messaging like:
$sql = "SELECT * FROM messagetreads
WHERE (senderID OR receiverID = ".$_SESSION['MyCurrentId'].")
AND deleted !== 0
ORDER by TreadID AND DateTime ASC";
When a sender "delete" is tread... All tread relatedID in the database change for 1 or 0 if delete colomn is 2...
But I think it's better to creat another colomn for getting of the repeated deleted and notifications data like
TreadID (FK_message_Table)
delete (0=both deleted UserID=don't appear to this sender or receiver)
notify (0=both read UserID=read to this sender or receiver)
Then it's ezee to change a status or a delete statement.
The SELECT will be sometings like this:
$SQL = "SELECT *
FROM messagetreads
WHERE (senderID OR receiverID = ".$_SESSION['MyCurrentId'].")
IN (SELECT TreadID WHERE delete !== (0 OR ".$_SESSION['MyCurrentId']."))";
If is our member id involve in the colomn delete all the tread don't appear but if is the id of the receiver when the sender will delete 0 can be attributed so that both of the members can "delete" the message. Same thing for notifications!
very far later a cron-job can come delete very-old message read(0)...
PS: this can be notification system too like notifying for a new post in wall or comment on a photos or new events on calendar ... just add more information in column data and faormat it with php or java-ajaxed alike...

User messaging system

I'm looking at creating a user messaging system (similar to Facebook).
What I want to know is essentially what should the table structure look like? The requirements I have are as follows:
Messages are exchanged between users - a sender can select multiple users to send a message to.
Messages are displayed in a thread-style layout, as a 1-1 conversation. i.e. each recipient's reply will appear in it's own thread.
Individual messages cannot be deleted, however a thread can be deleted. Deleting a thread doesn't delete any messages, it just removes that thread from the user's inbox. However the other user can still access the thread if he/she hasn't deleted it from his/her inbox.
Here is what I have at the moment:
Table messages
==============
id (PK)
user_id (from)
subject
body
sent_at
Table message_recipients
========================
message_id (PK)
user_id (PK)
read_status
EDIT: What about the following:
Table messages
==============
id (PK)
thread_id
user_id (from)
body
sent_at
Table threads
=============
id (PK)
user_id (from)
subject
Table thread_recipients
=======================
thread_id (PK)
user_id (PK)
read_status
sender_deleted
recipient_deleted
I would suggest having the following at least:
Users, Threads, Messages
All messages would have a thread
foreign key: thread_id
All threads would have at least one message and at least one recipient (as well as sender)
foreign key: to_user_id, from_user_id, message_id
From there you could simply assign a couple flags to your thread (to_user_deleted, from_user_deleted) that would be updated accordingly.
There a lot more things to consider of course, such what kinds of things you want to account for. For example:
Do you want to display the current message as opposed to the starting message?
Do you want to allow users to mark individual messages as read, or just threads?
You need to take all of these into account while designing your database.
Why not use something like Jabber (example: OpenFire or Web Client)
If you need PHP to interact with it you could use something like:
http://code.google.com/p/xmpphp/ or http://code.google.com/p/jaxl/
#Angelo R. Would like to know why we require the Thread Table? This isn't any discussion board. If you want to retrieve the whole thread/conversation of messages you can simply query by source AND recipient ID.
Plus, if you use Thread, nothing bad. But what in this situation
If new message - new thread_id (automatically created), If replied to existing conversation, you have the thread_id, but what if you are creating a new message (say like facebook popup), you don't know whether there was any previous conversation or if thread_id is available or not unless you execute a special query to it.
This was my thought. Tell me if I might be wrong somewhere.

Read status of messages in Inbox

I am developing a messaging inbox system that uses a thread-based layout for messages. What I'm struggling with at the moment is updating the read status of the messages. Perhaps I have got the structure wrong so I could do with some guidance.
Here is what I have:
Table messages
==============
id
thread_id
from_user_id
subject
body
sent_date
Table message_threads
=====================
id
message_id
owner_id
member_id
owner_read
member_read
In the message_threads table, 'owner' refers to the user who started the thread, and 'member' refers to the other participant. A user's inbox will contain threads they have created themselves (owner), and also threads created by other users to which they have replied to (member).
The logic for setting the read status is as follows:
if owner sends a reply: 'member_read' field is set to 0 (unread), 'owner_read' field is set to 1 (read)
if member sends a reply: 'owner_read' field is set to 0 (unread), 'member_read' field is set to 1 (read)
Now I don't think this is entirely the correct approach, because suppose a member wants to sort messages in their inbox by read status - however a user can be both an owner and a member, and there are two read fields in the table. So this sort would not be correct.
Any suggestions on how I should go about this?
EDIT: Here is an exaple scenario:
User 1 sends Thread A to User 2
Thread A appears in User 2's inbox (status 0: unread)
User 2 opens Thread A
Thread A status set to 1 (read)
User 2 replies to Thread A
Thread A status in User 1's inbox (status 0: unread)
Thread A status in User 2's inbox (status 1: read)
User 1 opens Thread A
Thread A status set to 1 (read)
User 1 replies to Thread A
Thread A status in User 1's inbox (status 1: read)
Thread A status in User 2's inbox (status 0: unread)
The only "read" status is for messages that didn't originate from you. a reply to a recipient is marked read when opened. The sender doesn't care if it was read unless you want "receipt confirmation"
So - with that in mind here is what i think it should look like (i didn't include all fields in messages):
Table "users"
id
name
table "messages"
id (int)
parent_id (int)
from_user_id (id)
to_user_id
read (bit/bool)
message (text)
Let me know if I didn't understand you right.

Categories