Browse Source

key

master
Siti Rahayu 4 years ago
parent
commit
5b9599d690
1 changed files with 137 additions and 137 deletions
  1. 137
    137
      app/Http/Controllers/NotificationController.php

+ 137
- 137
app/Http/Controllers/NotificationController.php View File

@@ -1,137 +1,137 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Api\BaseController;
use Config;
use File;
use Carbon\Carbon;
use App\Model\Staff;
use App\Model\StaffDetail;
use App\Model\Module\Notification;
class NotificationController extends BaseController
{
/**
* Create save firebase token controller.
*
* @return json
*/
public function saveToken(Request $request) {
$staff = Staff::where('_id', $request->_id)->first();
if(!empty($staff)){
$staff->token_firebase = $request->token;
$staff->save();
return $this->sendResponse($staff,'Berjaya kemaskini firebase token');
}else{
return $this->sendError('','Tidak berjaya kemaskini firebase token');
}
}
/**
* Create notification controller.
*
* @return json
*/
public function createNotifications($title, $msg, array $token) {
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$notification = [
'title' => $title,
'body' => $msg,
'icon' => '',
'sound' => true,
];
$extraNotificationData = ["message" => $notification];
$fcmNotification = [
'registration_ids' => $token, //multple token array
'notification' => $notification,
'data' => $extraNotificationData
];
$headers = [
'Authorization: key=AIzaSyCJQ2HZW7dr1l1ZEcM__Ywd3fyF832eoOc',
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function sendNotification(Request $request){
$title = $request->title;
$msg = $request->msg;
$register_id = $request->register_id;
if($register_id != null){
$result = $this->createNotifications($title, $msg, $register_id);
$response = json_decode($result);
foreach ($response->results as $key => $r) {
if(!empty($r->error)){}
else if(!empty($r->message_id)){
$staff = Staff::with('StaffDetail')->where('token_firebase',$register_id[$key])->first();
if(!empty($staff)){
$dataNoti = [
'title' => $request->title,
'body' => $request->msg,
];
$staff->notification()->create($dataNoti);
}
}
}
return $this->sendResponse($response,'Berjaya hantar notifikasi');
}
}
public function getNotificationMobile(Request $request){
$staff = Staff::with('StaffDetail','Notification')->where('api_token', $request->api_token)->first();
if(!empty($staff)){
$noti = $staff->Notification;
return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
}else{
return $this->sendError('','Kakitangan tidak ditemui!');
}
}
public function getNotificationWeb(Request $request){
$staff = Staff::with('StaffDetail','Notification')->where('_id', $request->_id)->first();
if(!empty($staff)){
$noti = $staff->Notification;
return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
}else{
return $this->sendError('','Kakitangan tidak ditemui!');
}
}
public function deleteNotification(Request $request){
$noti = Notification::where('_id', $request->_id)->first();
if(!empty($noti)){
$noti->delete();
return $this->sendResponse($noti,'Berjaya buang notifikasi');
}else{
return $this->sendError('','Data tidak ditemui!');
}
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Api\BaseController;
use Config;
use File;
use Carbon\Carbon;
use App\Model\Staff;
use App\Model\StaffDetail;
use App\Model\Module\Notification;
class NotificationController extends BaseController
{
/**
* Create save firebase token controller.
*
* @return json
*/
public function saveToken(Request $request) {
$staff = Staff::where('_id', $request->_id)->first();
if(!empty($staff)){
$staff->token_firebase = $request->token;
$staff->save();
return $this->sendResponse($staff,'Berjaya kemaskini firebase token');
}else{
return $this->sendError('','Tidak berjaya kemaskini firebase token');
}
}
/**
* Create notification controller.
*
* @return json
*/
public function createNotifications($title, $msg, array $token) {
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$notification = [
'title' => $title,
'body' => $msg,
'icon' => '',
'sound' => true,
];
$extraNotificationData = ["message" => $notification];
$fcmNotification = [
'registration_ids' => $token, //multple token array
'notification' => $notification,
'data' => $extraNotificationData
];
$headers = [
'Authorization: key=AIzaSyBJMDMk5s1Naxq5pW0cfcC4sSg_ExMyJYM',
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function sendNotification(Request $request){
$title = $request->title;
$msg = $request->msg;
$register_id = $request->register_id;
if($register_id != null){
$result = $this->createNotifications($title, $msg, $register_id);
$response = json_decode($result);
foreach ($response->results as $key => $r) {
if(!empty($r->error)){}
else if(!empty($r->message_id)){
$staff = Staff::with('StaffDetail')->where('token_firebase',$register_id[$key])->first();
if(!empty($staff)){
$dataNoti = [
'title' => $request->title,
'body' => $request->msg,
];
$staff->notification()->create($dataNoti);
}
}
}
return $this->sendResponse($response,'Berjaya hantar notifikasi');
}
}
public function getNotificationMobile(Request $request){
$staff = Staff::with('StaffDetail','Notification')->where('api_token', $request->api_token)->first();
if(!empty($staff)){
$noti = $staff->Notification;
return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
}else{
return $this->sendError('','Kakitangan tidak ditemui!');
}
}
public function getNotificationWeb(Request $request){
$staff = Staff::with('StaffDetail','Notification')->where('_id', $request->_id)->first();
if(!empty($staff)){
$noti = $staff->Notification;
return $this->sendResponse($noti,'Berjaya dapatkan notifikasi');
}else{
return $this->sendError('','Kakitangan tidak ditemui!');
}
}
public function deleteNotification(Request $request){
$noti = Notification::where('_id', $request->_id)->first();
if(!empty($noti)){
$noti->delete();
return $this->sendResponse($noti,'Berjaya buang notifikasi');
}else{
return $this->sendError('','Data tidak ditemui!');
}
}
}

Loading…
Cancel
Save