diff --git a/app/Http/Controllers/AccountApiController.php b/app/Http/Controllers/AccountApiController.php index 77e85243d7..b8a2a6a049 100644 --- a/app/Http/Controllers/AccountApiController.php +++ b/app/Http/Controllers/AccountApiController.php @@ -117,4 +117,40 @@ class AccountApiController extends BaseAPIController return $this->response($account); } + + public function addDeviceToken(Request $request) + { + $account = Auth::user()->account; + + //scan if this user has a token already registered (tokens can change, so we need to use the users email as key) + $devices = json_decode($account->devices,TRUE); + + if(count($devices) >= 1) { + foreach ($devices as $device) { + if ($device['email'] == Auth::user()->username) { + $device['token'] = $request->token; //update + return $this->response($account); + } + } + } + //User does not have a device, create new record + + $newDevice = [ + 'token' => $request->token, + 'email' => $request->email, + 'device' => $request->device, + 'notify_sent' => TRUE, + 'notify_viewed' => TRUE, + 'notify_approved' => TRUE, + 'notify_paid' => TRUE, + ]; + + $devices[] = $newDevice; + $account->devices = json_encode($devices); + $account->save(); + + return $this->response($account); + + } + } diff --git a/app/Http/routes.php b/app/Http/routes.php index 2bfa12d5b1..9cfb2ed94e 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -235,6 +235,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function() Route::resource('tax_rates', 'TaxRateApiController'); Route::resource('users', 'UserApiController'); Route::resource('expenses','ExpenseApiController'); + Route::post('add_token', 'AccountApiController@addDeviceToken'); // Vendor Route::resource('vendors', 'VendorApiController'); diff --git a/app/Ninja/Notifications/PushService.php b/app/Services/PushService.php similarity index 79% rename from app/Ninja/Notifications/PushService.php rename to app/Services/PushService.php index 0816a8c1ff..c33859bc24 100644 --- a/app/Ninja/Notifications/PushService.php +++ b/app/Services/PushService.php @@ -1,9 +1,9 @@ devices Definition * - * @param string token + * @param string token (push notification device token) + * @param string email (user email address - required for use as key) + * @param string device (ios, gcm etc etc) * @param bool notify_sent * @param bool notify_paid * @param bool notify_approved @@ -24,6 +26,9 @@ class PushService { protected $pushFactory; + /** + * @param PushFactory $pushFactory + */ public function __construct(PushFactory $pushFactory) { $this->pushFactory = $pushFactory; @@ -45,7 +50,7 @@ class PushService foreach($devices as $device) { - if($device["notify_{$type}"] == TRUE) + if(($device["notify_{$type}"] == TRUE) && ($device['device'] == 'ios')) $this->pushMessage($invoice, $device['token'], $device["notify_{$type}"]); } @@ -53,6 +58,11 @@ class PushService } + /** + * @param $invoice + * @param $token + * @param $type + */ private function pushMessage($invoice, $token, $type) { $this->pushFactory->message($token, $this->messageType($invoice, $type)); @@ -77,6 +87,11 @@ class PushService return FALSE; } + /** + * @param $invoice + * @param $type + * @return string + */ private function messageType($invoice, $type) { switch($type) @@ -99,6 +114,10 @@ class PushService } } + /** + * @param $invoice + * @return string + */ private function entitySentMessage($invoice) { if($invoice->is_quote) @@ -108,16 +127,28 @@ class PushService } + /** + * @param $invoice + * @return string + */ private function invoicePaidMessage($invoice) { return 'Invoice #{$invoice->invoice_number} paid!'; } + /** + * @param $invoice + * @return string + */ private function quoteApprovedMessage($invoice) { return 'Quote #{$invoice->invoice_number} approved!'; } + /** + * @param $invoice + * @return string + */ private function entityViewedMessage($invoice) { if($invoice->is_quote)