• File: PushNotificationController.php.save
  • Full Path: /var/www/lastworkingimaliapi/app/Http/Controllers/PushNotificationController.php.save
  • Date Modified: 12/20/2022 12:59 AM
  • File size: 3.5 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace App\Http\Controllers;

use App\Classes\SendSMSSislog;
use App\Imali\ImaliAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class PushNotificationController extends Controller
{


    public function sendMessage(Request $request)
    {


        if ($request->messageFormat === 'SMS') {

            //$token = new SendSMSSislog();

	     $numbers = [];

            foreach ($request->accounts as $account) {

                $user = ImaliAccount::query()
                    ->join('users', 'users.id', '=', 'imali_accounts.user_id')
                    ->where('account_number', $account)
                    ->first();
		   array_push($numbers,$user->country_code . $user->phone);
//                'body' => $request->message,
               // $data = ['message' => $request->message, 'phone' => $user->phone];
//               $token->sendNotification($user);
               // $token->sendNotification($data);
            }

	$response = Http::post('http://localhost:3000/api/send/sms',['phoneNumbers'=>$numbers,'message'=>$request->message]);

	return $response;
        }
        if ($request->messageFormat === 'PUSH') {

            $notification = array(
//                'icon' => 'ic_imali_logo_verde_01',
                'icon' => 'ic_i_mali_cover',
                'title' => $request->title,
                'body' => $request->message,
                'click_action' => 'com.imali.payapp.payment_NOTICIA',
//                            'color' => '#008577'
                'color' => '#ffffff'
            );

            $data = array(
//            'transaction' => $recharge->transaction_id,
//            'name' => $user->name,
//            'amount' => (double)$recharge->amount,
//            'phone' => $user->phone,
//            'reference' => $imaliAccount->reference,
//            'data' => $recharge->created_at,
//            'estado' => $recharge->estado,
                'sms' => $request->message,
                'terminal' => 'firebase'
            );

            foreach ($request->accounts as $account) {

                $user = ImaliAccount::query()
                    ->join('users', 'users.id', '=', 'imali_accounts.user_id')
                    ->where('account_number', $account)
                    ->first();

                $this->pushNotifification($user->firebase_token, $notification, $data);
            }
        }

        if ($request->messageFormat === 'EMAIL') {

        }


    }

    public function pushNotifification($token, $notification = array(), $data = array())
    {
        $apiKey = 'AAAA8zVzEPQ:APA91bHl_DXB6UGb_6gZlmFnaLTQoANtX_OBjvl3nOy2bSlnFhxedvk6EhGj7cZoIvmlbKeCnqGxXbuyMH_rEPuhRXvuitXzo6Pfl2TMXLar1PlifXqEhYq6tS55UMrY2Kffzj-P_UH-';
        $fields = array('to' => $token, 'notification' => $notification, 'data' => $data);
        $headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json');
        $url = 'https://fcm.googleapis.com/fcm/send';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($curl);
        curl_close($curl);

        return json_decode($result, true);

    }
}