PHP code example of craftpulse / craft-notifications
1. Go to this page and download the library: Download craftpulse/craft-notifications library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
/**
* Get the notification's delivery channels.
*
* @return array
*/
public function via()
{
$entry = $this->event->sender;
if ($entry->section->handle === 'blog' && $this->event->isNew) {
return [
'database' => Craft::$app->getUsers()->getUserByUsernameOrEmail('[email protected]'),
];
}
return [];
}
use percipioglobal\notifications\Notifications;
use app\notifications\BlogPostAdded;
Notifications::getInstance()->notificationsService->send(new BlogPostAdded());
public function toDatabase()
{
return ArrayHelper::toArray($this);
}
use percipioglobal\notifications\Notifications;
// All unread notifications
Notifications::getInstance()->notificationsService->getAllUnread();
// All read notifications
Notifications::getInstance()->notificationsService->getAllRead();
// All notifications
Notifications::getInstance()->notificationsService->getAll();
use percipioglobal\notifications\Notifications;
Notifications::getInstance()->notificationsService->markAsRead($notification);
public function toMail($notifiable)
{
$title = $this->event->sender->title;
$message = new Message();
$message->setTo($notifiable);
$message->setSubject("A new blogpost was added");
$message->setHtmlBody("
<p>Hey there!</p>
<p>A new blogpost was added with the title {$title}</p>
");
return $message;
}
return [
'slack' => '<YOUR_WEBHOOK_URL>',
];
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack()
{
return (new SlackMessage)
->content('A new blogpost was added!');
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)
->from('Ghost', ':ghost:')
->to('#other')
->content('This will be sent to #other');
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)
->from('Laravel')
->image('https://yoursite.com/favicon.png')
->content('This will display your logo next to the message');
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
$url = url('/exceptions/'.$this->exception->id);
return (new SlackMessage)
->error()
->content('Whoops! Something went wrong.')
->attachment(function ($attachment) use ($url) {
$attachment->title('Exception: File Not Found', $url)
->content('File [background.jpg] was not found.');
});
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
$url = url('/invoices/'.$this->invoice->id);
return (new SlackMessage)
->success()
->content('One of your invoices has been paid!')
->attachment(function ($attachment) use ($url) {
$attachment->title('Invoice 1322', $url)
->fields([
'Title' => 'Server Expenses',
'Amount' => '$1,234',
'Via' => 'American Express',
'Was Overdue' => ':-1:',
]);
});
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
$url = url('/exceptions/'.$this->exception->id);
return (new SlackMessage)
->error()
->content('Whoops! Something went wrong.')
->attachment(function ($attachment) use ($url) {
$attachment->title('Exception: File Not Found', $url)
->content('File [background.jpg] was *not found*.')
->markdown(['text']);
});
}
/**
* @var mixed The notifiable.
*/
public $notifiable;
/**
* @var NotificationsRecord The notification about to be sent.
*/
public $notification;
/**
* @var string The channel on which the notification is about to be sent.
*/
public $channel;
/**
* @var bool Whether we send the notification
*/
public $sendNotification = true;
/**
* @var mixed The response after sending the event
*/
public $response = null;
Event::on(
NotificationsService::class,
NotificationsService::EVENT_REGISTER_CHANNELS,
function (RegisterChannelsEvent $event) {
$event->channels[] = [
'voice' => function () {
return new VoiceChannel();
},
];
}
);
namespace app\channels;
use percipioglobal\notifications\models\Notification;
class VoiceChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \percipioglobal\notifications\models\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toVoice($notifiable);
// Send notification to the $notifiable...
}
}
namespace App\Notifications;
use app\channels\VoiceChannel;
use app\channels\messages\VoiceMessage;
use percipioglobal\notifications\models\Notification;
class InvoicePaid extends Notification
{
/**
* Get the notification channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return [
'voice' => '<YOUR_DESTINATION>',
];
}
/**
* Get the voice representation of the notification.
*
* @param mixed $notifiable
* @return VoiceMessage
*/
public function toVoice($notifiable)
{
// ...
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.