From 57a4a24294ed4b9a01455158d9e758a5fe683559 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 1 Mar 2016 11:31:16 +1100 Subject: [PATCH] Push Notifications --- app/Ninja/Notifications/PushService.php | 96 +++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 4 deletions(-) diff --git a/app/Ninja/Notifications/PushService.php b/app/Ninja/Notifications/PushService.php index c304ed14d273..0816a8c1ff0a 100644 --- a/app/Ninja/Notifications/PushService.php +++ b/app/Ninja/Notifications/PushService.php @@ -9,6 +9,17 @@ use Illuminate\Http\Request; * @package App\Ninja\Notifications */ + +/** + * $account->devices Definition + * + * @param string token + * @param bool notify_sent + * @param bool notify_paid + * @param bool notify_approved + * @param bool notify_viewed + */ + class PushService { protected $pushFactory; @@ -20,7 +31,7 @@ class PushService /** * @param $invoice - Invoice object - * @param $type - Type of notification, ie. Quote APPROVED, Invoice PAID, Invoice SENT, Invoice VIEWED + * @param $type - Type of notification, ie. Quote APPROVED, Invoice PAID, Invoice/Quote SENT, Invoice/Quote VIEWED */ public function sendNotification($invoice, $type) @@ -30,15 +41,92 @@ class PushService return; //Harvest an array of devices that are registered for this notification type + $devices = json_decode($invoice->account->devices, TRUE); + + foreach($devices as $device) + { + if($device["notify_{$type}"] == TRUE) + $this->pushMessage($invoice, $device['token'], $device["notify_{$type}"]); + } - //loop and send } + + private function pushMessage($invoice, $token, $type) + { + $this->pushFactory->message($token, $this->messageType($invoice, $type)); + } + + + /** + * checkDeviceExists function + * + * Returns a boolean if this account has devices registered for PUSH notifications + * + * @param $account + * @return bool + */ private function checkDeviceExists($account) { - $devices = json_decode($account->devices); + $devices = json_decode($account->devices, TRUE); - + if(count($devices) >= 1) + return TRUE; + else + return FALSE; } + + private function messageType($invoice, $type) + { + switch($type) + { + case 'notify_sent': + return $this->entitySentMessage($invoice); + break; + + case 'notify_paid': + return $this->invoicePaidMessage($invoice); + break; + + case 'notify_approved': + return $this->quoteApprovedMessage($invoice); + break; + + case 'notify_viewed': + return $this->entityViewedMessage($invoice); + break; + } + } + + private function entitySentMessage($invoice) + { + if($invoice->is_quote) + return 'Quote #{$invoice->invoice_number} sent!'; + else + return 'Invoice #{$invoice->invoice_number} sent!'; + + } + + private function invoicePaidMessage($invoice) + { + return 'Invoice #{$invoice->invoice_number} paid!'; + } + + private function quoteApprovedMessage($invoice) + { + return 'Quote #{$invoice->invoice_number} approved!'; + } + + private function entityViewedMessage($invoice) + { + if($invoice->is_quote) + return 'Quote #{$invoice->invoice_number} viewed!'; + else + return 'Invoice #{$invoice->invoice_number} viewed!'; + + } + + + } \ No newline at end of file