Dashboard sipadu mbip
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

NotificationController.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Api\BaseController;
  5. use Config;
  6. use File;
  7. use Carbon\Carbon;
  8. use App\Model\Staff;
  9. use App\Model\StaffDetail;
  10. use App\Model\Module\Notification;
  11. class NotificationController extends BaseController
  12. {
  13. /**
  14. * Create save firebase token controller.
  15. *
  16. * @return json
  17. */
  18. public function saveToken(Request $request) {
  19. $staff = Staff::where('_id', $request->_id)->first();
  20. if(!empty($staff)){
  21. $staff->token_firebase = $request->token;
  22. $staff->save();
  23. return $this->sendResponse($staff,'Berjaya kemaskini firebase token');
  24. }else{
  25. return $this->sendError('','Tidak berjaya kemaskini firebase token');
  26. }
  27. }
  28. /**
  29. * Create notification controller.
  30. *
  31. * @return json
  32. */
  33. public function createNotifications($title, $msg, array $token) {
  34. $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
  35. $notification = [
  36. 'title' => $title,
  37. 'body' => $msg,
  38. 'icon' => '',
  39. 'sound' => true,
  40. ];
  41. $extraNotificationData = ["message" => $notification];
  42. $fcmNotification = [
  43. 'registration_ids' => $token, //multple token array
  44. 'notification' => $notification,
  45. 'data' => $extraNotificationData
  46. ];
  47. $headers = [
  48. 'Authorization: key=AIzaSyCJQ2HZW7dr1l1ZEcM__Ywd3fyF832eoOc',
  49. 'Content-Type: application/json'
  50. ];
  51. $ch = curl_init();
  52. curl_setopt($ch, CURLOPT_URL,$fcmUrl);
  53. curl_setopt($ch, CURLOPT_POST, true);
  54. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  57. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
  58. $response = curl_exec($ch);
  59. curl_close($ch);
  60. return $response;
  61. }
  62. public function sendNotification(Request $request){
  63. $title = $request->title;
  64. $msg = $request->msg;
  65. $register_id = $request->register_id;
  66. if($register_id != null){
  67. $result = $this->createNotifications($title, $msg, $register_id);
  68. $response = json_decode($result);
  69. foreach ($response->results as $key => $r) {
  70. if(!empty($r->error)){}
  71. else if(!empty($r->message_id)){
  72. $staff = Staff::with('StaffDetail')->where('token_firebase',$register_id[$key])->first();
  73. if(!empty($staff)){
  74. $dataNoti = [
  75. 'title' => $request->title,
  76. 'body' => $request->msg,
  77. ];
  78. $staff->notification()->create($dataNoti);
  79. }
  80. }
  81. }
  82. return $this->sendResponse($response,'Berjaya hantar notifikasi');
  83. }
  84. }
  85. public function getNotificationMobile(Request $request){
  86. $staff = Staff::with('StaffDetail','Notification')->where('api_token', $request->api_token)->first();
  87. if(!empty($staff)){
  88. $noti = $staff->Notification;
  89. return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
  90. }else{
  91. return $this->sendError('','Kakitangan tidak ditemui!');
  92. }
  93. }
  94. public function getNotificationWeb(Request $request){
  95. $staff = Staff::with('StaffDetail','Notification')->where('_id', $request->_id)->first();
  96. if(!empty($staff)){
  97. $noti = $staff->Notification;
  98. return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
  99. }else{
  100. return $this->sendError('','Kakitangan tidak ditemui!');
  101. }
  102. }
  103. public function deleteNotification(Request $request){
  104. $noti = Notification::where('_id', $request->_id)->first();
  105. if(!empty($noti)){
  106. $noti->delete();
  107. return $this->sendResponse($noti,'Berjaya buang notifikasi');
  108. }else{
  109. return $this->sendError('','Data tidak ditemui!');
  110. }
  111. }
  112. }