Add notification to user produces a notification to admin user (or user with id 1)

Rafael [redcrested.net] Posted in Component Development 2 years ago

Hello

In my custom component, I'm trying to create a notification message. After a few mistakes, and after deleting 18000 records into the database, I started a single component to understand the notification system. I followed this post (https://www.opensource-socialnetwork.org/discussion/view/2967/how-to-send-notifications-to-users-using-ossnnotifications-class) as starting point.

In this sample, the idea is to send a notification alert to logged user and click in the message open user profile. The component makes all these steps. But in this process, the admin user (id=1) receives a similar notification. Looking into the database I found 3 records in ossnnotitication with the same timecreated field.

The component is in GitHub ( https://github.com/rafaelmamorim/OSSN-Notification-Sample ) and I would be glad if someone can help me to fix this issue.

Regards!

component NotificationSample

Replies
Breton Rafael [redcrested.net] Replied 2 years ago

Arsalan

Thank you for the tips. The component now works correctly. I'll update it on GitHub and then make it available in the components section if that's possible.

I put the '111' because when I started component creation I didn't know which parameters would be stored in the database. And later, I forgot to remove it.

About the dummy user, I put 1 into tests because user with guid 4 runnning the component. But in the next version of component on GitHub, I will put a define variable at the beginning of the file. And an explication message.

Regards!

Indonesian Arsalan Shah Replied 2 years ago

The problem seems in your line

$auxNotifications = $notifications->add('notification:message:tomyself',strval(ossn_loggedin_user()->guid),'1',$auxAnnotation,'111');

If we compare it with actual method

add($type, $poster_guid, $subject_guid, $item_guid = NULL, $notification_owner = '') 

You are specifying 111 as notification owner and also the second paramter as strval however it should be integer so (int)ossnloggedinuser()->guid.

The function should look like

        $who_sent_notification_guid = 234;
        $who_will_receive_notification_guid = ossn_loggedin_user()->guid;

        $subject_guid = $auxAnnotation;
        $item_guid = NULL;

        $auxNotifications = $notifications->add('notification:message:tomyself', $who_sent_notification_guid, $auxAnnotation, $item_guid,  $who_will_receive_notification_guid);

Now the problem is that you can not make whosentnotificationguid and whowillreceivenotification_guid same. As per https://github.com/opensource-socialnetwork/opensource-socialnetwork/blob/v6.x/components/OssnNotifications/classes/OssnNotifications.php#L66

So what you need to do is to first create a system dummy user lets suppose 'Support ABC', and let suppose system generates it GUID as 234. Now when notification is added user will see the notification is from system user.

Notification can not be sent to users own id both sender and receiver must be different. So you need to create a dummy user.