Notifications

The notifications in Codoforum is highly extensible and allows you to manage and send your own alerts/notifications.

In this tutorial you will learn how to write your own custom notification.

Creating your notification

Components

Before we create our notification, we will see the componenets that make up a notification.

In below image:

forum notification annotation


notification label:
This is the name or type of your notification.
For example. 'New topic', 'New message', etc

actor avatar:
The user avatar of the actor which becomes the icon/image for the notification.

notification body:
The actual content of the notification.

time:
The time when notification was created.

Data

From looking at above components, we can figure out the data that is required for creating a notification.

Lets take an example of notification of user upvoting a post .

$data = array(
   'label' => 'New upvote',
   'cid' => CATEGORY_ID,
   'tid' => TOPIC_ID,
   'pid' => POST_ID,
   'notification' => '%actor% upvoted your post in %title%',
   'bindings' => array('title' => TOPIC_TITLE)
);
$notifier = new \CODOF\Forum\Notification\Notifier();
$notifier->queueNotify('ofTopic', $data);

Above will create a notification and insert it into the database.

If you see the above code, the notification body is created from the two keys 'notification' & 'bindings' of the array $data.

If you observe, the binding for %actor% is not passed, that is because it is internally defined as the current username by codoforum.

Let us see some more examples:

array(
    'notification' => '%actor% likes your post.'
); //no bindings defined since not required    

array(
    'notification' => '%actor% moved your topic to %category%',
    'bindings' => array('category' => CATEGORY_NAME)
);

Now, you may have some questions:

  • What happens when someone clicks on the notification ?
  • How will I know who will be notified for above notification ?
  • What about translation for different languages ?

What happens when someone clicks on the notification ?

When someone clicks on the notification the user is taken to the location whose url is built by Codoforum. The url built depends on the data that you passed while creating the notification.

The data used for building url is this:

   array(
       'tid' => TOPIC_ID, //the topic id
       'pid' => POST_ID //the post id
   );

Codoforum will build an url to the topic id mentioned in above data.

How will I know who will be notified for above notification ?

While creating the notification, you can mention the type of subscription.

For example:

Notifying users following a particular cateogry

$notifier->queueNotify('ofCategory', $data);

Notifying users following a particular topic

$notifier->queueNotify('ofTopic', $data);

Note: Above will also notify users following category of that topic

NOTE: A user following a particular category or topic means that they have set the notification level for that category or topic as 2 or 3.

What about translation for different languages ?

You will have to provide translations in your plugin according to the data passed while creating our notification.

In our example, the 'notification label' was New upvote and 'action' was upvoted We will have to provide translations for above two words.

Please refer Adding translations if you want to learn how to add translations for your plugin.

© CODOFORUM 2023. All Rights Reserved.