Skip to content

Commit fb5e726

Browse files
authored
Merge 6a0fc12 into 7cff3f8
2 parents 7cff3f8 + 6a0fc12 commit fb5e726

10 files changed

+412
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## [Unreleased]
88
### Added
99
- Coverage information
10+
- Support for Laravel's notification channels
1011
### Fixed
1112
- Require correct configuration of package by removing placeholder data
1213

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,15 @@ This command copies the configuration file to `config/pushwoosh.php`.
4343

4444
Make sure you set up the `api_token` and `application_id` of the default
4545
application.
46+
47+
## Usage
48+
49+
You can use the wrapper to interact with the Pushwoosh SDK directly, or, you could add support for Pushwoosh to your
50+
[Laravel notifications](https://laravel.com/docs/notifications).
51+
52+
### Laravel Notifications
53+
54+
If you are using Laravel's notification system, you can add `'pushwoosh'` to the `via()` response of a notification.
55+
The channel name is also available as a class constant on the `Contextmapp\Pushwoosh\PushwooshChannel` class.
56+
The notification class is expected to implement the `Contextmapp\Pushwoosh\Contracts\PushwooshNotification` contract.
57+
Your notifiable classes should implement the `Contextmapp\Pushwoosh\Contracts\PushwooshNotifiable` contract.

composer.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"description": "Laravel package for integrating Pushwoosh",
44
"type": "library",
55
"license": "MIT",
6-
"keywords": ["pushwoosh", "laravel"],
6+
"keywords": [
7+
"pushwoosh",
8+
"laravel"
9+
],
710
"authors": [
811
{
912
"name": "Raymond Jelierse",
@@ -12,8 +15,9 @@
1215
],
1316
"require": {
1417
"php": "^7.0",
15-
"illuminate/support": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.*",
16-
"gomoob/php-pushwoosh": "^1.0"
18+
"gomoob/php-pushwoosh": "^1.0",
19+
"illuminate/notifications": "5.3 - 5.6",
20+
"illuminate/support": "5.1 - 5.6"
1721
},
1822
"require-dev": {
1923
"phpunit/phpunit": "~6.0",
@@ -29,6 +33,9 @@
2933
"Contextmapp\\Pushwoosh\\Tests\\": "tests/"
3034
}
3135
},
36+
"suggest": {
37+
"laravel-notification-channels/backport": "Required when using Laravel 5.1 or 5.2, drop-in replacement for 'illuminate/notifications'."
38+
},
3239
"extra": {
3340
"laravel": {
3441
"providers": [
@@ -38,5 +45,8 @@
3845
"Pushwoosh": "Contextmapp\\Pushwoosh\\Facades\\Pushwoosh"
3946
}
4047
}
48+
},
49+
"config": {
50+
"sort-packages": true
4151
}
4252
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laravel Pushwoosh package.
5+
*
6+
* (c) Contextmapp B.V. <support@contextmapp.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Contextmapp\Pushwoosh\Contracts;
13+
14+
use Gomoob\Pushwoosh\Model\Condition\ICondition;
15+
16+
/**
17+
* Implement this interface on the classes that include the {@link \Illuminate\Notifications\Notifiable} trait
18+
* and should have routing for Pushwoosh notifications.
19+
*/
20+
interface PushwooshNotifiable
21+
{
22+
/**
23+
* Set the Pushwoosh conditions that target the intended user.
24+
*
25+
* @return ICondition[]
26+
*/
27+
public function routeNotificationForPushwoosh(): array;
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laravel Pushwoosh package.
5+
*
6+
* (c) Contextmapp B.V. <support@contextmapp.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Contextmapp\Pushwoosh\Contracts;
13+
14+
use Contextmapp\Pushwoosh\PushwooshMessage;
15+
16+
/**
17+
* Implement this interface on notifications that should pass through the Pushwoosh channel.
18+
*/
19+
interface PushwooshNotification
20+
{
21+
/**
22+
* Create the payload for the notification when routed through Pushwoosh.
23+
*
24+
* @param mixed $notifiable
25+
*
26+
* @return PushwooshMessage
27+
*/
28+
public function toPushwoosh($notifiable): PushwooshMessage;
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laravel Pushwoosh package.
5+
*
6+
* (c) Contextmapp B.V. <support@contextmapp.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Contextmapp\Pushwoosh\Exceptions;
13+
14+
class NotificationFailedException extends \RuntimeException
15+
{
16+
}

src/PushwooshChannel.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laravel Pushwoosh package.
5+
*
6+
* (c) Contextmapp B.V. <support@contextmapp.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Contextmapp\Pushwoosh;
13+
14+
use Contextmapp\Pushwoosh\Contracts\PushwooshNotifiable;
15+
use Contextmapp\Pushwoosh\Contracts\PushwooshNotification;
16+
use Contextmapp\Pushwoosh\Exceptions\NotificationFailedException;
17+
use Gomoob\Pushwoosh\Exception\PushwooshException;
18+
use Gomoob\Pushwoosh\Model\Notification\Android;
19+
use Gomoob\Pushwoosh\Model\Notification\IOS;
20+
use Gomoob\Pushwoosh\Model\Request\CreateMessageRequest;
21+
use Illuminate\Notifications\Notification;
22+
23+
/**
24+
* Channel for dispatching notifications through Pushwoosh.
25+
*/
26+
class PushwooshChannel
27+
{
28+
const NAME = 'pushwoosh';
29+
30+
private $manager;
31+
32+
/**
33+
* PushwooshChannel constructor.
34+
*
35+
* @param PushwooshManager $manager
36+
*/
37+
public function __construct(PushwooshManager $manager)
38+
{
39+
$this->manager = $manager;
40+
}
41+
42+
/**
43+
* Send the notification through the Pushwoosh notification channel.
44+
*
45+
* @param mixed $notifiable
46+
* @param \Illuminate\Notifications\Notification $notification
47+
*
48+
* @return void
49+
*/
50+
public function send($notifiable, Notification $notification)
51+
{
52+
if ((!$notifiable instanceof PushwooshNotifiable) || (!$notification instanceof PushwooshNotification)) {
53+
return;
54+
}
55+
56+
$message = $notification->toPushwoosh($notifiable);
57+
$request = $this->buildRequest($notifiable, $message);
58+
59+
try {
60+
$response = $this->manager->application($message->application)->createMessage($request);
61+
} catch (PushwooshException $e) {
62+
throw new NotificationFailedException('Failed to send notification to Pushwoosh', 0, $e);
63+
}
64+
65+
if (false === $response->isOk()) {
66+
throw new NotificationFailedException('Failed to send notification to Pushwoosh', $response->getStatusCode());
67+
}
68+
}
69+
70+
/**
71+
* Build the request to send to Pushwoosh.
72+
*
73+
* @param \Contextmapp\Pushwoosh\Contracts\PushwooshNotifiable $notifiable
74+
* @param \Contextmapp\Pushwoosh\PushwooshMessage $message
75+
*
76+
* @return \Gomoob\Pushwoosh\Model\Request\CreateMessageRequest
77+
*/
78+
private function buildRequest(PushwooshNotifiable $notifiable, PushwooshMessage $message): CreateMessageRequest
79+
{
80+
$android = new Android();
81+
$ios = new IOS();
82+
83+
$notification = new \Gomoob\Pushwoosh\Model\Notification\Notification();
84+
$notification->setContent($message->content);
85+
$notification->setData($message->data);
86+
$notification->setAndroid($android);
87+
$notification->setIOS($ios);
88+
89+
if ($message->subject) {
90+
$android->setHeader($message->subject);
91+
}
92+
93+
if ($message->increaseBadgeNumber) {
94+
$android->setBadges('+1');
95+
$ios->setBadges('+1');
96+
}
97+
98+
$notification->setConditions($notifiable->routeNotificationForPushwoosh());
99+
100+
$request = new CreateMessageRequest();
101+
$request->addNotification($notification);
102+
103+
return $request;
104+
}
105+
}

src/PushwooshMessage.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laravel Pushwoosh package.
5+
*
6+
* (c) Contextmapp B.V. <support@contextmapp.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Contextmapp\Pushwoosh;
13+
14+
/**
15+
* Message information for Pushwoosh notifications.
16+
*/
17+
class PushwooshMessage
18+
{
19+
/**
20+
* The Pushwoosh application to target with this message.
21+
*
22+
* @var string|null
23+
*/
24+
public $application;
25+
26+
/**
27+
* @var string|null
28+
*/
29+
public $subject;
30+
31+
/**
32+
* The content of the notification.
33+
*
34+
* @var string
35+
*/
36+
public $content;
37+
38+
/**
39+
* Should the application icon badge number on the recipient's device be increased?
40+
*
41+
* @var bool
42+
*/
43+
public $increaseBadgeNumber = true;
44+
45+
/**
46+
* Extra data to send along with the notification.
47+
*
48+
* @var array
49+
*/
50+
public $data = [];
51+
52+
public function __construct(string $message = '')
53+
{
54+
$this->content = $message;
55+
}
56+
57+
public function message(string $message)
58+
{
59+
$this->content = $message;
60+
61+
return $this;
62+
}
63+
64+
public function subject(string $subject)
65+
{
66+
$this->subject = $subject;
67+
68+
return $this;
69+
}
70+
71+
public function application(string $application)
72+
{
73+
$this->application = $application;
74+
75+
return $this;
76+
}
77+
78+
public function increaseBadgeNumber(bool $increaseBadgeNumber)
79+
{
80+
$this->increaseBadgeNumber = $increaseBadgeNumber;
81+
82+
return $this;
83+
}
84+
85+
public function data(array $data)
86+
{
87+
$this->data = $data;
88+
89+
return $this;
90+
}
91+
}

src/PushwooshServiceProvider.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Contextmapp\Pushwoosh;
1313

1414
use Gomoob\Pushwoosh\Client\Pushwoosh;
15+
use Illuminate\Contracts\Foundation\Application;
16+
use Illuminate\Notifications\ChannelManager;
1517
use Illuminate\Support\ServiceProvider;
1618

1719
/**
@@ -24,13 +26,19 @@ class PushwooshServiceProvider extends ServiceProvider
2426
/**
2527
* Perform post-registration booting of services.
2628
*
29+
* @param \Illuminate\Notifications\ChannelManager
30+
*
2731
* @return void
2832
*/
29-
public function boot()
33+
public function boot(ChannelManager $manager)
3034
{
3135
$this->publishes([
3236
__DIR__.'/../config/pushwoosh.php' => config_path('pushwoosh.php'),
3337
]);
38+
39+
$manager->extend(PushwooshChannel::NAME, function (Application $app) {
40+
return $app->make(PushwooshChannel::class);
41+
});
3442
}
3543

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

45-
$this->app->singleton(PushwooshManager::class, function ($app) {
53+
$this->app->singleton(PushwooshFactory::class);
54+
$this->app->singleton(PushwooshChannel::class);
55+
$this->app->singleton(PushwooshManager::class, function (Application $app) {
4656
return new PushwooshManager($app['config']['pushwoosh'], $app[PushwooshFactory::class]);
4757
});
48-
49-
$this->app->singleton(PushwooshFactory::class, function () {
50-
return new PushwooshFactory();
51-
});
52-
5358
$this->app->singleton(Pushwoosh::class, function ($app) {
5459
return $app[PushwooshManager::class]->application();
5560
});
@@ -61,8 +66,9 @@ public function register()
6166
public function provides()
6267
{
6368
return [
64-
PushwooshManager::class,
69+
PushwooshChannel::class,
6570
PushwooshFactory::class,
71+
PushwooshManager::class,
6672
Pushwoosh::class,
6773
];
6874
}

0 commit comments

Comments
 (0)