1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Push Notifications

This commit is contained in:
David Bomba 2016-03-01 13:57:59 +11:00
parent 57a4a24294
commit f1bc597034
3 changed files with 72 additions and 4 deletions

View File

@ -117,4 +117,40 @@ class AccountApiController extends BaseAPIController
return $this->response($account); 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);
}
} }

View File

@ -235,6 +235,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function()
Route::resource('tax_rates', 'TaxRateApiController'); Route::resource('tax_rates', 'TaxRateApiController');
Route::resource('users', 'UserApiController'); Route::resource('users', 'UserApiController');
Route::resource('expenses','ExpenseApiController'); Route::resource('expenses','ExpenseApiController');
Route::post('add_token', 'AccountApiController@addDeviceToken');
// Vendor // Vendor
Route::resource('vendors', 'VendorApiController'); Route::resource('vendors', 'VendorApiController');

View File

@ -1,9 +1,9 @@
<?php <?php
namespace App\Ninja\Notifications; namespace App\Services;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Ninja\Notifications\PushFactory;
/** /**
* Class PushService * Class PushService
* @package App\Ninja\Notifications * @package App\Ninja\Notifications
@ -13,7 +13,9 @@ use Illuminate\Http\Request;
/** /**
* $account->devices Definition * $account->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_sent
* @param bool notify_paid * @param bool notify_paid
* @param bool notify_approved * @param bool notify_approved
@ -24,6 +26,9 @@ class PushService
{ {
protected $pushFactory; protected $pushFactory;
/**
* @param PushFactory $pushFactory
*/
public function __construct(PushFactory $pushFactory) public function __construct(PushFactory $pushFactory)
{ {
$this->pushFactory = $pushFactory; $this->pushFactory = $pushFactory;
@ -45,7 +50,7 @@ class PushService
foreach($devices as $device) 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}"]); $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) private function pushMessage($invoice, $token, $type)
{ {
$this->pushFactory->message($token, $this->messageType($invoice, $type)); $this->pushFactory->message($token, $this->messageType($invoice, $type));
@ -77,6 +87,11 @@ class PushService
return FALSE; return FALSE;
} }
/**
* @param $invoice
* @param $type
* @return string
*/
private function messageType($invoice, $type) private function messageType($invoice, $type)
{ {
switch($type) switch($type)
@ -99,6 +114,10 @@ class PushService
} }
} }
/**
* @param $invoice
* @return string
*/
private function entitySentMessage($invoice) private function entitySentMessage($invoice)
{ {
if($invoice->is_quote) if($invoice->is_quote)
@ -108,16 +127,28 @@ class PushService
} }
/**
* @param $invoice
* @return string
*/
private function invoicePaidMessage($invoice) private function invoicePaidMessage($invoice)
{ {
return 'Invoice #{$invoice->invoice_number} paid!'; return 'Invoice #{$invoice->invoice_number} paid!';
} }
/**
* @param $invoice
* @return string
*/
private function quoteApprovedMessage($invoice) private function quoteApprovedMessage($invoice)
{ {
return 'Quote #{$invoice->invoice_number} approved!'; return 'Quote #{$invoice->invoice_number} approved!';
} }
/**
* @param $invoice
* @return string
*/
private function entityViewedMessage($invoice) private function entityViewedMessage($invoice)
{ {
if($invoice->is_quote) if($invoice->is_quote)