News:

SMF 2.1.6 has been released! Take it for a spin! Read more.

Main Menu

Custom Alerts/Notifications

Started by FrizzleFried, March 08, 2022, 12:13:13 PM

Previous topic - Next topic

FrizzleFried

Is there any documentation available showing how to create custom alerts for the alert system?

For example,  I have a rates mod... i'd love to alert the users to the rating.  Same with a badges mod I have... I'd love to alert the user who got the badge to the fact... etc.

I did have an alerts mod from SMF Packs on my 2.0.x system and there was a way to generate custom alerts (Nibogo went out of his way to help too)... I was hoping there was a similar system with the stock SMF alerts system.

Diego Andrés

Asked the same some time ago
It has changed, but only a little bit, so it should still be very helpful

https://www.simplemachines.org/community/index.php?topic=568711.0

SMF Tricks - Free & Premium Responsive Themes for SMF.

K-Dog

I have generated a custom alert but I keep getting an error I can't seem to figure.

Here is my task class: <?php
/**
* Class Dogchat_Notify_Background
*/
class Dogchat_Notify_Background extends SMF_BackgroundTask
{
   
/**
    * This is the task dogchat alert
    * @return bool Always returns true
    */
   
public function execute()
   {
       global
$smcFunc, $sourcedir;

       
// Get the recipient's preferences
       
require_once($sourcedir . '/Subs-Notify.php');
       
$prefs = getNotifyPrefs($this->_details['receiver_id'], 'dogchat_pm', true);

       if (
$prefs[$this->_details['receiver_id']]['dogchat_pm'])
       {
           
$alert_row = array(
               
'alert_time' => $this->_details['time'],
               
'id_member' => $this->_details['receiver_id'],
               
'id_member_started' => $this->_details['id_member'],
               
'member_name' => $this->_details['member_name'],
               
'content_type' => 'msg',
               
'content_id' => $this->_details['id_member'],
               
'content_action' => 'dogchat_pm',
               
'is_read' => 0,
               
'extra' => $smcFunc['json_encode'](
                   array(
                       
'text' => "Hello World!",
                   )
               ),
           );

           
$smcFunc['db_insert']('insert', '{db_prefix}user_alerts',
               array(
                   
'alert_time' => 'int',
                   
'id_member' => 'int',
                   
'id_member_started' => 'int',
                   
'member_name' => 'string',
                   
'content_type' => 'string',
                   
'content_id' => 'int',
                   
'content_action' => 'string',
                   
'is_read' => 'int',
                   
'extra' => 'string'
               
),
               
$alert_row,
               array()
           );

           
updateMemberData($this->_details['receiver_id'], array('alerts' => '+'));
       }

       return
true;
   }
}

And here is a test alert I generate.
        $taskData = array(
            'receiver_id' => 1,
            'id_member' => 24,
            'member_name' => 'Fantasy Girl',
            'time' => time()
        );
        $query = $smcFunc['db_query']('', "
            INSERT INTO {db_prefix}background_tasks
            (task_file, task_class, task_data, claimed_time)
            VALUES
            ({string:task_file}, {string:task_class}, {string:task_data}, {int:claimed_time})",
            array(
                'task_file' => '$sourcedir/tasks/Dogchat_Notify_Background.php',
                'task_class' => 'Dogchat_Notify_Background',
                'task_data' => $smcFunc['json_encode']($taskData),
                'claimed_time' => 0
            )
        );

This is the error;

Type of error
Undefined_vars
Error message
2: Undefined array key "text"
File
/var/www/html/homedog/doomstead/Themes/doomstead/Profile.template.php
Line
116
URL of page causing the error
hxxp:localhost/homedog/doomstead/index.php?action=profile;area=alerts_popup;u=1;ajax [nonactive]
Backtrace information

    #0: smf_error_handler()
    Called from /var/www/html/homedog/doomstead/Themes/doomstead/Profile.template.php on line 116
    #1: template_alerts_popup()
    Called from /var/www/html/homedog/doomstead/Sources/Load.php on line 2778
    #2: loadSubTemplate()
    Called from /var/www/html/homedog/doomstead/Sources/Subs.php on line 4162
    #3: obExit()
    Called from /var/www/html/homedog/doomstead/index.php on line 187






the root of my forum is doomstead and I have my own doomstead theme. on line 116

<span class="alert_text">', $details['text'], '</span> - <span class="alert_time">', $details['time'], '</span>

$details['text'] needs a value and I believe the class for my alert should set it.


Any ideas how?

Arantor

Have you defined $txt['msg_dogchat_pm'] anywhere? This is the string that will be looked for in terms of constructing the alert body - content_type plus content_action.
Holder of controversial views, all of which my own.


K-Dog

Quote from: Arantor on December 24, 2023, 05:47:01 PMHave you defined $txt['msg_dogchat_pm'] anywhere? This is the string that will be looked for in terms of constructing the alert body - content_type plus content_action.

Thanks, that helped me find what to do.  I needed 'alert_' in front of 'msg_' , then my error went away.
$txt['alert_msg_dogchat_pm']

Arantor

Ah, yes, I forgot about the prefix, but glad you got it sorted.
Holder of controversial views, all of which my own.


Advertisement: