/
internalgcmaccess.php
104 lines (93 loc) · 2.68 KB
/
internalgcmaccess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
require 'db_ops.php';
require 'vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__, '.env');
$dotenv->load();
header('Content-Type: application/json');
function sendPushNotificationToGCM($registration_ids, $message) {
$GCM_SERVER_API_KEY = $_ENV["GCM_SERVER_API_KEY"];
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
// Update your Google Cloud Messaging API Key
if (!defined('GOOGLE_API_KEY')) {
define("GOOGLE_API_KEY", $GCM_SERVER_API_KEY);
}
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$db = new DB;
if (isset($_POST['message']) && isset($_POST['title']) && isset($_POST['cluster']) && isset($_POST['event_name']) && isset($_POST['type']) && isset($_POST['internalgcmpin'])) {
$msg = $_POST['message'];
$title = $_POST['title'];
$cluster = $_POST['cluster'];
$type = $_POST['type'];
$event = $_POST['event_name'];
$gcmpin = $_POST['internalgcmpin'];
if($gcmpin == $_ENV["INTERNAL_GCM_PIN"])
{
$chunk_size = 300;
$result = $db->get_all($_ENV["GCM_TABLE"]);
$reg_ids = array();
while($row = mysql_fetch_assoc($result))
{
array_push($reg_ids, $row['gcm_id']);
}
$size = sizeof($reg_ids);
$msg_arr = array("text" => $msg, "title" => $title, "cluster" => $cluster, "type"=>$type, "event" => $event);
$reg_id_chunks = array_chunk($reg_ids, $chunk_size);
$db = new DB;
$db->insert($_ENV["MESSAGE_TABLE"],array(
"message" => $msg,
"title" => $title,
"type" => $type,
"cluster" => $cluster,
"event" => $event,
"sent_to" => $size
));
for($i=0;$i<sizeof($reg_id_chunks);$i++)
{
sendPushNotificationToGCM($reg_id_chunks[$i], $msg_arr);
}
$response = array (
"status_code" => 200,
"description" => "Success"
);
echo json_encode($response);
}
else
{
$response = array (
"status_code" => 102,
"description" => "Incorrect Pin"
);
echo json_encode($response);
}
}
else
{
$response = array(
"status_code" => 101,
"description" => "Incorrect parameters"
);
echo json_encode($response);
}
?>