-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
292 lines (256 loc) · 12.9 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>
<?php
require_once __DIR__ . '/include/db_handler.php';
$demo = new DbHandler();
$admin_id = $demo->getAdminUser();
?>
<html>
<head>
<title>Google Cloud Messaging 3.0 | AndroidHive</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='https://fonts.googleapis.com/css?family=Raleway:400,800,100' rel='stylesheet' type='text/css'>
<link href='style.css' rel='stylesheet' type='text/css'>
<link href='http://api.androidhive.info/gcm/styles/default.css' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript">
var user_id = '<?= $admin_id ?>';
$(document).ready(function () {
getChatroomMessages($('#topics li:first').attr('id'));
$('ul#topics li').on('click', function () {
$('ul#topics li').removeClass('selected');
$(this).addClass('selected');
getChatroomMessages($(this).prop('id'))
});
function getChatroomMessages(id) {
$.getJSON("v1/chat_rooms/" + id, function (data) {
var li = '';
$.each(data.messages, function (i, message) {
li += '<li class="others"><label class="name">' + message.user.username + '</label><div class="message">' + message.message + '</div><div class="clear"></div></li>';
});
$('ul#topic_messages').html(li);
if (data.messages.length > 0) {
scrollToBottom('msg_container_topic');
}
}).done(function () {
}).fail(function () {
alert('Sorry! Unable to fetch topic messages');
}).always(function () {
});
// attaching the chatroom id to send button
$('#send_to_topic').attr('chat_room', id);
}
$('#send_to_topic').on('click', function () {
var msg = $('#send_to_topic_message').val();
if (msg.trim().length === 0) {
alert('Enter a message');
return;
}
$('#send_to_topic_message').val('');
$('#loader_topic').show();
$.post("v1/chat_rooms/" + $(this).attr('chat_room') + '/message',
{user_id: user_id, message: msg},
function (data) {
if (data.error === false) {
var li = '<li class="self" tabindex="1"><label class="name">' + data.user.name + '</label><div class="message">' + data.message.message + '</div><div class="clear"></div></li>';
$('ul#topic_messages').append(li);
scrollToBottom('msg_container_topic');
} else {
alert('Sorry! Unable to send message');
}
}).done(function () {
}).fail(function () {
alert('Sorry! Unable to send message');
}).always(function () {
$('#loader_topic').hide();
});
});
$('input#send_to_single_user').on('click', function () {
var msg = $('#send_to_single').val();
var to = $('.select_single').val();
if (msg.trim().length === 0) {
alert('Enter a message');
return;
}
$('#send_to_single').val('');
$('#loader_single').show();
$.post("v1/private_chats/" + to + '/message',
{user_id: user_id, message: msg},
function (data) {
if (data.error === false) {
$('#loader_single').hide();
alert('Push notification sent successfully! You should see a Toast message on device.');
} else {
alert('Sorry! Unable to post message');
}
}).done(function () {
}).fail(function () {
alert('Sorry! Unable to send message');
}).always(function () {
$('#loader_single').hide();
});
});
$('input#send_to_multiple_users').on('click', function () {
var msg = $('#send_to_multiple').val();
var to = $('.select_multiple').val();
if (to === null) {
alert("Please select the users!");
return;
}
if (msg.trim().length === 0) {
alert('Enter a message');
return;
}
$('#send_to_multiple').val('');
$('#loader_multiple').show();
var selMulti = $.map($(".select_multiple option:selected"), function (el, i) {
return $(el).val();
});
to = selMulti.join(",");
$.post("v1/users/message",
{user_id: user_id, to: to, message: msg},
function (data) {
if (data.error === false) {
$('#loader_multiple').hide();
alert('Push notification sent successfully to multiple users');
} else {
alert('Sorry! Unable to send message');
}
}).done(function () {
}).fail(function () {
alert('Sorry! Unable to send message');
}).always(function () {
$('#loader_multiple').hide();
});
});
$('input#send_to_multiple_users_with_image').on('click', function () {
var msg = $('#send_to_multiple_with_image').val();
if (msg.trim().length === 0) {
alert('Enter a message');
return;
}
$('#send_to_multiple_with_image').val('');
$('#loader_multiple_with_image').show();
$.post("v1/users/send_to_all",
{user_id: user_id, message: msg},
function (data) {
if (data.error === false) {
$('#loader_multiple_with_image').hide();
alert('Push notification sent successfully to multiple users');
} else {
alert('Sorry! Unable to send message');
}
}).done(function () {
}).fail(function () {
alert('Sorry! Unable to send message');
}).always(function () {
$('#loader_topic_with_image').hide();
});
});
function scrollToBottom(cls) {
$('.' + cls).scrollTop($('.' + cls + ' ul li').last().position().top + $('.' + cls + ' ul li').last().height());
}
});
</script>
</head>
<body>
<div class="header">
<label class="logo">ANDROIDHIVE</label>
<h2>Google Cloud Messaging</h2>
<h2 class="small">Sending push notifications using Android, PHP & MySQL</h2>
</div>
<div class="container_body">
<div class="topics">
<h2 class="heading">Download & Install the GCM apk</h2>
Download & Install the Google Cloud Messaging <a href="#">apk</a> before trying the demos. <br/><br/>Once installed, refresh this page
to see your name, email in the recipients list.
</div>
<div class="topics">
<div class="separator"></div>
<h2 class="heading">Sending message to a `topic`</h2>
Select any of the topics below and send a message.<br/><br/>
<div class="box">
<div class="usr_container">
<ul id="topics">
<?php
$chatrooms = $demo->getAllChatRooms();
foreach ($chatrooms as $key => $chatroom) {
$cls = $key == 0 ? 'selected' : '';
?>
<li id="<?= $chatroom['chat_room_id'] ?>" class="<?= $cls ?>">
<label><?= $chatroom['name'] ?></label>
<span>topic_<?= $chatroom['chat_room_id'] ?></span>
</li>
<?php
}
?>
</ul>
</div>
<div class="msg_container msg_container_topic">
<ul id="topic_messages"></ul>
</div>
<div class="send_container">
<textarea placeholder="Type a message here" id="send_to_topic_message"></textarea>
<input id="send_to_topic" type="button" value="Send to Topic"/>
<img src="loader.gif" id="loader_topic" class="loader"/>
</div>
<div class="clear"></div>
</div>
<br/>
<div class="separator"></div>
<h2 class="heading">Sending message to `Single User`</h2>
Select your name from the below recipients and send a message<br/><br/>
<div class="container">
<select class="select_single">
<?php
$users = $demo->getAllUsers();
foreach ($users as $key => $user) {
?>
<option value="<?= $user['user_id'] ?>"><?= $user['name'] ?> (<?= $user['email'] ?>)</option>
<?php
}
?>
</select><br/>
<textarea id="send_to_single" class="textarea_msg" placeholder="Type a message"></textarea><br/>
<input id="send_to_single_user" type="button" value="Send to single user" class="btn_send"/>
<img src="loader.gif" id="loader_single" class="loader"/>
</div>
<br/>
<div class="separator"></div>
<h2 class="heading">Sending message to `Multiple Users`</h2>
Select multiple recipients and send a message. You can use ctrl or shift to select multiple users<br/><br/><br/>
<div class="container">
<select multiple class="select_multiple">
<?php
foreach ($users as $key => $user) {
?>
<option value="<?= $user['user_id'] ?>"><?= $user['name'] ?> (<?= $user['email'] ?>)</option>
<?php
}
?>
</select>
<br/>
<textarea id="send_to_multiple" class="textarea_msg" placeholder="Type a message"></textarea><br/>
<input id="send_to_multiple_users" type="button" value="Send to multiple users" class="btn_send"/>
<img src="loader.gif" id="loader_multiple" class="loader"/>
</div>
<br/>
<div class="separator"></div>
<h2 class="heading">Sending push notification with an `Image`</h2>
A message with an image attachment will be sent to every user. You have to minimize or close the app in order to see
it in action.<br/><br/>
<div class="container">
<textarea id="send_to_multiple_with_image" class="textarea_msg" placeholder="Type a message"></textarea><br/>
<input id="send_to_multiple_users_with_image" type="button" value="Send with image" class="btn_send"/>
<img src="loader.gif" id="loader_multiple_with_image" class="loader"/>
</div>
</div>
<br/><br/>
<br/><br/>
<br/><br/>
</div>
</body>
</html>