FCMMessageing.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /* FCM - Messageing */
  3. require_once '../vendor/autoload.php';
  4. putenv('GOOGLE_APPLICATION_CREDENTIALS=/home/axel/configs/narkampen-firebase-adminsdk-k42j5-d3d0354e37.json');
  5. $client = new Google_Client();
  6. $client->useApplicationDefaultCredentials();
  7. $client->addScope('https://www.googleapis.com/auth/firebase.messaging');
  8. $httpClient = $client->authorize();
  9. $hostname = 'localhost';
  10. $username = 'narKampen';
  11. $pass = 'IfRLzj2HJBXA9eei';
  12. $database = 'narKampen';
  13. $conn = new mysqli($hostname, $username, $pass, $database);
  14. if (!$conn) {
  15. die("Connection Failed. ". mysqli_connect_error());
  16. }
  17. mysqli_set_charset($conn,'utf8');
  18. $messageType = $conn->real_escape_string(isset($_POST['type'])?$_POST['type']:"");
  19. $title = $conn->real_escape_string($_POST['title']);
  20. $messageToSend = $conn->real_escape_string($_POST['message']);
  21. $gameId = $conn->real_escape_string($_POST['gameId']);
  22. file_put_contents("FCMMessageDebug.log", "Called $messageType at " . Date(), FILE_APPEND);
  23. if ($messageType === "FCMNextPlayer") {
  24. $playerName = $conn->real_escape_string($_POST['playerName']);
  25. $token = getToken($conn, $gameId, $playerName);
  26. if ($token != null && $token != "") {
  27. sendMessage($httpClient, $token, $title, $messageToSend, "NextPlayer");
  28. }
  29. } else if ($messageType === "InviteMessage") {
  30. $i = 0;
  31. while ($_POST['player' . $i] != null) {
  32. $token = getToken($conn, $gameId, $conn->real_escape_string($_POST['player' . $i]));
  33. sendMessage($httpClient, $token, $title, $messageToSend, "GameInvite");
  34. }
  35. } else if ($messageType === "gameFinishedMessage") {
  36. $i = 0;
  37. while ($_POST['player' . $i] != null) {
  38. $token = getToken($conn, $gameId, $conn->real_escape_string($_POST['player' . $i]));
  39. sendMessage($httpClient, $token, $title, $messageToSend, "GameFinished");
  40. }
  41. }
  42. function getToken($conn, $gameId, $playerName) {
  43. $sql = "SELECT messageToken FROM `gamePlayers` INNER JOIN users ON users.id = playerId WHERE gameId = $gameId and users.username = '$playerName'";
  44. $result = $conn->query($sql);
  45. if ($result->num_rows == 1) {
  46. $returnArray = array();
  47. $data = $result->fetch_assoc();
  48. $token = $data['messageToken'];
  49. } else {
  50. echo "No games found for user";
  51. }
  52. return $token;
  53. }
  54. function sendMessage($httpClient, $token, $title, $messageToSend, $tag) {
  55. // Your Firebase project ID
  56. $project = "narkampen";
  57. // Creates a notification for subscribers to the debug topic
  58. $message = [
  59. "message" => [
  60. "token" => $token,
  61. "notification" => [
  62. "body" => $messageToSend,
  63. "title" => $title,
  64. "tag" => $tag,
  65. ]
  66. ]
  67. ];
  68. // Send the Push Notification - use $response to inspect success or errors
  69. $response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
  70. }