FCMNextPlayer.php 1.8 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. /*
  9. $token = "eMvkzPwkTheSeFLFAE5fho:APA91bE-pb5_0xoMn_Bza6-HrgpI5ufKPZ7zhF4NM_leYdfgYTk4MZJdfpfiKDkijUuQAiK7iaT7rXJucF20uh1GvmgjiomiuQehnDHcjUkGjaSvkQtVfj_s0AtLSP>
  10. */
  11. $hostname = 'localhost';
  12. $username = 'narKampen';
  13. $pass = 'IfRLzj2HJBXA9eei';
  14. $database = 'narKampen';
  15. $conn = new mysqli($hostname, $username, $pass, $database);
  16. if (!$conn) {
  17. die("Connection Failed. ". mysqli_connect_error());
  18. }
  19. mysqli_set_charset($conn,'utf8');
  20. $gameId = $conn->real_escape_string($_POST['gameId']);
  21. $playerName = $conn->real_escape_string($_POST['playerName']);
  22. $sql = "SELECT messageToken FROM `gamePlayers` INNER JOIN users ON users.id = playerId WHERE gameId = $gameId and users.username = '$playerName'";
  23. $result = $conn->query($sql);
  24. if ($result->num_rows == 1) {
  25. $returnArray = array();
  26. $data = $result->fetch_assoc();
  27. $token = $data['messageToken'];
  28. } else {
  29. echo "No games found for user";
  30. }
  31. if ($token != null && $token != "") {
  32. // Your Firebase project ID
  33. $project = "narkampen";
  34. // Creates a notification for subscribers to the debug topic
  35. $message = [
  36. "message" => [
  37. "token" => $token,
  38. "notification" => [
  39. "body" => "This is an FCM notification message!",
  40. "title" => "FCM Message",
  41. ]
  42. ]
  43. ];
  44. // Send the Push Notification - use $response to inspect success or errors
  45. $response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
  46. }