NewFirebaseCaller.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. require __DIR__ . "/vendor/autoload.php";
  3. use Kreait\Firebase\Factory;
  4. use Kreait\Firebase\Messaging\Notification;
  5. use Kreait\Firebase\Messaging\CloudMessage;
  6. use Kreait\Firebase\Messaging\FcmOptions;
  7. // Connection to db
  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. // Constants
  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. $factory = (new Factory)->withServiceAccount("narkampen-firebase-adminsdk-k42j5-d3d0354e37.json");
  23. if ($messageType === "FCMNextPlayer") {
  24. $playerName = $conn->real_escape_string($_POST['playerName']);
  25. $deviceToken = getToken($conn, $gameId, $playerName);
  26. if ($deviceToken != null && $deviceToken != "") {
  27. sendMessage($title, $messageToSend, $deviceToken, "NextPlayer");
  28. }
  29. }
  30. function sendMessage($title, $messageToSend, $deviceToken, $tag = "") {
  31. $messaging = $factory->createMessaging();
  32. $notification = Notification::create($title, $messageToSend);
  33. $message = CloudMessage::withTarget(
  34. 'token' => $deviceToken);
  35. if ($tag !== "") {
  36. $fcmOptions = FcmOptions::create()->withAnalyticsLabel($tag);
  37. }
  38. $message->withNotification($notification);
  39. if ($fcmOptions != null) {
  40. $message->withFcmOptions($fcmOptions);
  41. }
  42. $messaging->send($message);
  43. }
  44. /*
  45. // NOTIFICATION EXAMPLE
  46. $title = 'My Notification Title';
  47. $body = 'My Notification Body';
  48. $imageUrl = 'http://lorempixel.com/400/200/';
  49. $notification = Notification::fromArray([
  50. 'title' => $title,
  51. 'body' => $body,
  52. 'image' => $imageUrl,
  53. ]);
  54. $notification = Notification::create($title, $body);
  55. $changedNotification = $notification
  56. ->withTitle('Changed title')
  57. ->withBody('Changed body')
  58. ->withImageUrl('http://lorempixel.com/200/400/');
  59. $message->withNotification($notification);
  60. // NOTIFICATION EXAMPLE END
  61. // DATA Example
  62. $data = [
  63. 'first key' => "First value",
  64. "second key" => "Second value"];
  65. $message->withData($data);
  66. // DATA EXAMPLE END
  67. $message = CloudMessage::withTarget('token', $deviceToken)
  68. ->withNotification($notification) // OPTIONAL
  69. ->withData($data); // OPTIONAL
  70. $message = CloudMessage::fromArray([
  71. 'token' => $deviceToken,
  72. 'notification' => [],
  73. 'data' => []
  74. ]);
  75. $messaging->send($message);
  76. */
  77. function getToken($conn, $gameId, $playerName) {
  78. $sql = "SELECT messageToken FROM `gamePlayers` INNER JOIN users ON users.id = playerId WHERE gameId = $gameId and users.username = '$playerName'";
  79. $result = $conn->query($sql);
  80. if ($result->num_rows == 1) {
  81. $returnArray = array();
  82. $data = $result->fetch_assoc();
  83. $token = $data['messageToken'];
  84. } else {
  85. echo "No games found for user";
  86. }
  87. return $token;
  88. }