Skip to content

Commit 8836ac8

Browse files
committed
update v2
1 parent a9f6472 commit 8836ac8

29 files changed

+1064
-206
lines changed

.stubs.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Filament\Notifications;
4+
use Illuminate\Database\Eloquent\Model;
5+
6+
{
7+
/*
8+
* @method static static sendToFCM(Model $user, array $data=[])
9+
* @method static static sendToEmail(Model $user)
10+
* @method static static sendToSlack(Model $user)
11+
* @method static static sendToDiscord(Model $user)
12+
* @method static static sendToSMSMisr(Model $user)
13+
* @method static static sendToFCM(Model $user)
14+
*/
15+
class Notification
16+
{
17+
public function sendToFCM(Model $user, array $data=[]): static {}
18+
public function sendToEmail(Model $user): static {}
19+
public function sendToSlack(Model $user): static {}
20+
public function sendToDiscord(Model $user): static {}
21+
public function sendToSMSMisr(Model $user): static {}
22+
}
23+
}

README.md

+114-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,25 @@
77
[![License](https://poser.pugx.org/tomatophp/filament-alerts/license.svg)](https://packagist.org/packages/tomatophp/filament-alerts)
88
[![Downloads](https://poser.pugx.org/tomatophp/filament-alerts/d/total.svg)](https://packagist.org/packages/tomatophp/filament-alerts)
99

10-
Send notification to users using notification templates and multi notification channels
10+
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
11+
12+
## Features
13+
14+
- Send Notification to users
15+
- Use Filament Native Notification
16+
- Use Notification Templates
17+
- Full FCM Service Worker Integration
18+
- Use Multiple Notification Channels
19+
- API to get notifications
20+
- Hide Notifications Resources
21+
- Use Slack Driver
22+
- Use Discord Driver
23+
- Use Reverb Driver
24+
- Use SMS Misr Driver
25+
- Use Email Driver
26+
- Use Database Driver
27+
- Use MessageBird Driver
28+
1129

1230
## Screenshots
1331

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

1937
## Installation
2038

21-
2239
before use this package make sure you have installed
2340

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

55+
if you are not using this package as a plugin please register the plugin on `/app/Providers/Filament/AdminPanelProvider.php`
56+
57+
```php
58+
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
59+
)
60+
```
61+
3862
## Usage
3963

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

103+
### Use Filament Native Notification
104+
105+
you can use the filament native notification and we add some `macro` for you
106+
107+
```php
108+
use Filament\Notifications\Notification;
109+
110+
Notification::make('send')
111+
->title('Test Notifications')
112+
->body('This is a test notification')
113+
->icon('heroicon-o-bell')
114+
->color('success')
115+
->actions([
116+
\Filament\Notifications\Actions\Action::make('view')
117+
->label('View')
118+
->url('https://google.com')
119+
->markAsRead()
120+
])
121+
->sendToDiscord(auth()->user())
122+
->sendToEmail(auth()->user())
123+
->broadcast(auth()->user())
124+
->sendToDatabase(auth()->user())
125+
->sendToSlack(auth()->user())
126+
->sendToFCM(auth()->user())
127+
```
128+
79129
### Notification Service
80130

81131
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.
@@ -99,15 +149,14 @@ SendNotification::make($template->providers)
99149

100150
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
101151

102-
103152
### Notification Channels
104153

105154
you can use multiple notification channels like
106155

107156
- Email
108157
- SMS
109158
- FCM
110-
- Pusher
159+
- Reverb
111160
- Database
112161
- Slack
113162
- Discord
@@ -118,16 +167,76 @@ it can be working with direct user methods like
118167
$user->notifySMSMisr(string $message);
119168
$user->notifyEmail(string $message, ?string $subject = null, ?string $url = null);
120169
$user->notifyFCMSDK(string $message, string $type='web', ?string $title=null, ?string $url=null, ?string $image=null, ?string $icon=null, ?array $data=[]);
121-
$user->notifyPusherSDK(string $token, string $title, string $message);
122170
$user->notifyDB(string $message, ?string $title=null, ?string $url =null);
123171
$user->notifySlack(string $title,string $message=null,?string $url=null, ?string $image=null, ?string $webhook=null);
124172
$user->notifyDiscord(string $title,string $message=null,?string $url=null, ?string $image=null, ?string $webhook=null);
125173
```
126174

175+
### Use FCM Notifications Provider
176+
177+
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
178+
179+
```php
180+
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
181+
->useSettingHub()
182+
->useFCM()
183+
)
184+
```
185+
186+
now go to settings hub and update FCM settings by add admin-auth.json file and update fcm settings than run this command
187+
188+
```bash
189+
php artisan filament-alerts:fcm
190+
```
191+
192+
it will generate FCM worker for you to make notifications working on the background.
193+
194+
195+
### Hide Notifications Resources
196+
197+
to hide the notification resources from the sidebar you can use the plugin method `hideNotificationsResources` like
198+
199+
```php
200+
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
201+
->hideNotificationsResources()
202+
)
203+
```
204+
205+
### Use Slack Driver
206+
207+
to use slack driver you must set the slack webhook on the settings hub and use the plugin method `useSlack` like
208+
209+
```php
210+
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
211+
->useSlack()
212+
)
213+
```
214+
215+
now on your `.env` file add a `SLACK_WEBHOOK` key with the webhook URL
216+
217+
### Use Discord Driver
218+
219+
to use discord driver you must set the discord webhook on the settings hub and use the plugin method `useDiscord` like
220+
221+
```php
222+
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
223+
->useDiscord()
224+
)
225+
```
226+
227+
now on your `.env` file add a `DISCORD_WEBHOOK` key with the webhook URL
228+
127229
## API
128230

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

233+
you can change the user model by use the plugin method `apiModel` like
234+
235+
```php
236+
->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
237+
->apiModel(User::class)
238+
)
239+
```
131240
## Publish Assets
132241

133242
you can publish config file by use this command

composer.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
{
22
"name": "tomatophp/filament-alerts",
33
"type": "library",
4-
"description": "Send notification to users using notification templates and multi notification channels",
4+
"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",
55
"keywords": [
66
"php",
77
"laravel",
88
"notifications",
99
"filament",
1010
"alets",
11-
"TALL stack"
11+
"TALL stack",
12+
"fcm",
13+
"discord",
14+
"slack",
15+
"email",
16+
"database",
17+
"reverb"
1218
],
1319
"license": "MIT",
1420
"autoload": {

config/filament-alerts.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@
7070
"id" => "fcm-api"
7171
],
7272
[
73-
"name" => 'Pusher Web',
74-
"id" => "pusher-web"
75-
],
76-
[
77-
"name" => 'Pusher Mobile',
78-
"id" => "pusher-api"
73+
"name" => 'Reverb',
74+
"id" => "reverb"
7975
],
8076
[
8177
"name" => 'SMS MessageBird',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Spatie\LaravelSettings\Migrations\SettingsMigration;
4+
5+
return new class extends SettingsMigration
6+
{
7+
public function up(): void
8+
{
9+
$this->migrator->add('notifications.notifications_sound', null);
10+
}
11+
};

resources/lang/ar/messages.php

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"back" => "رجوع",
66
"notifications" => [
77
"title" => "التنبيهات",
8+
"single" => "تنبيه",
89
"create" => "إنشاء تنبيه",
910
"logs" => "سجل التنبيهات",
1011
"form" => [
@@ -19,6 +20,7 @@
1920
],
2021
"templates" => [
2122
"title" => "القوالب",
23+
"single" => "قالب",
2224
"create" => "إنشاء قالب",
2325
"edit" => "تعديل قالب",
2426
"actions" => [
@@ -58,6 +60,7 @@
5860
],
5961
"settings" => [
6062
"group" => "التنبيهات",
63+
"notifications_sound" => "صوت التنبيهات",
6164
"firebase" => [
6265
"title" => "إعدادات فايربيز",
6366
"description" => "تحديث الاعدادات الخاصة بالفايربيز",

resources/lang/en/messages.php

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"back" => "Back",
66
"notifications" => [
77
"title" => "Notifications",
8+
"single" => "Notification",
89
"create" => "Create Notification",
910
"logs" => "Logs",
1011
"form" => [
@@ -19,6 +20,7 @@
1920
],
2021
"templates" => [
2122
"title" => "Templates",
23+
"single" => "Template",
2224
"create" => "Create Template",
2325
"edit" => "Edit Template",
2426
"actions" => [
@@ -58,6 +60,7 @@
5860
],
5961
"settings" => [
6062
"group" => "Notifications",
63+
"notifications_sound" => "Notifications Sound",
6164
"firebase" => [
6265
"title" => "Firebase Settings",
6366
"description" => "Update firebase connection settings",
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@if($url)
2+
<a href="{{ $url }}" target="_blank">
3+
{!! $content !!}
4+
</a>
5+
@else
6+
{!! $content !!}
7+
@endif
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<div>
2+
3+
</div>
4+
<script type="module">
5+
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js'
6+
import {getMessaging, onMessage, getToken} from "https://www.gstatic.com/firebasejs/10.12.2/firebase-messaging.js";
7+
8+
const firebaseConfig = {
9+
apiKey: "{{ setting('fcm_apiKey') }}",
10+
authDomain: "{{ setting('fcm_authDomain') }}",
11+
databaseURL: "{{ setting('fcm_database_url') }}",
12+
projectId: "{{ setting('fcm_projectId') }}",
13+
storageBucket: "{{ setting('fcm_storageBucket') }}",
14+
messagingSenderId: "{{ setting('fcm_messagingSenderId') }}",
15+
appId: "{{ setting('fcm_appId') }}",
16+
measurementId: "{{ setting('fcm_measurementId') }}",
17+
};
18+
const app = initializeApp(firebaseConfig);
19+
const messaging = getMessaging(app);
20+
Notification.requestPermission().then((permission) => {
21+
if (permission === "granted") {
22+
console.log("Notification permission granted.");
23+
if ("serviceWorker" in navigator) {
24+
navigator.serviceWorker
25+
.register("/firebase-messaging-sw.js")
26+
.then(function (registration) {
27+
console.log(
28+
"Registration successful, scope is:",
29+
registration.scope
30+
);
31+
})
32+
.catch(function (err) {
33+
console.log(
34+
"Service worker registration failed, error:",
35+
err
36+
);
37+
});
38+
}
39+
navigator.serviceWorker.getRegistration().then(async (reg) => {
40+
let token = await getToken(messaging, {vapidKey: "{{ setting('fcm_vapid') }}"});
41+
console.log(token);
42+
Livewire.dispatch('fcm-token', { token: token });
43+
44+
45+
onMessage(messaging, (payload) => {
46+
console.log("message: ", payload);
47+
var audio = new Audio('https://devsuez.emalleg.net/storage/sound/notifications.mp3');
48+
audio.play();
49+
50+
console.log(payload);
51+
Livewire.dispatch('fcm-notification', {data: payload})
52+
// push notification can send event.data.json() as well
53+
const options = {
54+
body: payload.data.body,
55+
icon: payload.data.image,
56+
tag: "alert",
57+
};
58+
let notification = reg.showNotification(
59+
payload.data.title,
60+
options
61+
);
62+
// link to page on clicking the notification
63+
notification.onclick = (payload) => {
64+
window.open(payload.data.url);
65+
};
66+
});
67+
});
68+
}
69+
70+
});
71+
72+
</script>

resources/views/firebase.blade.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@livewire(\TomatoPHP\FilamentAlerts\Livewire\Firebase::class)
2+
3+

0 commit comments

Comments
 (0)