Ekapp/skolehjem/app/Http/Controllers/PushNotificationController.php

51 lines
1.3 KiB
PHP

<?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);
}
}