From 6a41353a96e3d11fa59a9e5ccde8662d560e17f6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 Jan 2017 14:35:30 +1100 Subject: [PATCH 1/5] Firebase notifications for Android (#1269) * revert error reporting * fix for non boolean output * Firebase notifications for Android --- app/Ninja/Notifications/PushFactory.php | 27 ++++++++++++++++--------- app/Services/PushService.php | 14 ++++++------- config/push-notification.php | 4 ++-- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/app/Ninja/Notifications/PushFactory.php b/app/Ninja/Notifications/PushFactory.php index e2f34c46be2d..6e69ac80bb7a 100644 --- a/app/Ninja/Notifications/PushFactory.php +++ b/app/Ninja/Notifications/PushFactory.php @@ -3,6 +3,7 @@ namespace App\Ninja\Notifications; use Davibennun\LaravelPushNotification\Facades\PushNotification; +use Illuminate\Support\Facades\Log; /** * Class PushFactory @@ -15,7 +16,6 @@ class PushFactory */ public function __construct() { - $this->certificate = IOS_PUSH_CERTIFICATE; } /** @@ -28,14 +28,15 @@ class PushFactory * @param $token * @param $message * @param $messageArray + * @param string $device - Type of device the message is being pushed to. * * @return void */ - public function customMessage($token, $message, $messageArray) + public function customMessage($token, $message, $messageArray, $device) { $customMessage = PushNotification::Message($message, $messageArray); - $this->message($token, $customMessage); + $this->message($token, $customMessage, $device); } /** @@ -51,11 +52,17 @@ class PushFactory * @return void */ - public function message($token, $message) + public function message($token, $message, $device) { - PushNotification::app($this->certificate) - ->to($token) - ->send($message); + try { + PushNotification::app($device) + ->to($token) + ->send($message); + } + catch(\Exception $e) { + Log::error($e->getMessage()); + } + } /** @@ -69,13 +76,13 @@ class PushFactory * * @param string $token - A valid token (can be any valid token) * @param string $message - Nil value for message - * + * @param string $device - Type of device the message is being pushed to. * @return array */ - public function getFeedback($token, $message = '') + public function getFeedback($token, $message = '', $device) { - $feedback = PushNotification::app($this->certificate) + $feedback = PushNotification::app($device) ->to($token) ->send($message); diff --git a/app/Services/PushService.php b/app/Services/PushService.php index 017426814bd8..759e0271cbff 100644 --- a/app/Services/PushService.php +++ b/app/Services/PushService.php @@ -30,10 +30,6 @@ class PushService */ public function sendNotification(Invoice $invoice, $type) { - if (! IOS_PUSH_CERTIFICATE) { - return; - } - //check user has registered for push notifications if(!$this->checkDeviceExists($invoice->account)) return; @@ -43,8 +39,10 @@ class PushService foreach($devices as $device) { - if(($device["notify_{$type}"] == TRUE) && ($device['device'] == 'ios')) - $this->pushMessage($invoice, $device['token'], $type); + 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); } } @@ -57,9 +55,9 @@ class PushService * @param $token * @param $type */ - private function pushMessage(Invoice $invoice, $token, $type) + private function pushMessage(Invoice $invoice, $token, $type, $device) { - $this->pushFactory->message($token, $this->messageType($invoice, $type)); + $this->pushFactory->message($token, $this->messageType($invoice, $type), $device); } /** diff --git a/config/push-notification.php b/config/push-notification.php index bf7ba5c40ab9..02fe2af03100 100644 --- a/config/push-notification.php +++ b/config/push-notification.php @@ -16,8 +16,8 @@ return [ ], 'ninjaAndroid' => [ 'environment' =>'production', - 'apiKey' =>'yourAPIKey', - 'service' =>'gcm' + 'apiKey' =>env(FCM_API_TOKEN), + 'service' =>'fcm' ] ]; From 16e3178fb3deed2642f8a08d44e294a7f6eb673f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 Jan 2017 16:35:43 +1100 Subject: [PATCH 2/5] Update .env.example --- .env.example | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.env.example b/.env.example index 3f6f0c7fdce9..18053683d7ba 100644 --- a/.env.example +++ b/.env.example @@ -31,6 +31,9 @@ PHANTOMJS_CLOUD_KEY='a-demo-key-with-low-quota-per-ip-address' LOG=single REQUIRE_HTTPS=false API_SECRET=password +IOS_DEVICE= +ANDROID_DEVICE= +FCM_API_TOKEN= #TRUSTED_PROXIES= From ccf6a6ed96484f66d3dd0309a273f3464cd76387 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 Jan 2017 16:42:27 +1100 Subject: [PATCH 3/5] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ed13c19da2c5..715feeb2a11e 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "php": ">=5.5.9", "ext-gmp": "*", "ext-gd": "*", - "turbo124/laravel-push-notification": "dev-laravel5", + "turbo124/laravel-push-notification": "2.*", "omnipay/mollie": "dev-master#22956c1a62a9662afa5f5d119723b413770ac525", "omnipay/2checkout": "dev-master#e9c079c2dde0d7ba461903b3b7bd5caf6dee1248", "omnipay/gocardless": "dev-master", From 403fe772b09f552854ca757c1c68bccbdd548526 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 Jan 2017 17:09:55 +1100 Subject: [PATCH 4/5] fix env variable (#1270) --- config/push-notification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/push-notification.php b/config/push-notification.php index 02fe2af03100..31e51edb88a9 100644 --- a/config/push-notification.php +++ b/config/push-notification.php @@ -16,7 +16,7 @@ return [ ], 'ninjaAndroid' => [ 'environment' =>'production', - 'apiKey' =>env(FCM_API_TOKEN), + 'apiKey' =>env('FCM_API_TOKEN'), 'service' =>'fcm' ] From 9728ef01180a9f218a387485cca05026b123c705 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 10 Jan 2017 17:17:00 +1100 Subject: [PATCH 5/5] Update constants in routes file (#1271) * fix env variable * update routes --- app/Http/routes.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/routes.php b/app/Http/routes.php index abe20b1eab4a..329ea6c0d4e2 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -709,7 +709,8 @@ if (!defined('CONTACT_EMAIL')) { define('DEFAULT_API_PAGE_SIZE', 15); define('MAX_API_PAGE_SIZE', 500); - define('IOS_PUSH_CERTIFICATE', env('IOS_PUSH_CERTIFICATE', '')); + define('IOS_DEVICE', env('IOS_DEVICE', '')); + define('ANDROID_DEVICE', env('ANDROID_DEVICE', '')); define('TOKEN_BILLING_DISABLED', 1); define('TOKEN_BILLING_OPT_IN', 2);