Skip to content

Commit

Permalink
Merge pull request #36 from Honey-Pi/firebase-adjustments
Browse files Browse the repository at this point in the history
Adjust Firebase notification script to new API
  • Loading branch information
JavanXD authored Jan 4, 2024
2 parents d261524 + 742bff3 commit badcbb2
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions docs/push.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
<?php

header('Content-Type: text/html; charset=utf-8');

/* Access like:
http://pathto/push.php?channel=123&title=&body=Das Gewicht ist um 10kg (12%) gesunken.
Replacement Keys: https://de.mathworks.com/help/thingspeak/thinghttp-app.html#bvtzy2y-5
*/

// if params are set
if ( isset($_REQUEST['channel'])
&& is_numeric($_REQUEST['channel'])
&& (INT)$_REQUEST['channel'] > 0
&& isset($_REQUEST['body']))
{

$url = "https://fcm.googleapis.com/fcm/send";
$token = '/topics/'.$_REQUEST['channel'];
$serverKey = 'fcm key';
$title = ($_REQUEST['title']) ? filter_var($_REQUEST['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH) : 'HoneyPi';
$body = filter_var($_REQUEST['body'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$notification = array('title' => $title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
echo '<p>Erfolgreich.</p>';
} else {
echo '<p>Fehler in den Parametern. Bitte überprüfe die Parameter.</p>';
}

?>
/* Access like:
http://pathto/push.php?channel=123&title=&body=Das Gewicht ist um 10kg (12%) gesunken.
Replacement Keys: https://de.mathworks.com/help/thingspeak/thinghttp-app.html#bvtzy2y-5
*/

require __DIR__ . '/../vendor/autoload.php';

use Google\Auth\ApplicationDefaultCredentials;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

header('Content-Type: text/html; charset=utf-8');

// if params are set
if ( isset($_REQUEST['channel'])
&& is_numeric($_REQUEST['channel'])
&& (INT)$_REQUEST['channel'] > 0
&& isset($_REQUEST['body']))
{

// specify the path to the application credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/../credentials.json');

$scopes = ['https://www.googleapis.com/auth/firebase.messaging'];

$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
$stack = HandlerStack::create();
$stack->push($middleware);

$client = new Client([
'handler' => $stack,
'base_uri' => 'https://fcm.googleapis.com',
'auth' => 'google_auth' // authorize all requests
]);

$topic = $_REQUEST['channel'];
$title = ($_REQUEST['title']) ? filter_var($_REQUEST['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH) : 'HoneyPi';
$body = filter_var($_REQUEST['body'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$notification = array('title' => $title , 'body' => $body);
$message = array('topic' => $topic, 'notification' => $notification);
$requestBody = array('message' => $message);

$response = $client->post('/v1/projects/honeypi-push-notifications/messages:send', ['json' => $requestBody]);

echo '<p>Erfolgreich.</p>';
} else {
echo '<p>Fehler in den Parametern. Bitte überprüfe die Parameter.</p>';
}

?>

0 comments on commit badcbb2

Please sign in to comment.