D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
re-viewers.com
/
app
/
Http
/
Controllers
/
Admin
/
Filename :
NotificationController.php
back
Copy
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Setting; use App\Models\Review; use App\Models\Category; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; use GuzzleHttp\Client; class NotificationController extends Controller { /** * Show the notification settings and send notification forms. */ public function index() { $page_title = 'Send Notification'; $settings = Setting::find(1); $reviews = Review::where('status', 1)->orderBy('id', 'desc')->get(); $categories = Category::where('status', 1)->orderBy('id', 'desc')->get(); return view('pages.notification.index', compact('page_title', 'settings', 'reviews', 'categories')); } /** * Send a push notification using the OneSignal API. */ public function send(Request $request) { $request->validate([ 'notification_title' => 'required|string', 'notification_msg' => 'required|string', 'big_picture' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', 'product_id' => 'nullable|integer', 'cat_id' => 'nullable|integer', 'external_link' => 'nullable|url', ]); $settings = Setting::find(1); $client = new Client(); $file_path = null; if ($request->hasFile('big_picture')) { $imageName = time() . '_' . $request->file('big_picture')->getClientOriginalName(); $request->file('big_picture')->storeAs('images', $imageName, 'public'); $file_path = asset('storage/images/' . $imageName); } $id = 0; $type = ''; $title = ''; if ($request->filled('cat_id')) { $id = $request->cat_id; $type = 'category'; $title = Category::where('id', $id)->first()->category_name ?? ''; } if ($request->filled('product_id')) { $id = $request->product_id; $type = 'review'; $title = Review::find($id)->product_title ?? ''; } $fields = [ 'app_id' => $settings->onesignal_app_id, 'included_segments' => ['All'], 'data' => [ 'foo' => 'bar', 'id' => $id, 'external_link' => $request->external_link, 'type' => $type, 'title' => $title, ], 'headings' => ['en' => $request->notification_title], 'contents' => ['en' => $request->notification_msg], ]; if ($file_path) { $fields['big_picture'] = $file_path; } try { $response = $client->post('https://onesignal.com/api/v1/notifications', [ 'headers' => [ 'Content-Type' => 'application/json; charset=utf-8', 'Authorization' => 'Basic ' . $settings->onesignal_rest_key, ], 'json' => $fields, ]); // You can log the response for debugging if needed // $responseBody = json_decode($response->getBody(), true); return redirect()->route('notification.index')->with('success', 'Notification sent successfully.'); } catch (\Exception $e) { return redirect()->route('notification.index')->with('error', 'Failed to send notification: ' . $e->getMessage()); } } /** * Update OneSignal API settings. */ public function updateSettings(Request $request) { $request->validate([ 'onesignal_app_id' => 'required|string', 'onesignal_rest_key' => 'required|string', ]); $settings = Setting::find(1); $settings->onesignal_app_id = $request->onesignal_app_id; $settings->onesignal_rest_key = $request->onesignal_rest_key; $settings->save(); return redirect()->route('notification.index')->with('success', 'Notification settings saved successfully.'); } }