| Server IP : 34.87.99.72 / Your IP : 216.73.217.31 Web Server : Apache/2.4.46 (Unix) OpenSSL/1.1.1n System : Linux zlock-bitnami-lamp-prod-v2-vm 4.19.0-16-cloud-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64 User : bitnami ( 1000) PHP Version : 7.4.18 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /opt/bitnami/apache/htdocs/app/app/Http/Services/ |
Upload File : |
<?php
namespace App\Http\Services;
use App\Models\UserDevice;
use App\Models\UserFcmToken;
use Google\Client as GoogleClient;
use Storage;
/*
## Reference
- https://firebase.google.com/docs/cloud-messaging/http-server-ref
*/
class FcmService
{
public function notifyUser($payload){
foreach ($payload['token'] as $token) {
$notification = [
'title' => $payload['title'] ?? ".",
'body' => $payload['body'] ?? ".",
];
$fcmNotification = [
'message' => [
"notification" => $notification,
"token" => $token
]
];
$curl_result = self::send($fcmNotification);
\Debugbar::info($curl_result);
info($payload['title']);
info($token);
if($curl_result && isset($curl_result->error)){
UserFcmToken::whereIn('token', [
$token
])
->delete();
}
}
return null;
}
public function sendBoardcast($payload){
$notification = [
'topic' => $payload['to'],
'notification' => [
'title' => $payload['title'] ?? ".",
'body' => $payload['body'] ?? ".",
],
];
$fcmNotification = [
'message' => $notification,
];
$curl_result = self::send($fcmNotification);
\Debugbar::info($curl_result);
info("[Boardcast] ".$payload['title']);
info($curl_result ? "null" : $curl_result->message_id);
return null;
}
public function send($fcmNotification){
$project_id = config('services.fcm.project_id');
$fcmUrl = "https://fcm.googleapis.com/v1/projects/{$project_id}/messages:send";
$api_key = config('services.fcm.key');
$access_token = self::getAccessToken();
$headers = [
"Authorization: Bearer $access_token",
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
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($fcmNotification));
$content = curl_exec($ch);
$curl_result = json_decode($content);
\Log::debug($content);
curl_close($ch);
return $curl_result;
}
public function getAccessToken(){
$json = config('services.fcm.json');
$credentialsFilePath = Storage::path('json/'.$json);
$client = new GoogleClient();
$client->setAuthConfig($credentialsFilePath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();
info($token);
return $token["access_token"];
}
// public function send_notification($target_token,$target_user_id, $type, $title, $content, $onTap = null)
// {
// $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
// $api_key = 'AAAADkXwVLk:APA91bFQvBGwjiVzbJIza7fNnFvwmBIpak50O15wRj8FhN_-9LZNfTYIvfevrRQv7mwTIliBMi9L1wDxRXw2dQ6iTN2akvfo86BhE7QDpPlugifjrpQlbxNERqmOwqn7wAYFz3TWfLpK';
// $headers = [
// 'Authorization: key='.$api_key,
// 'Content-Type: application/json'
// ];
// $notification = [
// 'title' => $title,
// 'body' => substr($content,0,2) == "{\"" ? self::bake_fcm_message($type,$content) : $content,
// 'sound' => true,
// // 'image' => "some_url"
// ];
// $data = null;
// if($onTap){
// $data = [
// "type" => $type,
// "id"=> $onTap,
// ];
// }
// $fcmNotification = [
// 'notification' => $notification,
// 'data' => $data,
// "priority" => "high",
// // "dry_run" => true
// ];
// if(is_array($target_token) && count($target_token) > 0 ){
// $fcmNotification['registration_ids'] = $target_token;
// }else{
// $fcmNotification['to'] = $target_token;
// }
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL,$fcmUrl);
// 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($fcmNotification));
// $curl_result = json_decode(curl_exec($ch));
// curl_close($ch);
// // handle result (remove device token)
// if(isset($fcmNotification['registration_ids']) && isset($curl_result->results)){
// foreach ($curl_result->results as $key => $result) {
// if(isset($result->error) ) {
// UserDevice::where('user_id',$target_user_id[$key])->delete();
// }
// }
// }
// return $curl_result;
// }
// protected function bake_fcm_message($type,$keywords){
// $decoded = json_decode($keywords);
// switch ($type) {
// // case 'ERS': // En.Rollment Success
// // $fcm_message = "Your enrollment for ".$decoded->course_name." at ".$decoded->provider_name." is successful!";
// // break;
// case 'EWQP': // Enroll With Q.Point
// $head = "You have spent ".($decoded->qpoints)*-1;
// $fcm_message = $head." QPoints for your enrollment of ".$decoded->course_name." at ".$decoded->provider_name.".";
// break;
// case 'RBQP': // Re.Bate Q.Point
// $head = "You received ".$decoded->qpoints;
// $fcm_message = $head." QPoints for your enrollment of ".$decoded->course_name." at ".$decoded->provider_name.".";
// break;
// case 'CER': // Cancel En.Rollent
// $fcm_message = "Your cancelation request on enrollment for ".$decoded->course_name." at ".$decoded->provider_name." is successful!";
// break;
// case 'CMSS': // ComMisSion Self
// $fcm_message = "You received ".$decoded->qpoints." ".$decoded->qpoint_type." QPoints from your enrollment for ".$decoded->course_name." at ".$decoded->provider_name.".";
// break;
// case 'CMSD': // ComMisSion Downline
// $fcm_message = "You received ".$decoded->qpoints." ".$decoded->qpoint_type." QPoints from your downline ".$decoded->downline.".";
// break;
// // case 'RRER': // Rate and Review EnRollment
// // $fcm_message = "Rate and review ".$decoded->course_name." at ".$decoded->provider_name.".";
// // break;
// default:
// $fcm_message = $keywords;
// break;
// }
// return $fcm_message;
// }
}