Dashboard sipadu mbip
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

NotificationController.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, $data, array $token) {
  34. $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
  35. $notification = [
  36. 'title' => $title,
  37. 'body' => $msg,
  38. 'icon' => '',
  39. 'sound' => true,
  40. 'data' => $data,
  41. ];
  42. $extraNotificationData = ["message" => $notification];
  43. $fcmNotification = [
  44. 'registration_ids' => $token, //multple token array
  45. 'notification' => $notification,
  46. 'data' => $extraNotificationData
  47. ];
  48. $headers = [
  49. 'Authorization: key=AAAAuCqc038:APA91bF0lSnEURlAscOjD6dRfPDaGnjH-jsePM6-AXcQTytN486RaM_zL8WoqDGTYnRZTeAh6nI8XFcQ9n293KJNfTKvdiNg_cOgnQN_4Yxqb0uK9hou8cmZuSMQgIw-QQl8Jl0WV51L',
  50. 'Content-Type: application/json'
  51. ];
  52. $ch = curl_init();
  53. curl_setopt($ch, CURLOPT_URL,$fcmUrl);
  54. curl_setopt($ch, CURLOPT_POST, true);
  55. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  56. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  58. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
  59. $response = curl_exec($ch);
  60. curl_close($ch);
  61. return $response;
  62. }
  63. public function sendNotification(Request $request){
  64. $title = $request->title;
  65. $msg = $request->msg;
  66. $data = $request->data;
  67. $register_id = $request->register_id;
  68. // info($data);
  69. if($register_id != null){
  70. $result = $this->createNotifications($title, $msg, $data, $register_id);
  71. $response = json_decode($result);
  72. foreach ($response->results as $key => $r) {
  73. if(!empty($r->error)){}
  74. else if(!empty($r->message_id)){
  75. $staff = Staff::with('StaffDetail')->where('token_firebase',$register_id[$key])->first();
  76. if(!empty($staff)){
  77. $dataNoti = [
  78. 'title' => $title,
  79. 'body' => $msg,
  80. 'data' => $data,
  81. ];
  82. $staff->notification()->create($dataNoti);
  83. }
  84. }
  85. }
  86. return $this->sendResponse($response,'Berjaya hantar notifikasi');
  87. }
  88. }
  89. public function getNotificationMobile(Request $request){
  90. $staff = Staff::with('StaffDetail','Notification')->where('api_token', $request->api_token)->first();
  91. if(!empty($staff)){
  92. $noti = $staff->Notification;
  93. return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
  94. }else{
  95. return $this->sendError('','Kakitangan tidak ditemui!');
  96. }
  97. }
  98. public function getNotificationWeb(Request $request){
  99. $staff = Staff::with('StaffDetail','Notification')->where('_id', $request->_id)->first();
  100. if(!empty($staff)){
  101. $noti = $staff->Notification;
  102. return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
  103. }else{
  104. return $this->sendError('','Kakitangan tidak ditemui!');
  105. }
  106. }
  107. public function deleteNotification(Request $request){
  108. $noti = Notification::where('_id', $request->_id)->first();
  109. if(!empty($noti)){
  110. $noti->delete();
  111. return $this->sendResponse($noti,'Berjaya buang notifikasi');
  112. }else{
  113. return $this->sendError('','Data tidak ditemui!');
  114. }
  115. }
  116. }