Edit File: Firebase1.php
<?php namespace App\Traits; use GuzzleHttp\Client; trait Firebase { public function sendNotification($tokens,$data,$type=''){ $apiurl = 'https://fcm.googleapis.com/v1/projects/'.env('PROJECT_ID').'/messages:send'; //replace "your-project-id" with...your project ID $headers = [ 'Authorization: Bearer ' . $this->getToken(), 'Content-Type: application/json' ]; $data = $this->prepareData($data); foreach ($tokens as $token) { $notification = [ 'title' => $data['title_'.lang()], 'body' => $data['message_'.lang()], ]; if($type == 'ios'){ $message=$this->getIosMessageFormat($token,$data,$notification); } else { $message = $this->getAndroidMessageFormat($token,$data); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiurl); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message)); $result = curl_exec($ch); curl_close($ch); \Log::info($type); \Log::info($token); \Log::info($result); } // if ($result === FALSE) { // //Failed // die('Curl failed: ' . curl_error($ch)); // } // curl_close($ch); } private function prepareData($data){ foreach($data as $key => $value){ if(is_int($value)){ $data[$key] = strval($value); } else if (is_bool($value)){ $data[$key] = strval($value); } else if(is_array($value)){ $data[$key] = json_encode($value); } } return $data; } private function getToken(){ // Parse service account details // $authConfig = json_decode($authConfigString); // Read private key from service account details // $secret = openssl_get_privatekey($authConfig->private_key); $secret = openssl_get_privatekey(env('PRIVATE_KEY')); // Create the token header $header = json_encode([ 'typ' => 'JWT', 'alg' => 'RS256' ]); // Get seconds since 1 January 1970 $time = time(); $payload = json_encode([ // "iss" => $authConfig->client_email, "iss" => env('CLIENT_EMAIL'), "scope" => "https://www.googleapis.com/auth/firebase.messaging", "aud" => "https://oauth2.googleapis.com/token", "exp" => $time + 3600, "iat" => $time ]); // Encode Header $base64UrlHeader = $this->base64UrlEncode($header); // Encode Payload $base64UrlPayload = $this->base64UrlEncode($payload); // Create Signature Hash $result = openssl_sign($base64UrlHeader . "." . $base64UrlPayload, $signature, $secret, OPENSSL_ALGO_SHA256); // Encode Signature to Base64Url String $base64UrlSignature = $this->base64UrlEncode($signature); // Create JWT $jwt = $base64UrlHeader . "." . $base64UrlPayload . "." . $base64UrlSignature; $client = new Client(); $response = $client->post('https://oauth2.googleapis.com/token', [ 'form_params' => [ 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwt ], 'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded' ] ]); $responseBody = json_decode($response->getBody()); if (!isset($responseBody->access_token)) { throw new \Exception("Failed to get access token: " . json_encode($responseBody)); } return $responseBody->access_token; } private function base64UrlEncode($text) { return str_replace( ['+', '/', '='], ['-', '_', ''], base64_encode($text) ); } private function getIosMessageFormat($token,$data,$notification){ return [ 'message' => [ 'token' => $token, 'notification' => $notification, 'data' => $data, 'apns' => [ 'headers' => [ 'apns-priority' => '10', // High priority for immediate delivery 'apns-push-type' => 'alert', // For alert notifications ], 'payload' => [ 'aps' => [ 'alert' => [ 'title' => $data['title_'.lang()], 'body' => $data['message_'.lang()], ], 'sound' => 'default', ], ], ], // 'sound' => 'default', ], ]; } private function getAndroidMessageFormat($token,$data){ return [ 'message' => [ 'token' => $token, // 'notification' => $notification, 'data' => $data, ], ]; } }
Back to File Manager