File: /var/www/admin.javaapp.co.uk/app/Helpers/AppPushNotification.php
<?php
namespace App\Helpers;
use Illuminate\Support\Carbon;
use Exception;
use Illuminate\Support\Facades\Log;
class AppPushNotification
{
/**
* Send push notification to app
*
* @param array $data
* @param integer $notification_type notification type
* @param string $title notification title
* @param string $body notification body
* @return bool true if notification send successfully
*/
public static function sendNotification($data, $notification_type, $title, $body)
{
try {
$SERVER_API_KEY = config('app.fcm_key.' . config('app.env'));
// foreach ($data as $value) {
switch ($notification_type) {
case 3:
// for manage loyalty stamp
$notification_message = [
// for single user
// 'to' => $value['fcm_token'],
// for multi user
'registration_ids' => $data['fcm_token'],
'data' => [
'title' => $title,
'message' => $body,
'notification_type' => $notification_type,
'time' => Carbon::now()->format('h:i A'),
// 'cafe_id' => $value['cafe_id'],
'cafe_id' => $data['cafe_id'],
'group_id' => '',
'admin' => false,
],
'notification' => [
'title' => $title,
'body' => $body,
'sound' => 'default',
'color' => '#F5F3F0',
'icon' => public_path('assets/img/default-image.png')
],
];
break;
case 4:
// for universal loyalty stamp
$notification_message = [
// for single user
// 'to' => $value['fcm_token'],
// for multi user
'registration_ids' => $data['fcm_token'],
'data' => [
'title' => $title,
'message' => $body,
'notification_type' => $notification_type,
'time' => Carbon::now()->format('h:i A'),
'cafe_id' => '',
'group_id' => '',
'admin' => true,
],
'notification' => [
'title' => $title,
'body' => $body,
'sound' => 'default',
'color' => '#F5F3F0',
'icon' => public_path('assets/img/default-image.png')
],
];
break;
default:
break;
}
$notifications = json_encode($notification_message);
$headers = [
'Authorization: key=' . $SERVER_API_KEY,
'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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $notifications);
$result = curl_exec($ch);
if ($result === FALSE || preg_match('/ERROR/', $result) || preg_match('/Error/', $result) || preg_match('/error/', $result)) {
// throw new Exception('Curl failed: ' . curl_error($ch));
Log::info('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
// }
return true;
} catch (\Throwable $th) {
throw new Exception($th);
}
}
}