NewFirebaseCaller.php 3.2 KB

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