XF 2.1 Alert is not showing

grantus

Active member
My alert code is sending an alert to the appropriate user and it shows up with a "1" in the alert notification, but the alert is not showing anything. And when I go to the Alert page it doesn't show anything either. I'm assuming it's because the alert template is not named properly.

Here is my alert:

Code:
$receiver = \XF::finder('XF:User')->where('user_id', 24495)->fetchOne();

if ($receiver) {
   $sender = \XF::finder('XF:User')->where('user_id', 1)->fetchOne();
   $alertRepo = $this->repository('XF:UserAlert');
   $alertRepo->alert(
      $receiver,
      $sender->user_id,
      $sender->username,
      'newbattle',
      1111,
      'battle',
      ["value1" => 01, "value2" => 02, "status" => "active", 'depends_on_addon_id' => 'Battles/ILL']
   );
}

I have an Alert.php page under Battles\ILL\Alert with:
Code:
namespace Battles\ILL\Alert;
use XF\Alert\AbstractHandler;
use XF\Entity\UserAlert;

class NewBattle extends AbstractHandler {

    public function canViewAlert(UserAlert $alert, &$error = null): bool {
        return true;
    }

}

I've set up the content types like this:
Code:
content type: newbattle
field: entity
value: Battles\ILL:NewBattle
addon: Battles

content type: newbattle
field: alert_handler_class
value: Battles\ILL\Alert\NewBattle
addon: Battles

I see that the alert is inserted properly into the xf_user_alert table, so what should the alert template be named?
 
See \XF\Alert\AbstractHandler::getTemplateName and \XF\Alert\AbstractHandler::getPushTemplateName.

You can override these, but they are public:alert_{$contentType}_{$action} and public:push_{$contentType}_{$action} by default.
 
See \XF\Alert\AbstractHandler::getTemplateName and \XF\Alert\AbstractHandler::getPushTemplateName.

You can override these, but they are public:alert_{$contentType}_{$action} and public:push_{$contentType}_{$action} by default.
Sorry, I forgot to mention that I looked in AbstractHandler and I have my template as alert_newbattle_battle but it doesn't seem to work still. The content_type and action are inserted correctly.
 
I just tested it again by changing the content_type to 'user' and the action to 'newbattle', then named my template alert_user_newbattle and now I see the alert.

Why is that? Why would my content_type not work?

I even copied the exact code from XF\Alert\User and changed the class to NewBattle and it didn't work.
 
Beyond the content not being visible to the alerted user, I'm not sure. You might try dumping things from the alert page template to debug further.
 
Beyond the content not being visible to the alerted user, I'm not sure. You might try dumping things from the alert page template to debug further.
I dumped the alert page but it wasn't even showing the content_type, so it's confusing. I'm going to work with the user content_type since it seems to work no problem.
 
Back
Top Bottom