-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_notification.php
586 lines (479 loc) · 18.6 KB
/
send_notification.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<?php
include("header.php");
error_reporting(E_ALL);
// for permission
include_once("checkPer.php");
if($row=checkPermission($_SESSION["utype"],"notification")){ }
else{
header("location:home.php");
}
// insert data
if(isset($_REQUEST['btnsubmit']))
{
$option=$_REQUEST["option"];
$message=$_REQUEST["message"];
$user = isset($_REQUEST["user"])?$_REQUEST["user"]:"";
$userstring=implode(",",$user);
$image=false;
$res = 0;
$res1 = 0;
if ($_FILES["file1"]["name"] != "") {
$image=true;
if (file_exists("notification_img/" . $_FILES["file1"]["name"])) {
$i = 0;
$MainFileName = $_FILES["file1"]["name"];
$Arr = explode('.', $MainFileName);
$MainFileName = $Arr[0] . $i . "." . $Arr[1];
while (file_exists("notification_img/" . $MainFileName)) {
$i++;
$MainFileName = $Arr[0] . $i . "." . $Arr[1];
}
} else {
$MainFileName = $_FILES["file1"]["name"];
}
move_uploaded_file($_FILES["file1"]["tmp_name"], "notification_img/" . $MainFileName);
}
if($option == 'ios')
{
$iosuser_qry="SELECT * FROM `student_device` s1,student s2 where s1.stu_id=s2.sid and s1.device_type='ios' and s1.device_token!=''";
$iosuser_resp=$obj->select($iosuser_qry);
if(mysqli_num_rows($iosuser_resp)>0)
{
while($iosuser_row = mysqli_fetch_array($iosuser_resp))
{
$reg_ids_ios[] = $iosuser_row['device_token'];
}
$data= new stdClass();
$data->message = $message;
$data->body = $message;
if($image)
{
$data->image="notification_img/".$MainFileName;
}
$data->title = "English Express";
$title = "English Express";
$data->is_dispatch="false";
$body = $message;
//notification::send_notification($msg_payload,$ids);
echo send_notification_ios($data, $reg_ids_ios, $title, $body);
$res = 0;
}
else
{
$res = 1;
}
}
else if($option == 'android')
{
$src='android';
$user_query = "SELECT * FROM `student_device` s1,student s2 where s1.stu_id=s2.sid and s1.device_type='android' and s1.device_token!=''";
$user_result = $obj->select($user_query);
$user_num = mysqli_num_rows($user_result);
if($user_num > 0)
{
while($user_row = mysqli_fetch_array($user_result))
{
$reg_ids_android[] = $user_row['device_token'];
}
// print_r($reg_ids_android);
$data= new stdClass();
$data->message = $message;
$data->body = $message;
$data->title = "English Express";
$title = "English Express";
$data->is_dispatch="false";
if($image)
{
$data->image="notification_img/".$MainFileName;
}
$body = $message;
// notification::send_notification($msg_payload,$ids);
send_notification_android($data, $reg_ids_android, $title, $body);
$res = 0;
}else
{
$res = 1;
}
}
else if($option=="specific_user")
{
$user_qry="SELECT * FROM `student_device` s1,student s2 where s1.stu_id=s2.sid and s1.device_type='android' and s1.device_token!='' and find_in_set(s2.sid,'".$userstring."')";
$user_resp=$obj->select($user_qry);
if(mysqli_num_rows($user_resp)>0)
{
while($user_row = mysqli_fetch_array($user_resp))
{
if($user_row['type']=="ios")
{
$reg_ids_ios[] = $user_row['device_token'];
}
else
{
$reg_ids_android[] = $user_row['device_token'];
}
}
$data= new stdClass();
$data->message = $message;
$data->body = $message;
$data->title = "English Express";
$title = "English Express";
$data->is_dispatch="false";
if($image)
{
$data->image="notification_img/".$MainFileName;
}
$body = $message;
// print_r($data);
//print_r($reg_ids_android);
//notification::send_notification($msg_payload,$ids);
send_notification_ios($data, $reg_ids_ios, $title, $body);
send_notification_android($data, $reg_ids_android, $title, $body);
}
else{
$res =1;
}
}
else
{
$user_query = "SELECT * FROM `student_device` s1,student s2 where s1.stu_id=s2.sid and s1.device_token!=''";
$user_result = $obj->select($user_query);
$user_num = mysqli_num_rows($user_result);
if($user_num > 0)
{
while($user_row = mysqli_fetch_array($user_result))
{
if($user_row['device_type']=="ios")
{
$reg_ids_ios[] = $user_row['device_token'];
}
else
{
$reg_ids_android[] = $user_row['device_token'];
}
}
//print_r($reg_ids_android);
$data= new stdClass();
$data->message = $message;
$data->body = $message;
$data->title = "English Express";
$title = "English Express";
$data->is_dispatch="false";
if($image)
{
$data->image="notification_img/".$MainFileName;
}
$body = $message;
send_notification_ios($data, $reg_ids_ios, $title, $body);
send_notification_android($data, $reg_ids_android, $title, $body);
$res = 0;
}
else
{
$res = 1;
}
}
$add_noti="INSERT INTO `notification_center`( `notification_type`, `msg`, `user_ids`,`image`) VALUES ('".$option."','".$message."','".$userstring."','".$MainFileName."')";
$res_noti=$obj->select($add_noti);
if($res1 == 0)
{
header("location:send_notification.php?msg=n_success");
}else{
header("location:send_notification.php?msg=n_fail");
}
}
// fcm notification code
function send_notification_ios($data,$reg_ids,$title,$body)
{
//$reg_ids[0]="esR5GsVCeEBljF0hszij-k:APA91bEq7A2QCl6Rrt8-__t7OlUemcQOIy_KRe0Zm6h50b8ffZcciHDdnT8f9poGAiW6gcqywi438TWt_aOLN0yk7YKgbOakkvrmTlvVUEtr98aiz69BsgoACxfHXztRmFx-0HprNxLy";
$url='https://fcm.googleapis.com/fcm/send';
$api_key='AAAA5lYuOAA:APA91bEImlO4QpQYgwUluphC4Di-qhr9q_E6b9ZvtSvwoSxA9N5CN0LbnOiexpnY8hih5XBUW84wiEeHj_MGBcJR8o9BfkEK9FXVizGwIfMir-NEvZ3_IgjL1Eu8ylZdkoVAKCYt174K';
$msg = array(
'title' =>$title,
'body' => $body,
'icon' => 'myicon',
'sound' => 'mySound',
'data' => $data
);
$fields = array(
'registration_ids' => $reg_ids,
'notification' => $msg
);
//print_r($fields);
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$api_key
);
// echo json_encode($fields);
$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('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
// echo $result;
return $result;
}
// fcm notificaton for android
function send_notification_android($data,$reg_ids_android,$title,$body)
{
//echo "in notif";
//print_r($reg_ids_android);
$url='https://fcm.googleapis.com/fcm/send';
$api_key='AAAA5lYuOAA:APA91bEImlO4QpQYgwUluphC4Di-qhr9q_E6b9ZvtSvwoSxA9N5CN0LbnOiexpnY8hih5XBUW84wiEeHj_MGBcJR8o9BfkEK9FXVizGwIfMir-NEvZ3_IgjL1Eu8ylZdkoVAKCYt174K';
$msg = array(
'title' =>$title,
'body' => $body,
'icon' => 'myicon',
'sound' => 'mySound',
'data' => $data
);
$fields = array(
'registration_ids' => $reg_ids_android,
'data' => $data,
);
//print_r($fields);
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$api_key
);
echo json_encode($fields);
$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('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
// echo $result;
return $result;
}
?>
<h4 class="fw-bold py-3 mb-4">Notification Center</h4>
<?php
if(isset($_COOKIE["msg"]) )
{
if($_COOKIE['msg']=="data")
{
?>
<div class="alert alert-primary alert-dismissible" role="alert">
Data added succesfully
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
</button>
</div>
<script type="text/javascript">eraseCookie("msg")</script>
<?php
}
if($_COOKIE['msg']=="update")
{
?>
<div class="alert alert-primary alert-dismissible" role="alert">
Data updated succesfully
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
</button>
</div>
<script type="text/javascript">eraseCookie("msg")</script>
<?php
}
if($_COOKIE['msg']=="data_del")
{
?>
<div class="alert alert-primary alert-dismissible" role="alert">
Data deleted succesfully
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
</button>
</div>
<script type="text/javascript">eraseCookie("msg")</script>
<?php
}
if($_COOKIE['msg']=="fail")
{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
An error occured! Try again.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
</button>
</div>
<script type="text/javascript">eraseCookie("msg")</script>
<?php
}
}
if(isset($_COOKIE["sql_error"]))
{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<?php echo urldecode($_COOKIE['sql_error'])?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
</button>
</div>
<script type="text/javascript">eraseCookie("sql_error")</script>
<?php
}
?>
<?php if($row["write_func"]=="y" || $row["upd_func"]=="y" || $row["read_func"]=="y"){ ?>
<!-- Basic Layout -->
<div class="row">
<div class="col-xl">
<div class="card mb-4">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">Send Notification</h5>
</div>
<div class="card-body">
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="ttId" id="ttId">
<div class="mb-3">
<label class="form-label" for="basic-default-fullname">To</label>
<div class="form-check form-check-inline mt-3">
<input type="radio" value="all" name="option" id="all" checked onclick="show_users()">
<label class="form-check-label" for="inlineRadio1">All</label>
</div>
<div class="form-check form-check-inline mt-3">
<input type="radio" value="ios" name="option" id="ios" onclick="show_users()">
<label class="form-check-label" for="inlineRadio1">iOS</label>
</div>
<div class="form-check form-check-inline mt-3">
<input type="radio" value="android" name="option" id="android" onclick="show_users()">
<label class="form-check-label" for="inlineRadio1">Android</label>
</div>
<div class="form-check form-check-inline mt-3">
<input type="radio" value="specific_user" name="option" id="setuser" onclick="show_users()">
<label class="form-check-label" for="inlineRadio1">Specific users</label>
</div>
</div>
<div class="mb-3" id="users_div" style="display: none">
<label class="form-label" for="basic-default-fullname">Students</label>
<select id="user" name="user[]" class="form-control select2-multiple" multiple>
<?php
$user_qry="select * from student";
$res_qry=$obj->select($user_qry);
while ($users=mysqli_fetch_array($res_qry))
{
?>
<option value="<?php echo $users["sid"]?>"><?php echo $users["name"]?></option>
<?php
}
?>
</select>
</div>
<div class="mb-3">
<label class="form-label" for="basic-default-fullname">Image</label>
<input id="file1" name="file1" type="file" onchange="readURL(this);"/>
<img id='PreviewImage' name="PreviewImage" src="" height="100" width="120" hidden="true">
</div>
<div class="mb-3">
<label class="form-label" for="basic-default-fullname">Message</label>
<textarea class=" form-control" name="message" id="message" rows="4" required ></textarea>
</div>
<?php if($row["write_func"]=="y"){ ?>
<button type="submit" name="btnsubmit" id="btnsubmit" class="btn btn-primary">Save</button>
<?php } ?>
<button type="reset" name="btncancel" id="btncancel" class="btn btn-secondary" onclick="window.location.reload()">Cancel</button>
</form>
</div>
</div>
</div>
</div>
<?php } ?>
<!-- grid -->
<!-- Basic Bootstrap Table -->
<div class="card">
<h5 class="card-header">Exercise Records</h5>
<div class="table-responsive text-nowrap">
<table class="table" id="table_id">
<thead>
<tr>
<th>Serial No.</th>
<th>Notification Type</th>
<th>Message</th>
<th>Image</th>
<th>Date Time</th>
</tr>
</thead>
<tbody class="table-border-bottom-0">
<?php
$stmt_list = $obj->con1->prepare("select *,DATE_FORMAT(date_time, '%d-%m-%Y %H:%i:%s') as ndt from notification_center order by id desc");
$stmt_list->execute();
$result = $stmt_list->get_result();
$stmt_list->close();
$i=1;
while($notification=mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $i?></td>
<td> <?php echo $notification["notification_type"]?></td>
<td> <?php echo $notification["msg"]?></td>
<?php
if($notification["image"]!="")
{
$image="notification_img/".$notification["image"];
}
else
{
$image="-";
}
?>
<td><img src="<?php echo $image?>" width="80" height="80"></td>
<td><?php echo $notification["ndt"] ?></td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
</div>
</div>
<!--/ Basic Bootstrap Table -->
<!-- / grid -->
<!-- / Content -->
<script type="text/javascript">
function show_users() {
if($('#setuser').prop("checked"))
{
$('#users_div').show();
}
else
{
$('#users_div').hide();
}
}
function resendnotification(msg) {
$('#message').val(msg);
}
function readURL(input) {
var filePath = input.value;
var allowedExtensions = /(\.jpg|\.jpeg|\.png|\.gif)$/i;
if(!allowedExtensions.exec(filePath)){
alert('Please upload Image only.');
fileInput.value = '';
return false;
}
else
{
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#PreviewImage').attr('src', e.target.result);
document.getElementById("PreviewImage").hidden = false;
};
reader.readAsDataURL(input.files[0]);
}
}
}
</script>
<?php
include("footer.php");
?>