Advertisement:

Author Topic: [2.1] Any bits on how to use the Alerts  (Read 854 times)

Online Diego Andrés

  • Customizer
  • SMF Hero
  • *
  • Posts: 3,220
  • Gender: Male
    • DiegoSetroc on GitHub
    • @bihgetter on Twitter
    • SMF Tricks - Free & Premium Themes
[2.1] Any bits on how to use the Alerts
« on: July 14, 2019, 12:30:49 AM »
Is there any information available about adding custom alerts?
Nothing fancy just the basics  ;D

SMF Tricks - Free & Premium Responsive Themes for SMF.

Offline SychO

  • Lead Customizer
  • SMF Hero
  • *
  • Posts: 1,411
  • Gender: Male
    • SychO9 on GitHub
    • SychO
Re: [2.1] Any bits on how to use the Alerts
« Reply #1 on: July 14, 2019, 04:04:23 AM »
Well, for starters if you're gonna be adding a custom alert, you should add an option for users to be able to disable/enable it in the profile notifications area, you can do that with the integrate_alert_types hook which is called from Profile-Modify.php line 2080

As for sending out the alert, the proper way of doing it is to create a background task like so
Code: [Select]
<?php
// Prepare to send out notifications
$smcFunc['db_insert']('insert',
'{db_prefix}background_tasks',
array('task_file' => 'string''task_class' => 'string''task_data' => 'string''claimed_time' => 'int'),
array('$sourcedir/tasks/BackgroundTaskName-Notify.php''BackgroundTaskName_Notify_Background'$smcFunc['json_encode'](array(
'sender_id' => $context['user']['id'],
'sender_name' => $context['user']['name'],
'time' => time(),
...
)), 0),
array('id_task')
);

After that you go ahead and create your class located under $sourcedir/tasks, this is where the alerts and/or emails will be sent/inserted.
Note that you will be able to access the json formatted array through the background tasks's $_details attribute.

The class has an execute() method where all the work will be done.

Code: [Select]
<?php

class BackgroundTaskName_Notify_Background extends SMF_BackgroundTask
{
/**
 * This executes the task - loads up the information, puts the email in the queue and inserts alerts as needed.
 * @return bool Always returns true.
 */
public function execute()
{
...

return true;
}
}

You will have to query/get all the members that are supposed to receive this alert, and then you will want to check their preferences and if they want to receive an alert/email

Code: [Select]
<?php

class BackgroundTaskName_Notify_Background extends SMF_BackgroundTask
{
/**
 * This executes the task - loads up the information, puts the email in the queue and inserts alerts as needed.
 * @return bool Always returns true.
 */
public function execute()
{
// Query/get members here
$members = ...

// Result is an array of member ids in $members (as an example)
// You probably want to remove the sender from the array
$members array_diff($members, array($this->_details['sender_id']));

// Preferences
require_once($sourcedir '/Subs-Notify.php');
$prefs getNotifyPrefs($members'YOUR_ALERT_CUSTOM_NAME'true);

$alert_bits = array(
'alert' => self::RECEIVE_NOTIFY_ALERT,
'email' => self::RECEIVE_NOTIFY_EMAIL,
);
$notifies = array();

foreach ($prefs as $member => $pref_option)
{
foreach ($alert_bits as $type => $bitvalue)
if ($pref_option['msg_report'] & $bitvalue)
$notifies[$type][] = $member;
}

// Those receiving alerts
if (!empty($notifies['alert']))
{
// Alerts are relatively easy.
$insert_rows = array();
foreach ($notifies['alert'] as $member)
{
$insert_rows[] = array(
...
);
}

$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'),
$insert_rows,
array('id_alert')
);

// Update their alert count
updateMemberData($notifies['alert'], array('alerts' => '+'));
}

// Those receiving emails
if (!empty($notifies['email']))
{
...
}

return true;
}
}

Note that updateMemberData() has got to be called after inserting the new alerts, it MUST NOT be before.

You should take a look at any of the tasks located under $sourcedir/tasks they are great examples :), I've taken some of the code above from MsgReport-Notify.php

Imazighen

Online Diego Andrés

  • Customizer
  • SMF Hero
  • *
  • Posts: 3,220
  • Gender: Male
    • DiegoSetroc on GitHub
    • @bihgetter on Twitter
    • SMF Tricks - Free & Premium Themes
Re: [2.1] Any bits on how to use the Alerts
« Reply #2 on: July 14, 2019, 04:39:37 AM »
Very useful, thank you.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Offline vbgamer45

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 21,398
    • smfhacks on Facebook
    • VBGAMER45 on GitHub
    • @createaforum on Twitter
    • SMF For Free
Re: [2.1] Any bits on how to use the Alerts
« Reply #3 on: July 14, 2019, 10:48:49 AM »
Very helpful thanks!
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Online Diego Andrés

  • Customizer
  • SMF Hero
  • *
  • Posts: 3,220
  • Gender: Male
    • DiegoSetroc on GitHub
    • @bihgetter on Twitter
    • SMF Tricks - Free & Premium Themes
Re: [2.1] Any bits on how to use the Alerts
« Reply #4 on: July 14, 2019, 07:07:16 PM »
Having a look at that MsgReport-Notify.php as you mentioned.
I've got my alert working. However, for that particular alert (MsgReport-Notify) you guys seem to be cheating as the txt string has a different text on 2 different language files.

Is there a parameter we could use to set a different language string so I can have both strings on the same language file?
I'm referring to the setting txt string for enabling/disabling the alert in profile and the one that actually appears in the alert.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Online Diego Andrés

  • Customizer
  • SMF Hero
  • *
  • Posts: 3,220
  • Gender: Male
    • DiegoSetroc on GitHub
    • @bihgetter on Twitter
    • SMF Tricks - Free & Premium Themes
Re: [2.1] Any bits on how to use the Alerts
« Reply #5 on: July 14, 2019, 07:45:45 PM »
So I can use content_type for the actual alert. I got that working.
Thanks

SMF Tricks - Free & Premium Responsive Themes for SMF.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,224
    • StoryBB/StoryBB on GitHub
Re: [2.1] Any bits on how to use the Alerts
« Reply #6 on: July 18, 2019, 01:44:14 AM »
You don’t technically *need* a background task. If you’re doing something where you’re not checking the user’s preference and realistically only sending one at a time, just insert into the alerts table and call updateMemberData to bump the alerts count.

The background task is only to push it outside of the code where you’re doing the work so it can return to the user faster - in 2.0 if people have notifications set up on a topic “instantly”, that gets done immediately on posting which is why on some sites it can take a while to return to topic once done. In 2.1 this whole thing can be safely punted out to the background tasks and the main process can return to the user immediately.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Online Diego Andrés

  • Customizer
  • SMF Hero
  • *
  • Posts: 3,220
  • Gender: Male
    • DiegoSetroc on GitHub
    • @bihgetter on Twitter
    • SMF Tricks - Free & Premium Themes
Re: [2.1] Any bits on how to use the Alerts
« Reply #7 on: July 18, 2019, 05:18:29 PM »
So if it's a 1v1 alert I could skip the background task?

SMF Tricks - Free & Premium Responsive Themes for SMF.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,224
    • StoryBB/StoryBB on GitHub
Re: [2.1] Any bits on how to use the Alerts
« Reply #8 on: July 18, 2019, 05:24:26 PM »
You could, provided you're willing to accept the hit of working out if the user should get an alert in the same page load. The whole deal of background tasks is to move the processing to where the normal users won't see it so it won't slow down page loads for them.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.