How to send notifications to users (using OssnNotifications class)

Rishi B Posted in Component Development 6 years ago

Hey everyone, I'm working on a custom component and am having trouble figuring out exactly how to dispatch a notification to a user. I was hoping there was some simple function I could call that took care of everything. Can I use OssnNotifications->add() in this way, or do I need to first setup some kind of hooks and/or callback functions? Thanks

Replies
German Michael Zülsdorff Replied 5 years ago

Excuse me Bansh33 for flooding your thread with stuff that actually isn't related in any way to your topic.

And yes, I'd strongly recommend Villianer to start a new thread as "How to add a notification preview" and I'll move my stuff over there.

German Michael Zülsdorff Replied 5 years ago

Yes, of course ... ;)
So let's try to get into it step by step.
As Bansh33 already mentioned the necessary changes have to be applied to the function ossn_likes_post_notifiation() which is part of ossn_com.php of the OssnWall component.
And as you already investigated correctly the preview needs to be added to the data class. Fine.

Having a look into the code this data class appears at line 178 at the bottom of the funtion like

return "<a href='{$notification_read}'>
       <li {$viewed}> {$img} 
       <div class='notfi-meta'> {$type}
       <div class='data'>" . ossn_print("ossn:notifications:{$notif->type}", array(
            $user->fullname
    )) . '</div>
       </div></li></a>';

So my idea would be to add some static hmtl to that class first in order to get an idea which way things would look like ... as css may be tricky sometimes. Thus in hope to receive a new line in the notification drop-down I would add another div like

return "<a href='{$notification_read}'>
       <li {$viewed}> {$img} 
       <div class='notfi-meta'> {$type}
       <div class='data'>" . ossn_print("ossn:notifications:{$notif->type}", array(
            $user->fullname
    )) . '</div><div>Post preview goes here ...</div>
       </div></li></a>';

But stop, and before applying any changes: Make sure you have a backup in place and you have enabled Error reporting from your Ossn Admin panel. Even more make sure that error reporting is working correctly by adding a line like

error_log('this is line number 175');

before the return ... and checking your error_log file.

Good luck.

us Rishi B Replied 5 years ago

as far as I can tell, ossn-notifications-unviewed is a css class and not really relevant to this discussion.

I think what you wanna do is look at the function OssnNotifications->getByGUID() in components/OssnNotifications/classes/OssnNotifications.php (php class != css class lol). That function will retrieve all data associated with a particular notification (by guid). Parse that information and extract whatever you want, then add the appropriate html to the functions that I explained were part of the hook defintions (look at my post with all the ossn_add_hook() lines). Or, if you want to get to know things a little better on your own, follow Z-Man's advice and use Chrome's developer tools to inspect the elements in the notification drop down, then search the ossn codebase to find the appropriate string and figure out where you need to modify the html that creates those notifications in the drop down menu.

us Villianer Velasquez Replied 5 years ago

basically i want to show a preview of
$url = ossnsiteurl("post/view/{$notif->subject_guid}");

in the notifications drop down

us Villianer Velasquez Replied 5 years ago

ossn-notification-unviewed

us Villianer Velasquez Replied 5 years ago

<li class="ossn-notification-unviewed">

Thats my wild guess :-)

i would want to add to the data class puting a part of the post that is being liked and or commented

German Michael Zülsdorff Replied 5 years ago

Okay,
assuming there's a post on your wall, someone liked that post and you have read the instructions in https://www.opensource-socialnetwork.org/wiki/view/1137/how-to-find-something-a-word-pattern-in-the-source-code :

What's the HTML anchor of interest you want to search for?

us Villianer Velasquez Replied 5 years ago

i dont know why im having such an issue understanding the logic here. Anyone able to get me started?

us Rishi B Replied 5 years ago

oh... hmmm... I don't see why not. You'd need to modify the functions that define how those notifications look in that drop down. These functions are specified in the ossn_com.php file for the OssnWall component (components/OssnWall/ossn_com.php). These are the relevant hooks, so you'd need to modify the functions that they specify:

ossn_add_hook('notification:view', 'like:post', 'ossn_likes_post_notifiation');
ossn_add_hook('notification:view', 'comments:post', 'ossn_likes_post_notifiation');
ossn_add_hook('notification:view', 'wall:friends:tag', 'ossn_likes_post_notifiation');
ossn_add_hook('notification:view', 'comments:post:group:wall', 'ossn_group_comment_post');
ossn_add_hook('notification:view', 'like:post:group:wall', 'ossn_group_comment_post');

so for example, the first function you'd need to modify is ossn_likes_post_notifiation($hook, $type, $return, $params) (it's in the same file, as you'll see, that's where it spits out the relevant html to create the content for the notifications in the dropdown menu). Hope that helps.

us Villianer Velasquez Replied 5 years ago

What I was meaning to say was like on most social networks their notifications say what posts was liked or commented on instead of saying someone liked your comment on a post. Or out says sunbathing like someone commented on your post. Is there a way to add a preview of what post without clicking on it?