|
@@ -3,13 +3,59 @@
|
|
|
|
|
|
|
|
use Kreait\Firebase\Factory;
|
|
use Kreait\Firebase\Factory;
|
|
|
use Kreait\Firebase\Messaging\Notification;
|
|
use Kreait\Firebase\Messaging\Notification;
|
|
|
|
|
+ use Kreait\Firebase\Messaging\CloudMessage;
|
|
|
|
|
+ use Kreait\Firebase\Messaging\FcmOptions;
|
|
|
|
|
|
|
|
|
|
+// Connection to db
|
|
|
|
|
+ $hostname = 'localhost';
|
|
|
|
|
+ $username = 'narKampen';
|
|
|
|
|
+ $pass = 'IfRLzj2HJBXA9eei';
|
|
|
|
|
+ $database = 'narKampen';
|
|
|
|
|
+
|
|
|
|
|
+ $conn = new mysqli($hostname, $username, $pass, $database);
|
|
|
|
|
+ if (!$conn) {
|
|
|
|
|
+ die("Connection Failed. ". mysqli_connect_error());
|
|
|
|
|
+ }
|
|
|
|
|
+ mysqli_set_charset($conn,'utf8');
|
|
|
|
|
+
|
|
|
|
|
+ // Constants
|
|
|
|
|
+ $messageType = $conn->real_escape_string(isset($_POST['type'])?$_POST['type']:"");
|
|
|
|
|
+ $title = $conn->real_escape_string($_POST['title']);
|
|
|
|
|
+ $messageToSend = $conn->real_escape_string($_POST['message']);
|
|
|
|
|
+ $gameId = $conn->real_escape_string($_POST['gameId']);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
$factory = (new Factory)->withServiceAccount("narkampen-firebase-adminsdk-k42j5-d3d0354e37.json");
|
|
$factory = (new Factory)->withServiceAccount("narkampen-firebase-adminsdk-k42j5-d3d0354e37.json");
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- $messaging = $factory->createMessaging();
|
|
|
|
|
|
|
+ if ($messageType === "FCMNextPlayer") {
|
|
|
|
|
+ $playerName = $conn->real_escape_string($_POST['playerName']);
|
|
|
|
|
+ $deviceToken = getToken($conn, $gameId, $playerName);
|
|
|
|
|
+
|
|
|
|
|
+ if ($deviceToken != null && $deviceToken != "") {
|
|
|
|
|
+ sendMessage($title, $messageToSend, $deviceToken, "NextPlayer");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $deviceToken = "ck4OuDu3RweBW5s8ncj7FV:APA91bHo58TUsXVgTB8cLOKHkpVaSydej_TFZLJyuPMrAEhgWNxLRRd83a_6Mz1YnZYtzz5sTjnowlbYVwyp3JudKa-jl3UY1xNGae9FpYU7bn6hSHLzW-OuzM87w149lQg6zYC6PXJg";
|
|
|
|
|
|
|
+ function sendMessage($title, $messageToSend, $deviceToken, $tag = "") {
|
|
|
|
|
+ $messaging = $factory->createMessaging();
|
|
|
|
|
+ $notification = Notification::create($title, $messageToSend);
|
|
|
|
|
+ $message = CloudMessage::withTarget(
|
|
|
|
|
+ 'token' => $deviceToken);
|
|
|
|
|
+
|
|
|
|
|
+ if ($tag !== "") {
|
|
|
|
|
+ $fcmOptions = FcmOptions::create()->withAnalyticsLabel($tag);
|
|
|
|
|
+ }
|
|
|
|
|
+ $message->withNotification($notification);
|
|
|
|
|
+
|
|
|
|
|
+ if ($fcmOptions != null) {
|
|
|
|
|
+ $message->withFcmOptions($fcmOptions);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $messaging->send($message);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ /*
|
|
|
// NOTIFICATION EXAMPLE
|
|
// NOTIFICATION EXAMPLE
|
|
|
$title = 'My Notification Title';
|
|
$title = 'My Notification Title';
|
|
|
$body = 'My Notification Body';
|
|
$body = 'My Notification Body';
|
|
@@ -43,12 +89,28 @@
|
|
|
->withNotification($notification) // OPTIONAL
|
|
->withNotification($notification) // OPTIONAL
|
|
|
->withData($data); // OPTIONAL
|
|
->withData($data); // OPTIONAL
|
|
|
|
|
|
|
|
- /*
|
|
|
|
|
|
|
+
|
|
|
$message = CloudMessage::fromArray([
|
|
$message = CloudMessage::fromArray([
|
|
|
'token' => $deviceToken,
|
|
'token' => $deviceToken,
|
|
|
'notification' => [],
|
|
'notification' => [],
|
|
|
'data' => []
|
|
'data' => []
|
|
|
]);
|
|
]);
|
|
|
- */
|
|
|
|
|
|
|
|
|
|
- $messaging->send($message);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $messaging->send($message);
|
|
|
|
|
+
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+ function getToken($conn, $gameId, $playerName) {
|
|
|
|
|
+ $sql = "SELECT messageToken FROM `gamePlayers` INNER JOIN users ON users.id = playerId WHERE gameId = $gameId and users.username = '$playerName'";
|
|
|
|
|
+
|
|
|
|
|
+ $result = $conn->query($sql);
|
|
|
|
|
+ if ($result->num_rows == 1) {
|
|
|
|
|
+ $returnArray = array();
|
|
|
|
|
+ $data = $result->fetch_assoc();
|
|
|
|
|
+ $token = $data['messageToken'];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ echo "No games found for user";
|
|
|
|
|
+ }
|
|
|
|
|
+ return $token;
|
|
|
|
|
+ }
|