mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-08 20:22:42 +01:00
6a41353a96
* revert error reporting * fix for non boolean output * Firebase notifications for Android
155 lines
4.3 KiB
PHP
155 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Account;
|
|
use App\Models\Invoice;
|
|
use App\Ninja\Notifications\PushFactory;
|
|
|
|
/**
|
|
* Class PushService
|
|
*/
|
|
class PushService
|
|
{
|
|
/**
|
|
* @var PushFactory
|
|
*/
|
|
protected $pushFactory;
|
|
|
|
/**
|
|
* @param PushFactory $pushFactory
|
|
*/
|
|
public function __construct(PushFactory $pushFactory)
|
|
{
|
|
$this->pushFactory = $pushFactory;
|
|
}
|
|
|
|
/**
|
|
* @param Invoice $invoice
|
|
* @param $type
|
|
*/
|
|
public function sendNotification(Invoice $invoice, $type)
|
|
{
|
|
//check user has registered for push notifications
|
|
if(!$this->checkDeviceExists($invoice->account))
|
|
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) && ($device['device'] == 'ios') && IOS_DEVICE)
|
|
$this->pushMessage($invoice, $device['token'], $type, IOS_DEVICE);
|
|
elseif(($device["notify_{$type}"] == TRUE) && ($device['device'] == 'fcm') && ANDROID_DEVICE)
|
|
$this->pushMessage($invoice, $device['token'], $type, ANDROID_DEVICE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* pushMessage function
|
|
*
|
|
* method to dispatch iOS notifications
|
|
*
|
|
* @param Invoice $invoice
|
|
* @param $token
|
|
* @param $type
|
|
*/
|
|
private function pushMessage(Invoice $invoice, $token, $type, $device)
|
|
{
|
|
$this->pushFactory->message($token, $this->messageType($invoice, $type), $device);
|
|
}
|
|
|
|
/**
|
|
* checkDeviceExists function
|
|
*
|
|
* Returns a boolean if this account has devices registered for PUSH notifications
|
|
*
|
|
* @param Account $account
|
|
*
|
|
* @return bool
|
|
*/
|
|
private function checkDeviceExists(Account $account)
|
|
{
|
|
$devices = json_decode($account->devices, TRUE);
|
|
|
|
if(count($devices) >= 1)
|
|
return TRUE;
|
|
else
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* messageType function
|
|
*
|
|
* method which formats an appropriate message depending on message type
|
|
*
|
|
* @param Invoice $invoice
|
|
* @param $type
|
|
*
|
|
* @return string
|
|
*/
|
|
private function messageType(Invoice $invoice, $type)
|
|
{
|
|
switch($type)
|
|
{
|
|
case 'sent':
|
|
return $this->entitySentMessage($invoice);
|
|
break;
|
|
|
|
case 'paid':
|
|
return $this->invoicePaidMessage($invoice);
|
|
break;
|
|
|
|
case 'approved':
|
|
return $this->quoteApprovedMessage($invoice);
|
|
break;
|
|
|
|
case 'viewed':
|
|
return $this->entityViewedMessage($invoice);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Invoice $invoice
|
|
* @return string
|
|
*/
|
|
private function entitySentMessage(Invoice $invoice)
|
|
{
|
|
if($invoice->isType(INVOICE_TYPE_QUOTE))
|
|
return trans('texts.notification_quote_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
|
else
|
|
return trans('texts.notification_invoice_sent_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
|
}
|
|
|
|
/**
|
|
* @param Invoice $invoice
|
|
* @return string
|
|
*/
|
|
private function invoicePaidMessage(Invoice $invoice)
|
|
{
|
|
return trans('texts.notification_invoice_paid_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
|
}
|
|
|
|
/**
|
|
* @param Invoice $invoice
|
|
* @return string
|
|
*/
|
|
private function quoteApprovedMessage(Invoice $invoice)
|
|
{
|
|
return trans('texts.notification_quote_approved_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
|
}
|
|
|
|
/**
|
|
* @param Invoice $invoice
|
|
* @return string
|
|
*/
|
|
private function entityViewedMessage(Invoice $invoice)
|
|
{
|
|
if($invoice->isType(INVOICE_TYPE_QUOTE))
|
|
return trans('texts.notification_quote_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
|
else
|
|
return trans('texts.notification_invoice_viewed_subject', ['invoice' => $invoice->invoice_number, 'client' => $invoice->client->name]);
|
|
}
|
|
} |