IMMREX7
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Http;
use Google\Auth\Credentials\ServiceAccountCredentials;
class SendNotificationApi {
static function sendNotification($reg_ids,$message) {
return self::initiateApi($reg_ids,$message);
}
static function sendSingleNotification($reg_ids,$message) {
return self::initiateSingleApi($reg_ids,$message);
}
static function getGoogleAccessToken(){
$credentialsFilePath = storage_path().'/app/schoolmis-1c700-73bb7e87821a.json';
$client = new \Google_Client();
$client->setAuthConfig($credentialsFilePath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();
return $token['access_token'];
}
static function sendMessage($json){
$apiurl = 'https://fcm.googleapis.com/v1/projects/schoolmis-1c700/messages:send';
$headers = [
'Authorization: Bearer ' . self::getGoogleAccessToken(),
'Content-Type: application/json'
];
$notifications = [
'title' => $json["title"],
'body' => $json["body"],
];
$data = [
"title" => $json["title"],
"body" => $json["body"],
"message" => $json["body"]
];
$message = [
'message' => [
'token' => $json["token"],
'notification' => $notifications,
'data' => $data,
],
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
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($message));
$result = curl_exec($ch);
if ($result === FALSE) {
//Failed
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
static function testSingleNotification($reg_ids,$message) {
foreach($reg_ids as $key=>$value){
$json["token"] = $value;
$json["title"] = "School Notification";
$json["body"] = $message;
self::sendMessage($json);
}
return 1;
/*$url = 'https://fcm.googleapis.com/v1/projects/schoolmis-1c700/messages:send';
$headers = [
'Authorization: Bearer ' . self::getGoogleAccessToken(),
'Content-Type: application/json'
];
$fields = array (
'registration_ids' =>$reg_ids,
'notification' => array (
"title" => "Notification",
"body" => $message
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
if(curl_errno($ch)) return curl_error($ch);
curl_close ( $ch );
return $result;*/
}
static function initiateApi($reg_ids,$message) {
$temp_ids=array();
for($i=0;$i<sizeof($reg_ids);$i++){
array_push($temp_ids,$reg_ids[$i]);
if( $i % 1000 == 0 && $i > 0)
{
self::initiateSingleApi($temp_ids,$message);
$temp_ids=array();
}else
if($i==sizeof($reg_ids)-1){
self::initiateSingleApi($temp_ids,$message);
}
}
return "success";
}
static function sendBulkNotification($reg_ids,$message){
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' =>$reg_ids,
'notification' => array (
"title" => "Notification",
"body" => $message
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "AAAAtaPk0ds:APA91bErbAfUXZPjSAe8hrsmbgABmflRGsmG_MrelqxBOKXdvDuJ0tiRPgqHg85Sla7jdoto3dnGGjRVuPibZkXFf5EzvbGUQEfGawxxKo30DoYgPzlBQUHUvkf9Ic64wc59NNjrO4qC",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
if(curl_errno($ch)) return curl_error($ch);
curl_close ( $ch );
return $result;
}
static function initiateSingleApi($reg_ids,$message) {
/*$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' =>$reg_ids,
'notification' => array (
"title" => "Notification",
"body" => $message
),
'data' => array (
"message" => $message
)
);
$fields = json_encode ( $fields );
$headers = array (
'Authorization: key=' . "AAAAtaPk0ds:APA91bErbAfUXZPjSAe8hrsmbgABmflRGsmG_MrelqxBOKXdvDuJ0tiRPgqHg85Sla7jdoto3dnGGjRVuPibZkXFf5EzvbGUQEfGawxxKo30DoYgPzlBQUHUvkf9Ic64wc59NNjrO4qC",
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec ( $ch );
if(curl_errno($ch)) return curl_error($ch);
curl_close ( $ch );
return $result;*/
foreach($reg_ids as $key=>$value){
if($value != null){
if($value != ""){
$json["token"] = $value;
$json["title"] = "School Notification";
$json["body"] = $message;
self::sendMessage($json);
}
}
}
return 1;
}
static function subscribeToTopic($tokens) {
$credentialsFilePath = storage_path().'/app/schoolmis-1c700-73bb7e87821a.json';
$url = 'https://iid.googleapis.com/iid/v1:batchAdd';
$accessToken = self::getAccessToken($credentialsFilePath);
$headers = [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json'
];
$topic = "communications";
$data = [
'to' => '/topics/' . $topic,
'registration_tokens' => $tokens
];
$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_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
static function getAccessToken($serviceAccountPath) {
// Define the required FCM scope
$scopes = ['https://www.googleapis.com/auth/firebase.messaging'];
// Create a Service Account Credentials object
$credentials = new ServiceAccountCredentials($scopes, $serviceAccountPath);
// Fetch the access token
$authToken = $credentials->fetchAuthToken();
return $authToken['access_token'];
}
static function sendNotificationToTopic($topic, $json) {
$url = 'https://fcm.googleapis.com/v1/projects/schoolmis-1c700/messages:send';
$headers = [
'Authorization: Bearer ' .self::getGoogleAccessToken(),
'Content-Type: application/json'
];
$notifications = [
'title' => $json["title"],
'body' => $json["body"],
];
$data = [
"title" => $json["title"],
"body" => $json["body"],
"message" => $json["body"]
];
$data = [
'message' => [
'topic' => $topic,
'notification' => $notifications,
'data' => $data,
]
];
$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_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
}
Copyright © 2021 -