FCMMessageing.php 2.7 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. if ($messageType === "FCMNextPlayer") {
  23. $playerName = $conn->real_escape_string($_POST['playerName']);
  24. $token = getToken($conn, $gameId, $playerName);
  25. if ($token != null && $token != "") {
  26. sendMessage($httpClient, $token, $title, $messageToSend, "NextPlayer");
  27. }
  28. } else if ($messageType === "InviteMessage") {
  29. $i = 0;
  30. while ($_POST['player' . $i] != null) {
  31. $token = getToken($conn, $gameId, $conn->real_escape_string($_POST['player' . $i]));
  32. sendMessage($httpClient, $token, $title, $messageToSend, "GameInvite");
  33. }
  34. } else if ($messageType === "gameFinishedMessage") {
  35. $i = 0;
  36. while ($_POST['player' . $i] != null) {
  37. $token = getToken($conn, $gameId, $conn->real_escape_string($_POST['player' . $i]));
  38. sendMessage($httpClient, $token, $title, $messageToSend, "GameFinished");
  39. }
  40. }
  41. function getToken($conn, $gameId, $playerName) {
  42. $sql = "SELECT messageToken FROM `gamePlayers` INNER JOIN users ON users.id = playerId WHERE gameId = $gameId and users.username = '$playerName'";
  43. $result = $conn->query($sql);
  44. if ($result->num_rows == 1) {
  45. $returnArray = array();
  46. $data = $result->fetch_assoc();
  47. $token = $data['messageToken'];
  48. } else {
  49. echo "No games found for user";
  50. }
  51. return $token;
  52. }
  53. function sendMessage($httpClient, $token, $title, $messageToSend, $tag) {
  54. // Your Firebase project ID
  55. $project = "narkampen";
  56. // Creates a notification for subscribers to the debug topic
  57. $message = [
  58. "message" => [
  59. "token" => $token,
  60. "notification" => [
  61. "body" => $messageToSend,
  62. "title" => $title,
  63. "tag" => $tag,
  64. ]
  65. ]
  66. ];
  67. // Send the Push Notification - use $response to inspect success or errors
  68. $response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
  69. }