Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Added
- Coverage information
- Support for Laravel's notification channels
### Fixed
- Require correct configuration of package by removing placeholder data

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ This command copies the configuration file to `config/pushwoosh.php`.

Make sure you set up the `api_token` and `application_id` of the default
application.

## Usage

You can use the wrapper to interact with the Pushwoosh SDK directly, or, you could add support for Pushwoosh to your
[Laravel notifications](https://laravel.com/docs/notifications).

### Laravel Notifications

If you are using Laravel's notification system, you can add `'pushwoosh'` to the `via()` response of a notification.
The channel name is also available as a class constant on the `Contextmapp\Pushwoosh\PushwooshChannel` class.
The notification class is expected to implement the `Contextmapp\Pushwoosh\Contracts\PushwooshNotification` contract.
Your notifiable classes should implement the `Contextmapp\Pushwoosh\Contracts\PushwooshNotifiable` contract.
16 changes: 13 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"description": "Laravel package for integrating Pushwoosh",
"type": "library",
"license": "MIT",
"keywords": ["pushwoosh", "laravel"],
"keywords": [
"pushwoosh",
"laravel"
],
"authors": [
{
"name": "Raymond Jelierse",
Expand All @@ -12,8 +15,9 @@
],
"require": {
"php": "^7.0",
"illuminate/support": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.*",
"gomoob/php-pushwoosh": "^1.0"
"gomoob/php-pushwoosh": "^1.0",
"illuminate/notifications": "5.3 - 5.6",
"illuminate/support": "5.1 - 5.6"
},
"require-dev": {
"phpunit/phpunit": "~6.0",
Expand All @@ -29,6 +33,9 @@
"Contextmapp\\Pushwoosh\\Tests\\": "tests/"
}
},
"suggest": {
"laravel-notification-channels/backport": "Required when using Laravel 5.1 or 5.2, drop-in replacement for 'illuminate/notifications'."
},
"extra": {
"laravel": {
"providers": [
Expand All @@ -38,5 +45,8 @@
"Pushwoosh": "Contextmapp\\Pushwoosh\\Facades\\Pushwoosh"
}
}
},
"config": {
"sort-packages": true
}
}
28 changes: 28 additions & 0 deletions src/Contracts/PushwooshNotifiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Laravel Pushwoosh package.
*
* (c) Contextmapp B.V. <support@contextmapp.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Contextmapp\Pushwoosh\Contracts;

use Gomoob\Pushwoosh\Model\Condition\ICondition;

/**
* Implement this interface on the classes that include the {@link \Illuminate\Notifications\Notifiable} trait
* and should have routing for Pushwoosh notifications.
*/
interface PushwooshNotifiable
{
/**
* Set the Pushwoosh conditions that target the intended user.
*
* @return ICondition[]
*/
public function routeNotificationForPushwoosh(): array;
}
29 changes: 29 additions & 0 deletions src/Contracts/PushwooshNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Laravel Pushwoosh package.
*
* (c) Contextmapp B.V. <support@contextmapp.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Contextmapp\Pushwoosh\Contracts;

use Contextmapp\Pushwoosh\PushwooshMessage;

/**
* Implement this interface on notifications that should pass through the Pushwoosh channel.
*/
interface PushwooshNotification
{
/**
* Create the payload for the notification when routed through Pushwoosh.
*
* @param mixed $notifiable
*
* @return PushwooshMessage
*/
public function toPushwoosh($notifiable): PushwooshMessage;
}
16 changes: 16 additions & 0 deletions src/Exceptions/NotificationFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Laravel Pushwoosh package.
*
* (c) Contextmapp B.V. <support@contextmapp.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Contextmapp\Pushwoosh\Exceptions;

class NotificationFailedException extends \RuntimeException
{
}
105 changes: 105 additions & 0 deletions src/PushwooshChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of the Laravel Pushwoosh package.
*
* (c) Contextmapp B.V. <support@contextmapp.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Contextmapp\Pushwoosh;

use Contextmapp\Pushwoosh\Contracts\PushwooshNotifiable;
use Contextmapp\Pushwoosh\Contracts\PushwooshNotification;
use Contextmapp\Pushwoosh\Exceptions\NotificationFailedException;
use Gomoob\Pushwoosh\Exception\PushwooshException;
use Gomoob\Pushwoosh\Model\Notification\Android;
use Gomoob\Pushwoosh\Model\Notification\IOS;
use Gomoob\Pushwoosh\Model\Request\CreateMessageRequest;
use Illuminate\Notifications\Notification;

