FCMNextPlayer.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. require_once '../vendor/autoload.php';
  3. putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/axel/configs/narkampen-firebase-adminsdk-k42j5-d3d0354e37.json');
  4. $client = new Google_Client();
  5. $client->useApplicationDefaultCredentials();
  6. $client->addScope('https://www.googleapis.com/auth/firebase.messaging');
  7. $httpClient = $client->authorize();
  8. $hostname = 'localhost';
  9. $username = 'narKampen';
  10. $pass = 'IfRLzj2HJBXA9eei';
  11. $database = 'narKampen';
  12. $conn = new mysqli($hostname, $username, $pass, $database);
  13. if (!$conn) {
  14. die("Connection Failed. ". mysqli_connect_error());
  15. }
  16. mysqli_set_charset($conn,'utf8');
  17. $gameId = $conn->real_escape_string($_POST['gameId']);
  18. $playerName = $conn->real_escape_string($_POST['playerName']);
  19. $title = $conn->real_escape_string($_POST['title']);
  20. $message = $conn->real_escape_string($_POST['message']);
  21. $sql = "SELECT messageToken FROM `gamePlayers` INNER JOIN users ON users.id = playerId WHERE gameId = $gameId and users.username = '$playerName'";
  22. $result = $conn->query($sql);
  23. if ($result->num_rows == 1) {
  24. $returnArray = array();
  25. $data = $result->fetch_assoc();
  26. $token = $data['messageToken'];
  27. } else {
  28. echo "No games found for user";
  29. }
  30. if ($token != null && $token != "") {
  31. // Your Firebase project ID
  32. $project = "narkampen";
  33. // Creates a notification for subscribers to the debug topic
  34. $message = [
  35. "message" => [
  36. "token" => $token,
  37. "notification" => [
  38. "body" => $message,
  39. "title" => $title,
  40. ]
  41. ]
  42. ];
  43. // Send the Push Notification - use $response to inspect success or errors
  44. $response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
  45. }