Custom Alerts Not Showing Up

mjda

Well-known member
I've created an addon that inserts, what I thought was, the appropriate data into the xf_user_alerts table. It will also increase the alerts_unread in the xf_user table for the specified user. However, when I hover the Alerts tab it continues to say "You have no new alerts.". When I look into the database it shows the view_date in the xf_user_alerts table to be updated, but obviously this just isn't true because I can't view the alert at all. I'm assuming it might just be that I'm missing a template, but I'm not exactly sure? If it is just a template I'm missing, how would I know what the name of the template should be? If it's not a missing template, any ideas on what the problem could be?
 
The process for this to work correctly is:
  1. A custom content type added to the xf_content_type and xf_content_type_field tables
  2. A custom AlertHandler class (you can check out the code for the default AlertHandlers, e.g. located in library/XenForo/AlertHandler/)
  3. Send the Alert using the correct static function (XenForo_Model_Alert::alert)
  4. Create a template which will take the format of alert_{content_type}_{action} e.g. alert_post_like is the correct template name if your content_type is named "post" and the action you've passed to the alert function is "like"
Without the above, it will not work properly. To be clear: It isn't just as simple as running some queries to insert the data directly. That just won't work, unfortunately :)
 
Without the above, it will not work properly. To be clear: It isn't just as simple as running some queries to insert the data directly. That just won't work, unfortunately :)

Thanks for the info, Chris. I'll give it another go. Hopefully I'll be able to get it working this time around.
 
I'm sure you will.

Follow the code that's already in the XenForo core. It's the best way to learn how it all joins together.
 
Well, unsurprisingly, I'm still not able to get this to work.

I have added the content type to the xf_content_type and xf_content_type_field tables.

I have the following AlertHandler.

PHP:
  public function getContentByIds(array $contentIds, $model, $userId, array $viewingUser)
   {
     $myDataModel = $model->getModelFromCache('MyData_Model_MyData');
   
     return $myDataModel->getMyDataByIds($userId, $contentIds);
   }
 
   public function _prepareCreate(array $item)
   {
     if ($item['extra_data'])
     {
       $item['extra'] = unserialize($item['extra_data']);
     }
     unset($item['extra_data']);

     return $item;
   }

Here is my model function.

PHP:
  public function getMyDataByIds($userId, array $myDataIds)
   {
     if(!$myDataIds)
     {
       return array();
     }
   
     return $this->fetchAllKeyed('
       SELECT *
       FROM mydata
       WHERE mydata_id IN (' . $this->_getDb()->quote($myDataIds) . ')
     ','mydata_id');
   }

Here is the line that I am using to create the alert, using the function that you specified. I should note that the alert is being added to the database and the alerts_unread count is being increased.

PHP:
XenForo_Model_Alert::alert($partner['user_id'],$visitor['user_id'],$visitor['username'],'mydata',$myDataId,'create',NULL);

Finally, I have a template called alert_mydata_create that simply reads "If you see this, it means the alert is working.".

I'm still having the same problem as before, however. The alert count "balloon" is showing, and increasing, properly but there still aren't any alerts shown in the drop down.
 
The code looks correct.

After inserting your custom content type into xf_content_type you may need to trigger a cache rebuild before this will work.

Also, I see you're referring to the content type in your code as "mydata", which is fine. But that must match the name of the content type in xf_content_type

So in xf_content_type you should have something like:

content_type: mydata
addon_id: MyAddOn
fields: (can be NULL, this gets rebuilt by the cache rebuild)

And in xf_content_type_field:

content_type: mydata
field_name: alert_handler_class
field_value: MyAddOn_AlertHandler_MyData (this must match the class of your AlertHandler file)

Once you've got that in there, export your add-on, and perform an upgrade on it (or do something else that triggers a full cache rebuild such as install an add-on or similar) and hopefully that might work.

Failing all that, feel free to package the add-on up and post it here (or send it to me) and I will see if I can see anything wrong - the code you've posted so far seems fine to me.
 
Top Bottom