/**
* Channel for dispatching notifications through Pushwoosh.
*/
class PushwooshChannel
{
const NAME = 'pushwoosh';

private $manager;

/**
* PushwooshChannel constructor.
*
* @param PushwooshManager $manager
*/
public function __construct(PushwooshManager $manager)
{
$this->manager = $manager;
}

/**
* Send the notification through the Pushwoosh notification channel.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return void
*/
public function send($notifiable, Notification $notification)
{
if ((!$notifiable instanceof PushwooshNotifiable) || (!$notification instanceof PushwooshNotification)) {
return;
}

$message = $notification->toPushwoosh($notifiable);
$request = $this->buildRequest($notifiable, $message);

try {
$response = $this->manager->application($message->application)->createMessage($request);
} catch (PushwooshException $e) {
throw new NotificationFailedException('Failed to send notification to Pushwoosh', 0, $e);
}

if (false === $response->isOk()) {
throw new NotificationFailedException('Failed to send notification to Pushwoosh', $response->getStatusCode());
}
}

/**
* Build the request to send to Pushwoosh.
*
* @param \Contextmapp\Pushwoosh\Contracts\PushwooshNotifiable $notifiable
* @param \Contextmapp\Pushwoosh\PushwooshMessage $message
*
* @return \Gomoob\Pushwoosh\Model\Request\CreateMessageRequest
*/
private function buildRequest(PushwooshNotifiable $notifiable, PushwooshMessage $message): CreateMessageRequest
{
$android = new Android();
$ios = new IOS();

$notification = new \Gomoob\Pushwoosh\Model\Notification\Notification();
$notification->setContent($message->content);
$notification->setData($message->data);
$notification->setAndroid($android);
$notification->setIOS($ios);

if ($message->subject) {
$android->setHeader($message->subject);
}

if ($message->increaseBadgeNumber) {
$android->setBadges('+1');
$ios->setBadges('+1');
}

$notification->setConditions($notifiable->routeNotificationForPushwoosh());

$request = new CreateMessageRequest();
$request->addNotification($notification);

return $request;
}
}
91 changes: 91 additions & 0 deletions src/PushwooshMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Laravel Pushwoosh package.
*
* (c) Contextmapp B.V. <support@contextmapp.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Contextmapp\Pushwoosh;

/**
* Message information for Pushwoosh notifications.
*/
class PushwooshMessage
{
/**
* The Pushwoosh application to target with this message.
*
* @var string|null
*/
public $application;

/**
* @var string|null
*/
public $subject;

/**
* The content of the notification.
*
* @var string
*/
public $content;

/**
* Should the application icon badge number on the recipient's device be increased?
*
* @var bool
*/
public $increaseBadgeNumber = true;

/**
* Extra data to send along with the notification.
*
* @var array
*/
public $data = [];

public function __construct(string $message = '')
{
$this->content = $message;
}

public function message(string $message)
{
$this->content = $message;

return $this;
}

public function subject(string $subject)
{
$this->subject = $subject;

return $this;
}

public function application(string $application)
{
$this->application = $application;

return $this;
}

public function increaseBadgeNumber(bool $increaseBadgeNumber)
{
$this->increaseBadgeNumber = $increaseBadgeNumber;

return $this;
}

public function data(array $data)
{
$this->data = $data;

return $this;
}
}
22 changes: 14 additions & 8 deletions src/PushwooshServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Contextmapp\Pushwoosh;

use Gomoob\Pushwoosh\Client\Pushwoosh;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\ServiceProvider;

/**
Expand All @@ -24,13 +26,19 @@ class PushwooshServiceProvider extends ServiceProvider
/**
* Perform post-registration booting of services.
*
* @param \Illuminate\Notifications\ChannelManager
*
* @return void
*/
public function boot()
public function boot(ChannelManager $manager)
{
$this->publishes([
__DIR__.'/../config/pushwoosh.php' => config_path('pushwoosh.php'),
]);

$manager->extend(PushwooshChannel::NAME, function (Application $app) {
return $app->make(PushwooshChannel::class);
});
}

/**
Expand All @@ -42,14 +50,11 @@ public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/pushwoosh.php', 'pushwoosh');

$this->app->singleton(PushwooshManager::class, function ($app) {
$this->app->singleton(PushwooshFactory::class);
$this->app->singleton(PushwooshChannel::class);
$this->app->singleton(PushwooshManager::class, function (Application $app) {
return new PushwooshManager($app['config']['pushwoosh'], $app[PushwooshFactory::class]);
});

$this->app->singleton(PushwooshFactory::class, function () {
return new PushwooshFactory();
});

$this->app->singleton(Pushwoosh::class, function ($app) {
return $app[PushwooshManager::class]->application();
});
Expand All @@ -61,8 +66,9 @@ public function register()
public function provides()
{
return [
PushwooshManager::class,
PushwooshChannel::class,
PushwooshFactory::class,
PushwooshManager::class,
Pushwoosh::class,
];
}
Expand Down
Loading