This package gives you all the basic elements you need to send notifications to notifiable models via SimplePush.
Install the package using composer:
composer require barnsleyhq/laravel-simplepush-notifications-channel
Once installed, all you need to do is setup your notifications to send to the SimplePush channel:
<?php
use BarnsleyHQ\SimplePush\Models\Actions\GetAction;
use BarnsleyHQ\SimplePush\Models\Actions\GetActions;
use BarnsleyHQ\SimplePush\Models\Attachments\VideoAttachment;
use BarnsleyHQ\SimplePush\Models\SimplePushMessage;
...
class CustomAlert
{
...
public function via($notifiable)
{
$channels = [];
...
$channels[] = 'simplepush';
return $channels;
}
public function toSimplePush($notifiable): SimplePushMessage
{
return (new SimplePushMessage)
->token($notifiable->tokens->simplepush) // Change this line to get the token
->title('Custom Alert')
->content('You have a new alert!')
->event('Custom Event')
->actions(GetActions::make([
GetAction::make('Pause for 1 hour', 'https://webhooks.test.com/pause?hours=1'),
GetAction::make('Pause for 24 hours', 'https://webhooks.test.com/pause?hours=24'),
]))
->attachments(VideoAttachment::make('https://my-url.com/thumbnail.png', 'https://my-url.com/video.mp4'));
}
...
}
The token to be used when sending the notification.
Example:
$message = (new SimplePushMessage())
->token('test-token');
$message = new SimplePushMessage();
$message->token('test-token');
The message content to be included in the notification.
Example:
$message = (new SimplePushMessage())
->content('This is a Test Alert');
$message = new SimplePushMessage();
$message->content('This is a Test Alert');
The title of the notification.
Example:
$message = (new SimplePushMessage())
->title('Test Alert');
$message = new SimplePushMessage();
$message->title('Test Alert');
An event to trigger once the notification is sent.
Example:
$message = (new SimplePushMessage())
->event('test-event');
$message = new SimplePushMessage();
$message->event('test-event');
Actions to be sent along with the event.
Feedback Actions Example:
$message = (new SimplePushMessage())
->actions(FeedbackActions::make('Pause events for 1 hour'));
$actions = FeedbackActions::make([
'Pause events for 1 hour',
'Pause events for 2 hours',
]);
$actions->add('Pause events for 3 hours');
$message = new SimplePushMessage();
$message->actions($actions);
GET Actions Example:
$message = (new SimplePushMessage())
->actions(GetActions::make(GetAction::make('Pause events for 1 hour', 'https://my-url.com/pause?hours=1')));
$actions = GetActions::make([
GetAction::make('Pause events for 1 hour', 'https://my-url.com/pause?hours=1'),
GetAction::make('Pause events for 2 hours', 'https://my-url.com/pause?hours=2'),
]);
$actions->add(GetAction::make('Pause events for 3 hours', 'https://my-url.com/pause?hours=3'));
$message = new SimplePushMessage();
$message->actions($actions);
Create new instance of FeedbackActions with an initial action.
Example:
$actions = FeedbackActions::make('Action 1');
$actions = FeedbackActions::make('Action 1', fn ($feedbackId) => saveFeedbackId($feedbackId));
Check message for feedback response.
Example:
FeedbackActions::getFeedbackResponseForId('5e885b1d33c547bbac78bda8cdaf7be7');
Add another action to an existing instance of FeedbackActions.
Example:
$actions = FeedbackActions::make('Action 1')
->add('Action 2');
$actions = new FeedbackActions();
$actions->add([
'Action 1',
'Action 2',
]);
Update callback to retrieve Feedback ID for an existing instance of FeedbackActions.
Example:
$actions = FeedbackActions::make('Action 1')
->sendCallback(fn ($feedbackId) => saveFeedbackId($feedbackId));
$actions = new FeedbackActions();
$actions->sendCallback(function ($feedbackId) {
$this->saveFeedbackId($feedbackId);
});
Return FeedbackAction options as an array.
Example:
$actions = FeedbackActions::make('Action 1')
->toArray();
$actions = new FeedbackActions();
$actions->toArray();
Create new instance of GetActions with an initial action.
Example:
$actions = GetActions::make(GetAction::make('Action 1', 'https://my-url.com/action'));
Add another action to an existing instance of GetActions.
Example:
$actions = GetActions::make(GetAction::make('Action 1', 'https://my-url.com/action'))
->add(GetAction::make('Action 2', 'https://my-url.com/action-2'));
$actions = new GetActions();
$actions->add([
GetAction::make('Action 1', 'https://my-url.com/action'),
GetAction::make('Action 2', 'https://my-url.com/action-2'),
]);
Add another action, with just the required values, to an existing instance of GetActions.
Example:
$actions = GetActions::make()
->addAction('Action 1', 'https://my-url.com/action');
$actions->addAction('Action 1', 'https://my-url.com/action');
Return GetActions options as an array.
Example:
$actions = GetActions::make(GetAction::make('Action 1', 'https://my-url.com/action'))
->toArray();
$actions = new GetActions();
$actions->toArray();
Create new instance of GetAction with initial values.
Example:
$actions = GetAction::make('Action 1', 'https://my-url.com/action');
Set the name of the action.
Example:
$actions = GetAction::make('Action 1', 'https://my-url.com/action')
->setName('Renamed Action')
->setUrl('https://my-url.com/renamed-action');
$actions = new GetAction();
$actions->setName('Action 1')
->setUrl('https://my-url.com/action');
Set the URL for the action.
Example:
$actions = GetAction::make()
->addAction('Action 1', 'https://my-url.com/action');
$actions->addAction('Action 1', 'https://my-url.com/action');
Return GetAction options as an array.
Example:
$actions = GetAction::make('Action 1', 'https://my-url.com/action')
->setName('Renamed Action')
->setUrl('https://my-url.com/renamed-action')
->toArray();
$actions = new GetAction();
$actions->setName('Action 1')
->setUrl('https://my-url.com/action')
->toArray();
Allowed Extensions
- jpg
- png
- gif
- mp4
Create new instance of GenericAttachments with an initial attachment.
Example:
$attachments = GenericAttachments::make('https://test.com/image.png');
Add another action to an existing instance of GenericAttachments.
Example:
$attachments = GenericAttachments::make('https://test.com/image.png')
->add('https://test.com/video.mp4');
$attachments = new GenericAttachments();
$attachments->add([
'https://test.com/image.png',
'https://test.com/video.mp4',
]);
Return FeedbackAction options as an array.
Example:
$attachments = GenericAttachments::make('https://test.com/image.png')
->toArray();
$attachments = new GenericAttachments();
$attachments->toArray();
Create new instance of StreamAttachment with initial values.
Example:
$actions = StreamAttachment::make('rtsp://my-url.com/stream');
Set the name of the action.
Example:
$actions = StreamAttachment::make('rtsp://my-url.com/stream')
->setStreamUrl('rtsp://my-url.com/different-stream');
$actions = new StreamAttachment();
$actions->setStreamUrl('rtsp://my-url.com/stream');
Return StreamAttachment options as an array.
Example:
$actions = StreamAttachment::make('rtsp://my-url.com/stream')
->toArray();
$actions = new StreamAttachment();
$actions->setStreamUrl('https://my-url.com/action')
->toArray();
Create new instance of VideoAttachment with initial values.
Example:
$actions = VideoAttachment::make('https://my-url.com/thumbnail.png', 'https://my-url.com/video.mp4');
Set the thumbnail URL of the attachment.
Example:
$actions = VideoAttachment::make('https://my-url.com/thumbnail.png', 'https://my-url.com/video.mp4')
->setThumbnailUrl('https://my-url.com/different-thumbnail.jpg');
$actions = new VideoAttachment();
$actions->setThumbnailUrl('rtsp://my-url.com/stream');
Set the video URL of the attachment.
Example:
$actions = VideoAttachment::make('https://my-url.com/thumbnail.png', 'https://my-url.com/video.mp4')
->setVideoUrl('https://my-url.com/different-video.mp4');
$actions = new VideoAttachment();
$actions->setVideoUrl('https://my-url.com/video.mp4');
Return VideoAttachment options as an array.
Example:
$actions = VideoAttachment::make('https://my-url.com/thumbnail.png', 'https://my-url.com/video.mp4')
->toArray();
$actions = new VideoAttachment();
$actions->setVideoUrl('https://my-url.com/action')
->toArray();
While this is built for Laravel, it's possible to use without.
<?php
use BarnsleyHQ\SimplePush\Models\GetAction;
use BarnsleyHQ\SimplePush\Models\GetActions;
use BarnsleyHQ\SimplePush\Models\SimplePushMessage;
(new SimplePushMessage)
->token('123456')
->title('Custom Alert')
->content('You have a new alert!')
->event('Custom Event')
->actions(GetActions::make([
GetAction::make('Pause for 1 hour', 'https://webhooks.my-url.com/pause?hours=1'),
GetAction::make('Pause for 24 hours', 'https://webhooks.my-url.com/pause?hours=24'),
]))
->send();
For basic testing, run:
$ composer test
Or for testing code coverage:
$ composer test:coverage
If you discover any security vulnerabilities, please email alex@barnsley.io instead of submitting an issue.
The MIT License (MIT). Please see License File for more information.