|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/bizkrypt/admin.bizkrypt.com/app/Helpers/ |
Upload File : |
<?php
namespace App\Helpers;
use App\Models\Subject;
use App\Models\User;
use Google\Client as GoogleClient;
use Illuminate\Support\Facades\Storage;
class Backend
{
public static function get_subject_name($class_id, $subject_id)
{
if (!empty($subject_id)) {
$subject = Subject::where(['id' => $subject_id, 'class_id' => $class_id])->first();
return $subject->subject;
}
return "-";
}
public static function get_teacher_name($user_id)
{
if (!empty($user_id)) {
$Qteacher = User::join('user_details', 'users.id', '=', 'user_details.user_id');
$Qteacher->where(['users.id' => $user_id]);
$teachers = $Qteacher->first(['users.id', 'users.name']);
if (!empty($teachers)) {
return $teachers->name;
}
return "-";
}
return "-";
}
// FCM Notification
public static function sendFcmNotificationTopic($topic_name, $heading, $text, $image = null)
{
$title = $heading;
$description = $text;
$projectId = config('services.fcm.parent_project_id');
$credentialsFilePath = Storage::path('json/firebase-adminsdk-4lr71.json');
$client = new GoogleClient();
$client->setAuthConfig($credentialsFilePath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();
$access_token = $token['access_token'];
$headers = [
"Authorization: Bearer $access_token",
'Content-Type: application/json',
];
$data = [
"message" => [
'topic' => $topic_name,
'notification' => [
'title' => $title,
'body' => $description,
'image' => $image ?? "https://school.edukrypt.app/public/logo.png"
],
],
];
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/v1/projects/$projectId/messages: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);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable verbose output for debugging
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
return true;
} else {
return false;
}
}
// FCM Notification
public static function sendFcmNotificationToken($tokens, $heading, $text, $image = null)
{
if (empty($tokens)) {
return false;
}
$title = $heading;
$description = $text;
$projectId = config('services.fcm.parent_project_id');
$credentialsFilePath = Storage::path('json/firebase-adminsdk-4lr71.json');
$client = new GoogleClient();
$client->setAuthConfig($credentialsFilePath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();
$access_token = $token['access_token'];
$headers = [
"Authorization: Bearer $access_token",
'Content-Type: application/json',
];
// Split to 1000 chunks
$chunks = array_chunk($tokens, 950);
$response = [];
foreach ($chunks as $chunk) {
foreach ($chunk as $token) {
# code...
$data = [
"message" => [
'notification' => [
'title' => $title,
'body' => $description,
'image' => $image ?? "https://school.edukrypt.app/public/logo.png"
],
"token" => $token
]
];
$payload = json_encode($data);
// dd($payload);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/v1/projects/$projectId/messages: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);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable verbose output for debugging
$res = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
$response[] = $res;
}
}
return true;
}
}