Skip to content

Commit

Permalink
update v2
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Jun 10, 2024
1 parent a9f6472 commit 8836ac8
Show file tree
Hide file tree
Showing 29 changed files with 1,064 additions and 206 deletions.
23 changes: 23 additions & 0 deletions .stubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Filament\Notifications;
use Illuminate\Database\Eloquent\Model;

{
/*
* @method static static sendToFCM(Model $user, array $data=[])
* @method static static sendToEmail(Model $user)
* @method static static sendToSlack(Model $user)
* @method static static sendToDiscord(Model $user)
* @method static static sendToSMSMisr(Model $user)
* @method static static sendToFCM(Model $user)
*/
class Notification
{
public function sendToFCM(Model $user, array $data=[]): static {}
public function sendToEmail(Model $user): static {}
public function sendToSlack(Model $user): static {}
public function sendToDiscord(Model $user): static {}
public function sendToSMSMisr(Model $user): static {}
}
}
119 changes: 114 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@
[![License](https://poser.pugx.org/tomatophp/filament-alerts/license.svg)](https://packagist.org/packages/tomatophp/filament-alerts)
[![Downloads](https://poser.pugx.org/tomatophp/filament-alerts/d/total.svg)](https://packagist.org/packages/tomatophp/filament-alerts)

Send notification to users using notification templates and multi notification channels
Send notification to users using notification templates and multi notification channels, it's support Filament Native Notification Service with macro, and a full integration to FCM service worker notifications

## Features

- Send Notification to users
- Use Filament Native Notification
- Use Notification Templates
- Full FCM Service Worker Integration
- Use Multiple Notification Channels
- API to get notifications
- Hide Notifications Resources
- Use Slack Driver
- Use Discord Driver
- Use Reverb Driver
- Use SMS Misr Driver
- Use Email Driver
- Use Database Driver
- Use MessageBird Driver


## Screenshots

Expand All @@ -18,7 +36,6 @@ Send notification to users using notification templates and multi notification c

## Installation


before use this package make sure you have installed

- [Filament Spatie Translatable](https://filamentphp.com/plugins/filament-spatie-translatable)
Expand All @@ -35,6 +52,13 @@ after install your package please run this command
php artisan filament-alerts:install
```

if you are not using this package as a plugin please register the plugin on `/app/Providers/Filament/AdminPanelProvider.php`

```php
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
)
```

## Usage

to set up any model to get notifications you
Expand Down Expand Up @@ -76,6 +100,32 @@ the notification is run on queue, so you must run the queue worker to send the n
php artisan queue:work
```

### Use Filament Native Notification

you can use the filament native notification and we add some `macro` for you

```php
use Filament\Notifications\Notification;

Notification::make('send')
->title('Test Notifications')
->body('This is a test notification')
->icon('heroicon-o-bell')
->color('success')
->actions([
\Filament\Notifications\Actions\Action::make('view')
->label('View')
->url('https://google.com')
->markAsRead()
])
->sendToDiscord(auth()->user())
->sendToEmail(auth()->user())
->broadcast(auth()->user())
->sendToDatabase(auth()->user())
->sendToSlack(auth()->user())
->sendToFCM(auth()->user())
```

### Notification Service

to create a new template you can use template CRUD and make sure that the template key is unique because you will use it on every single notification.
Expand All @@ -99,15 +149,14 @@ SendNotification::make($template->providers)

where `$template` is selected of the template by key and $matchesTitle and $matchesBody is an array of matches to replace the template and $titleFill and $titleBody are an array of values to replace the matches


### Notification Channels

you can use multiple notification channels like

- Email
- SMS
- FCM
- Pusher
- Reverb
- Database
- Slack
- Discord
Expand All @@ -118,16 +167,76 @@ it can be working with direct user methods like
$user->notifySMSMisr(string $message);
$user->notifyEmail(string $message, ?string $subject = null, ?string $url = null);
$user->notifyFCMSDK(string $message, string $type='web', ?string $title=null, ?string $url=null, ?string $image=null, ?string $icon=null, ?array $data=[]);
$user->notifyPusherSDK(string $token, string $title, string $message);
$user->notifyDB(string $message, ?string $title=null, ?string $url =null);
$user->notifySlack(string $title,string $message=null,?string $url=null, ?string $image=null, ?string $webhook=null);
$user->notifyDiscord(string $title,string $message=null,?string $url=null, ?string $image=null, ?string $webhook=null);
```

### Use FCM Notifications Provider

to make FCM Notification Work you need to install [Filament Settings Hub](https://www.github.com/tomatophp/filament-settings-hub) and allow use Setting Hub on the Plugin

```php
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
->useSettingHub()
->useFCM()
)
```

now go to settings hub and update FCM settings by add admin-auth.json file and update fcm settings than run this command

```bash
php artisan filament-alerts:fcm
```

it will generate FCM worker for you to make notifications working on the background.


### Hide Notifications Resources

to hide the notification resources from the sidebar you can use the plugin method `hideNotificationsResources` like

```php
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
->hideNotificationsResources()
)
```

### Use Slack Driver

to use slack driver you must set the slack webhook on the settings hub and use the plugin method `useSlack` like

```php
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
->useSlack()
)
```

now on your `.env` file add a `SLACK_WEBHOOK` key with the webhook URL

### Use Discord Driver

to use discord driver you must set the discord webhook on the settings hub and use the plugin method `useDiscord` like

```php
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
->useDiscord()
)
```

now on your `.env` file add a `DISCORD_WEBHOOK` key with the webhook URL

## API

we are support some API to get the notification and make some actions you can find it under `api/notifications` route

you can change the user model by use the plugin method `apiModel` like

```php
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
->apiModel(User::class)
)
```
## Publish Assets

you can publish config file by use this command
Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"name": "tomatophp/filament-alerts",
"type": "library",
"description": "Send notification to users using notification templates and multi notification channels",
"description": "Send notification to users using notification templates and multi notification channels, it's support Filament Native Notification Service with macro, and a full integration to FCM service worker notifications",
"keywords": [
"php",
"laravel",
"notifications",
"filament",
"alets",
"TALL stack"
"TALL stack",
"fcm",
"discord",
"slack",
"email",
"database",
"reverb"
],
"license": "MIT",
"autoload": {
Expand Down
8 changes: 2 additions & 6 deletions config/filament-alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@
"id" => "fcm-api"
],
[
"name" => 'Pusher Web',
"id" => "pusher-web"
],
[
"name" => 'Pusher Mobile',
"id" => "pusher-api"
"name" => 'Reverb',
"id" => "reverb"
],
[
"name" => 'SMS MessageBird',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class extends SettingsMigration
{
public function up(): void
{
$this->migrator->add('notifications.notifications_sound', null);
}
};
3 changes: 3 additions & 0 deletions resources/lang/ar/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"back" => "رجوع",
"notifications" => [
"title" => "التنبيهات",
"single" => "تنبيه",
"create" => "إنشاء تنبيه",
"logs" => "سجل التنبيهات",
"form" => [
Expand All @@ -19,6 +20,7 @@
],
"templates" => [
"title" => "القوالب",
"single" => "قالب",
"create" => "إنشاء قالب",
"edit" => "تعديل قالب",
"actions" => [
Expand Down Expand Up @@ -58,6 +60,7 @@
],
"settings" => [
"group" => "التنبيهات",
"notifications_sound" => "صوت التنبيهات",
"firebase" => [
"title" => "إعدادات فايربيز",
"description" => "تحديث الاعدادات الخاصة بالفايربيز",
Expand Down
3 changes: 3 additions & 0 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"back" => "Back",
"notifications" => [
"title" => "Notifications",
"single" => "Notification",
"create" => "Create Notification",
"logs" => "Logs",
"form" => [
Expand All @@ -19,6 +20,7 @@
],
"templates" => [
"title" => "Templates",
"single" => "Template",
"create" => "Create Template",
"edit" => "Edit Template",
"actions" => [
Expand Down Expand Up @@ -58,6 +60,7 @@
],
"settings" => [
"group" => "Notifications",
"notifications_sound" => "Notifications Sound",
"firebase" => [
"title" => "Firebase Settings",
"description" => "Update firebase connection settings",
Expand Down
7 changes: 7 additions & 0 deletions resources/views/email/template.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@if($url)
<a href="{{ $url }}" target="_blank">
{!! $content !!}
</a>
@else
{!! $content !!}
@endif
72 changes: 72 additions & 0 deletions resources/views/firebase-base.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<div>

</div>
<script type="module">
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js'
import {getMessaging, onMessage, getToken} from "https://www.gstatic.com/firebasejs/10.12.2/firebase-messaging.js";
const firebaseConfig = {
apiKey: "{{ setting('fcm_apiKey') }}",
authDomain: "{{ setting('fcm_authDomain') }}",
databaseURL: "{{ setting('fcm_database_url') }}",
projectId: "{{ setting('fcm_projectId') }}",
storageBucket: "{{ setting('fcm_storageBucket') }}",
messagingSenderId: "{{ setting('fcm_messagingSenderId') }}",
appId: "{{ setting('fcm_appId') }}",
measurementId: "{{ setting('fcm_measurementId') }}",
};
const app = initializeApp(firebaseConfig);
const messaging = getMessaging(app);
Notification.requestPermission().then((permission) => {
if (permission === "granted") {
console.log("Notification permission granted.");
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/firebase-messaging-sw.js")
.then(function (registration) {
console.log(
"Registration successful, scope is:",
registration.scope
);
})
.catch(function (err) {
console.log(
"Service worker registration failed, error:",
err
);
});
}
navigator.serviceWorker.getRegistration().then(async (reg) => {
let token = await getToken(messaging, {vapidKey: "{{ setting('fcm_vapid') }}"});
console.log(token);
Livewire.dispatch('fcm-token', { token: token });
onMessage(messaging, (payload) => {
console.log("message: ", payload);
var audio = new Audio('https://devsuez.emalleg.net/storage/sound/notifications.mp3');
audio.play();
console.log(payload);
Livewire.dispatch('fcm-notification', {data: payload})
// push notification can send event.data.json() as well
const options = {
body: payload.data.body,
icon: payload.data.image,
tag: "alert",
};
let notification = reg.showNotification(
payload.data.title,
options
);
// link to page on clicking the notification
notification.onclick = (payload) => {
window.open(payload.data.url);
};
});
});
}
});
</script>
3 changes: 3 additions & 0 deletions resources/views/firebase.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@livewire(\TomatoPHP\FilamentAlerts\Livewire\Firebase::class)


Loading

0 comments on commit 8836ac8

Please sign in to comment.