File: /var/www/html/breadsecret.com/wp-content/themes/zeen-child/test_fcm.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/*
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\HttpHandler\HttpHandlerFactory;
require 'vendor/autoload.php';
$credential = new ServiceAccountCredentials(
"https://www.googleapis.com/auth/firebase.messaging",
json_decode(file_get_contents("fcm.json"), true)
);
$token = $credential->fetchAuthToken(HttpHandlerFactory::build());
$url = "https://fcm.googleapis.com/v1/projects/bsbpushnotify/messages:send";
$ch = curl_init($url);
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$token['access_token']
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{
"message": {
"topic": "newOrder",
"notification": {
"title": "Background Message Title",
"body": "Background message body"
},
"data": {
"order_id": "12345"
}
}
}');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "post");
$response = curl_exec($ch);
curl_close($ch);
echo $response;
*/
//{"message":{"topic":"newOrder","notification":{"body":"CONTENT","title":"TEST"},"data":{"message":"CONTENT","title":"TEST","id":10000}}}
function initPushNotification($title, $message, $topic, $order_id)
{
$fields = array(
'message' => array(
'topic' => $topic,
'notification' => array(
'body' => $message,
'title' => $title,
),
'data' => array(
'message' => $message,
'title' => $title,
'id' => $order_id,
)
)
);
sendPushNotification($fields);
}
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\HttpHandler\HttpHandlerFactory;
function sendPushNotification($fields = array()) {
require 'vendor/autoload.php';
$credential = new ServiceAccountCredentials(
"https://www.googleapis.com/auth/firebase.messaging",
json_decode(file_get_contents("fcm.json"), true)
);
$token = $credential->fetchAuthToken(HttpHandlerFactory::build());
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$token['access_token']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/v1/projects/bsbpushnotify/messages: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;
echo json_encode($fields);
}
initPushNotification("TEST", "CONTENT", "newOrder", "10000");
?>