diff --git a/app/Library/Notifications/Drivers/NativeDriver.php b/app/Library/Notifications/Drivers/NativeDriver.php index 1690d402..eac30fdd 100644 --- a/app/Library/Notifications/Drivers/NativeDriver.php +++ b/app/Library/Notifications/Drivers/NativeDriver.php @@ -21,6 +21,7 @@ namespace Poniverse\Ponyfm\Library\Notifications\Drivers; +use Config; use Poniverse\Ponyfm\Contracts\Favouritable; use Poniverse\Ponyfm\Models\Activity; use Poniverse\Ponyfm\Models\Comment; @@ -37,32 +38,34 @@ class NativeDriver extends AbstractDriver { * @param User[] $recipients collection of {@link User} objects */ private function pushNotifications(Activity $activity, $recipients) { - $apiKeys = array( - 'GCM' => 'AIzaSyCLmCVIgASWL280rHyPz8OP7il3pf8SrGg', - ); - - $webPush = new WebPush($apiKeys); - - $data = [ - 'id' => $activity->id, - 'text' => $activity->getTextAttribute(), - 'title' => $activity->getTitleFromActivityType(), - 'image' => $activity->getThumbnailUrlAttribute(), - 'url' => $activity->url - ]; - - $jsonData = json_encode($data); - - foreach ($recipients as $recipient) { - $webPush->sendNotification( - $recipient->endpoint, - $jsonData, - $recipient->p256dh, - $recipient->auth + if (Config::get('ponyfm.gcm_key') != 'default') { + $apiKeys = array( + 'GCM' => Config::get('ponyfm.gcm_key'), ); - } - $webPush->flush(); + $webPush = new WebPush($apiKeys); + + $data = [ + 'id' => $activity->id, + 'text' => $activity->getTextAttribute(), + 'title' => $activity->getTitleFromActivityType(), + 'image' => $activity->getThumbnailUrlAttribute(), + 'url' => $activity->url + ]; + + $jsonData = json_encode($data); + + foreach ($recipients as $recipient) { + $webPush->sendNotification( + $recipient->endpoint, + $jsonData, + $recipient->p256dh, + $recipient->auth + ); + } + + $webPush->flush(); + } } /** diff --git a/config/ponyfm.php b/config/ponyfm.php index 6d7ae949..c8184243 100644 --- a/config/ponyfm.php +++ b/config/ponyfm.php @@ -116,4 +116,17 @@ return [ */ 'user_slug_minimum_length' => 3 + + /* + |-------------------------------------------------------------------------- + | Indexing queue name + |-------------------------------------------------------------------------- + | + | Google Cloud Messaging API key. Needs to be generated in the Google Cloud + | Console as a browser key. This is used to send notifications to users + | with push notifications enabled. + | + */ + + 'gcm_key' => env('GCM_KEY', 'default'), ]; diff --git a/resources/environments/.env.local b/resources/environments/.env.local index 042397be..42c8fe39 100644 --- a/resources/environments/.env.local +++ b/resources/environments/.env.local @@ -24,3 +24,4 @@ PONI_CLIENT_SECRET=null PONYFM_DATASTORE=/vagrant/storage/app/datastore GOOGLE_ANALYTICS_ID=null +GCM_KEY=12345