Skip to content

Commit cee1372

Browse files
authored
Merge branch 'master' into master
2 parents fcb7db6 + 5a7fb1e commit cee1372

File tree

5 files changed

+250
-36
lines changed

5 files changed

+250
-36
lines changed

README.md

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Pusher Beams push notifications channel for Laravel 5.5+, 6.x & 7.x
1+
# Pusher Beams push notifications channel for Laravel 5.5+, 6.x, 7.x & 8.x
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/pusher-push-notifications.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/pusher-push-notifications)
44
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
@@ -17,16 +17,21 @@ Also please note that prior to version 2.0, this package integrated with Pusher'
1717

1818
## Contents
1919

20-
- [Installation](#installation)
21-
- [Setting up your Pusher account](#setting-up-your-pusher-account)
22-
- [Usage](#usage)
23-
- [Available Message methods](#available-message-methods)
24-
- [Changelog](#changelog)
25-
- [Testing](#testing)
26-
- [Security](#security)
27-
- [Contributing](#contributing)
28-
- [Credits](#credits)
29-
- [License](#license)
20+
- [Pusher Beams push notifications channel for Laravel 5.5+, 6.x, 7.x & 8.x](#pusher-beams-push-notifications-channel-for-laravel-55-6x-7x--8x)
21+
- [Contents](#contents)
22+
- [Installation](#installation)
23+
- [Setting up your Pusher account](#setting-up-your-pusher-account)
24+
- [Usage](#usage)
25+
- [Available Message methods](#available-message-methods)
26+
- [Sending to multiple platforms](#sending-to-multiple-platforms)
27+
- [Routing a message](#routing-a-message)
28+
- [Publish to users](#publish-to-users)
29+
- [Changelog](#changelog)
30+
- [Testing](#testing)
31+
- [Security](#security)
32+
- [Contributing](#contributing)
33+
- [Credits](#credits)
34+
- [License](#license)
3035

3136

3237
## Installation
@@ -87,9 +92,11 @@ class AccountApproved extends Notification
8792

8893
### Available Message methods
8994

90-
- `platform('')`: Accepts a string value of `iOS` or `Android`.
95+
- `platform('')`: Accepts a string value of `iOS`, `Android` or `web`.
9196
- `iOS()`: Sets the platform value to iOS.
9297
- `android()`: Sets the platform value to Android.
98+
- `web()`: Sets the platform value to web.
99+
- `link()`: Accepts a string value which will lead to URI specified on notification click.
93100
- `title('')`: Accepts a string value for the title.
94101
- `body('')`: Accepts a string value for the body.
95102
- `sound('')`: Accepts a string value for the notification sound file. Notice that if you leave blank the default sound value will be `default`.
@@ -120,11 +127,42 @@ public function toPushNotification($notifiable)
120127
```
121128

122129
> - Notice that iOS is the default platform, which means you don't have to call `->iOS()`.
123-
> - When using `withAndroid()` or `withiOS()` you don't have to define the platform, it's done behind the scenes for you.
130+
> - When using `withAndroid()`, `withiOS()` or `withWeb()` you don't have to define the platform, it's done behind the scenes for you.
124131
125132
### Routing a message
126133

127-
By default the pusher "interest" messages will be sent to will be defined using the {notifiable}.{id} convention, for example `App.User.1`, however you can change this behaviour by including a `routeNotificationForPusherPushNotifications()` in the notifiable class method that returns the interest name.
134+
By default, the pusher "interest" messages will be sent to will be defined using the {notifiable}.{id} convention, for example `App.User.1`,
135+
however you can change this behaviour by including a `routeNotificationFor()` in the notifiable class.
136+
137+
I.e. if you are pushing notification on ``User`` model, you can go to `App\User` class and implement method:
138+
139+
```
140+
public function routeNotificationFor($channel)
141+
{
142+
if($channel === 'PusherPushNotifications'){
143+
return 'your.custom.interest.string';
144+
}
145+
146+
$class = str_replace('\\', '.', get_class($this));
147+
148+
return $class.'.'.$this->getKey();
149+
}
150+
```
151+
152+
`PusherPushNotifications()` in the notifiable class method returns the interest name.
153+
154+
### Publish to users
155+
156+
You can publish to users in the same way that you publish to interests but you must add the following variable to the notifiable model:
157+
158+
```
159+
class Client extends Model
160+
{
161+
use Notifiable;
162+
163+
public $pushNotificationType = 'users';
164+
}
165+
```
128166

129167
## Changelog
130168

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
],
3434
"require": {
3535
"php": ">=7.0",
36-
"illuminate/events": "~5.5 || ~6.0 || ~7.0",
37-
"illuminate/notifications": "~5.5 || ~6.0 || ~7.0",
38-
"illuminate/queue": "~5.5 || ~6.0 || ~7.0",
39-
"illuminate/support": "~5.5 || ~6.0 || ~7.0",
36+
"illuminate/events": "~5.5 || ~6.0 || ~7.0 || ~8.0",
37+
"illuminate/notifications": "~5.5 || ~6.0 || ~7.0 || ~8.0",
38+
"illuminate/queue": "~5.5 || ~6.0 || ~7.0 || ~8.0",
39+
"illuminate/support": "~5.5 || ~6.0 || ~7.0 || ~8.0",
4040
"pusher/pusher-push-notifications": "^1.1"
4141
},
4242
"require-dev": {

src/PusherChannel.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
use Illuminate\Contracts\Events\Dispatcher;
66
use Illuminate\Notifications\Events\NotificationFailed;
77
use Illuminate\Notifications\Notification;
8+
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Str;
810
use Pusher\PushNotifications\PushNotifications;
911
use Throwable;
1012

1113
class PusherChannel
1214
{
15+
/**
16+
* @var string
17+
*/
18+
const INTERESTS = 'interests';
19+
1320
/**
1421
* @var PushNotifications
1522
*/
@@ -40,12 +47,16 @@ public function __construct(PushNotifications $beamsClient, Dispatcher $events)
4047
*/
4148
public function send($notifiable, Notification $notification)
4249
{
43-
$interest = $notifiable->routeNotificationFor('PusherPushNotifications')
44-
?: $this->interestName($notifiable);
50+
$type = $notifiable->pushNotificationType ?? self::INTERESTS;
51+
52+
$data = $notifiable->routeNotificationFor('PusherPushNotifications')
53+
?: $this->defaultName($notifiable);
4554

4655
try {
47-
$this->beamsClient->publishToInterests(
48-
is_array($interest) ? $interest : [$interest],
56+
$notificationType = sprintf('publishTo%s', Str::ucfirst($type));
57+
58+
$this->beamsClient->{$notificationType}(
59+
Arr::wrap($data),
4960
$notification->toPushNotification($notifiable)->toArray()
5061
);
5162
} catch (Throwable $exception) {
@@ -56,13 +67,13 @@ public function send($notifiable, Notification $notification)
5667
}
5768

5869
/**
59-
* Get the interest name for the notifiable.
70+
* Get the default name for the notifiable.
6071
*
6172
* @param $notifiable
6273
*
6374
* @return string
6475
*/
65-
protected function interestName($notifiable)
76+
protected function defaultName($notifiable)
6677
{
6778
$class = str_replace('\\', '.', get_class($notifiable));
6879

src/PusherMessage.php

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class PusherMessage
4949
*/
5050
protected $badge;
5151

52+
/**
53+
* URL to follow on notification click.
54+
*/
55+
protected $link;
56+
5257
/**
5358
* Extra options that will get added to the message.
5459
*
@@ -99,7 +104,7 @@ public function __construct($body = '')
99104
*/
100105
public function platform($platform)
101106
{
102-
if (! in_array($platform, ['iOS', 'Android'])) {
107+
if (! in_array($platform, ['iOS', 'Android', 'web'])) {
103108
throw CouldNotCreateMessage::invalidPlatformGiven($platform);
104109
}
105110

@@ -132,6 +137,18 @@ public function android()
132137
return $this;
133138
}
134139

140+
/**
141+
* Set the platform to web.
142+
*
143+
* @return $this
144+
*/
145+
public function web()
146+
{
147+
$this->platform = 'web';
148+
149+
return $this;
150+
}
151+
135152
/**
136153
* Set an extra message to be sent to Android.
137154
*
@@ -158,6 +175,19 @@ public function withiOS(self $message)
158175
return $this;
159176
}
160177

178+
/**
179+
* Set an extra message to be sent to web.
180+
*
181+
* @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
182+
* @return $this
183+
*/
184+
public function withWeb(self $message)
185+
{
186+
$this->withExtra($message->web());
187+
188+
return $this;
189+
}
190+
161191
/**
162192
* Set an extra message to be sent to another platform.
163193
*
@@ -257,6 +287,20 @@ public function meta(array $meta)
257287
return $this;
258288
}
259289

290+
/**
291+
* Set the message link.
292+
*
293+
* @param string $value
294+
*
295+
* @return $this
296+
*/
297+
public function link($value)
298+
{
299+
$this->link = $value;
300+
301+
return $this;
302+
}
303+
260304
/**
261305
* @param string $key
262306
* @param mixed $value
@@ -277,9 +321,14 @@ public function setOption($key, $value)
277321
*/
278322
public function toArray()
279323
{
280-
return $this->platform === 'iOS'
281-
? $this->toiOS()
282-
: $this->toAndroid();
324+
switch ($this->platform) {
325+
case 'Android':
326+
return $this->toAndroid();
327+
case 'web':
328+
return $this->toWeb();
329+
default:
330+
return $this->toiOS();
331+
}
283332
}
284333

285334
/**
@@ -338,6 +387,30 @@ public function toAndroid()
338387
return $message;
339388
}
340389

390+
/**
391+
* Format the message for web.
392+
*
393+
* @return array
394+
*/
395+
public function toWeb()
396+
{
397+
$message = [
398+
'web' => [
399+
'notification' => array_filter([
400+
'title' => $this->title,
401+
'body' => $this->body,
402+
'sound' => $this->sound,
403+
'icon' => $this->icon,
404+
'deep_link' => $this->link,
405+
]),
406+
],
407+
];
408+
409+
$this->formatMessage($message);
410+
411+
return $message;
412+
}
413+
341414
/**
342415
* Return the current platform.
343416
*

0 commit comments

Comments
 (0)