v1.5.0 Added notifications - Pytlick
This commit is contained in:
@@ -73,7 +73,7 @@ class NewsController extends Controller
|
||||
$news->save();
|
||||
|
||||
Helpers::sendNewsNotification($news, User::query()->where("wants_emails", "=", true)->get());
|
||||
|
||||
PushNotificationController::push();
|
||||
return redirect()->route("news.index");
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ class NewsController extends Controller
|
||||
$news->save();
|
||||
|
||||
Helpers::sendNewsNotification($news, User::query()->where("wants_emails", "=", true)->get());
|
||||
|
||||
PushNotificationController::push();
|
||||
return $news;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Notification;
|
||||
use App\Notifications\PushNews;
|
||||
use App\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class PushNotificationController extends Controller
|
||||
{
|
||||
public function __construct(){
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the PushSubscription.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function store(Request $request){
|
||||
$this->validate($request,[
|
||||
'endpoint' => 'required',
|
||||
'keys.auth' => 'required',
|
||||
'keys.p256dh' => 'required'
|
||||
]);
|
||||
$endpoint = $request->endpoint;
|
||||
$token = $request->keys['auth'];
|
||||
$key = $request->keys['p256dh'];
|
||||
$user = Auth::user();
|
||||
$user->updatePushSubscription($endpoint, $key, $token);
|
||||
|
||||
return response()->json(['success' => true],200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send Push Notifications to all users.
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
static public function push(){
|
||||
\Illuminate\Support\Facades\Notification::send(User::all(), new PushNews);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use NotificationChannels\WebPush\WebPushChannel;
|
||||
use NotificationChannels\WebPush\WebPushMessage;
|
||||
|
||||
class PushNews extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function via($notifiable)
|
||||
{
|
||||
return [WebPushChannel::class];
|
||||
}
|
||||
|
||||
public function toWebPush($notifiable, $notification)
|
||||
{
|
||||
return (new WebPushMessage)
|
||||
->title('Ny Nyhed')
|
||||
->icon('/images/icons/appIcon.png')
|
||||
->body('Der er en ny nyhed i ekapp\'en')
|
||||
->action('Se nyheder', 'root.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mail representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return \Illuminate\Notifications\Messages\MailMessage
|
||||
*/
|
||||
public function toMail($notifiable)
|
||||
{
|
||||
return (new MailMessage)
|
||||
->line('The introduction to the notification.')
|
||||
->action('Notification Action', url('/'))
|
||||
->line('Thank you for using our application!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array representation of the notification.
|
||||
*
|
||||
* @param mixed $notifiable
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($notifiable)
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
@@ -23,6 +24,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
Schema::defaultStringLength(191);
|
||||
if(env('HTTPS_ONLY') == true)
|
||||
url()->forceScheme('https');
|
||||
}
|
||||
|
||||
@@ -15,12 +15,14 @@ use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Spatie\Permission\Traits\HasRoles;
|
||||
use NotificationChannels\WebPush\HasPushSubscriptions;
|
||||
|
||||
//Class of which should extend Model Library
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use Notifiable;
|
||||
use HasRoles;
|
||||
use HasPushSubscriptions;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
||||
Reference in New Issue
Block a user