Files
bella_masala_laravel/app/Traits/OneSignalHelper.php
2025-11-06 06:55:15 +00:00

122 lines
3.4 KiB
PHP

<?php
namespace App\Traits;
use App\Models\Setting;
trait OneSignalHelper
{
public function setupEnvironmentForServiceProvider(): array
{
$appKey = "7dc8528d-f184-4902-a478-7785b795d5df";
$secretKey = "os_v2_app_pxeffdprqreqfjdyo6c3pfov34dbi44ywtle6xewpet3244pdtkzvrzrc63gl4hmeavupgz3i6hch5wge6f7u2li4dvylyhjp5d6mna";
return [
'appKey' => $appKey,
'secretKey' => $secretKey
];
}
public function setupEnvironmentForClients(): array
{
$appKey = "1abab490-0768-468e-92ca-1512f8b65f90";
$secretKey = "os_v2_app_dk5ljeahnbdi5ewkcujprns7sc4xhmzg6iaujlvl3bdnayojmdoeubourulpnfjru7pf55mlgydnvdhqqmxgw2ipbvcivmn3ravacfy";
return [
'appKey' => $appKey,
'secretKey' => $secretKey
];
}
public function sendAppNotification(string $notificationTitle, string $notificationContent, string $userType)
{
$configuration = $userType === "client"
? $this->setupEnvironmentForClients()
: $this->setupEnvironmentForServiceProvider();
$fields = [
'app_id' => $configuration['appKey'],
'included_segments' => ['All'],
'headings' => ['en' => $notificationTitle],
'contents' => ['en' => $notificationContent],
];
$jsonFields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic ' . $configuration['secretKey']
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonFields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}
public function sendNotificationToSingleUser(
string $notificationTitle,
string $notificationContent,
string $notificationId,
string $userType
) {
$fcmServerKey = "";
return;
if (!empty($notificationId)) {
return response()->json(['error' => 'User or FCM token not found.'], 404);
}
$fcmToken = $user->fcm_token;
$notification = [
'to' => $fcmToken,
'notification' => [
'title' => $notificationTitle,
'body' => $notificationContent,
'sound' => 'default',
],
'data' => [
'notification_id' => $notificationId,
'click_action' => 'FLUTTER_NOTIFICATION_CLICK'
],
'priority' => 'high'
];
$headers = [
'Authorization: key=' . $fcmServerKey,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // You can set this to true in production
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($notification));
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return response()->json(['error' => $error], 500);
}
return response()->json(['response' => json_decode($result)], 200);
}
}