Skip to content

Commit 23af65e

Browse files
authored
Implemented web channel (#52)
1 parent 5119027 commit 23af65e

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-8
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ class AccountApproved extends Notification
8686

8787
### Available Message methods
8888

89-
- `platform('')`: Accepts a string value of `iOS` or `Android`.
89+
- `platform('')`: Accepts a string value of `iOS`, `Android` or `web`.
9090
- `iOS()`: Sets the platform value to iOS.
9191
- `android()`: Sets the platform value to Android.
92+
- `web()`: Sets the platform value to web.
93+
- `link()`: Accepts a string value which will lead to URI specified on notification click.
9294
- `title('')`: Accepts a string value for the title.
9395
- `body('')`: Accepts a string value for the body.
9496
- `sound('')`: Accepts a string value for the notification sound file. Notice that if you leave blank the default sound value will be `default`.
@@ -118,11 +120,31 @@ public function toPushNotification($notifiable)
118120
```
119121

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

125-
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.
127+
By default, the pusher "interest" messages will be sent to will be defined using the {notifiable}.{id} convention, for example `App.User.1`,
128+
however you can change this behaviour by including a `routeNotificationFor()` in the notifiable class.
129+
130+
I.e. if you are pushing notification on ``User`` model, you can go to `App\User` class and implement method:
131+
132+
```
133+
public function routeNotificationFor($channel)
134+
{
135+
if($channel === 'PusherPushNotifications'){
136+
return 'your.custom.interest.string';
137+
}
138+
139+
$class = str_replace('\\', '.', get_class($this));
140+
141+
return $class.'.'.$this->getKey();
142+
}
143+
```
144+
145+
146+
PusherPushNotifications()` in the notifiable class method that
147+
returns the interest name.
126148

127149
## Changelog
128150

src/PusherChannel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Events\Dispatcher;
66
use Illuminate\Notifications\Events\NotificationFailed;
77
use Illuminate\Notifications\Notification;
8+
use Illuminate\Support\Arr;
89
use Pusher\PushNotifications\PushNotifications;
910
use Throwable;
1011

@@ -45,7 +46,7 @@ public function send($notifiable, Notification $notification)
4546

4647
try {
4748
$this->beamsClient->publishToInterests(
48-
is_array($interest) ? $interest : [$interest],
49+
Arr::wrap($interest),
4950
$notification->toPushNotification($notifiable)->toArray()
5051
);
5152
} catch (Throwable $exception) {

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
*
@@ -92,7 +97,7 @@ public function __construct($body = '')
9297
*/
9398
public function platform($platform)
9499
{
95-
if (! in_array($platform, ['iOS', 'Android'])) {
100+
if (! in_array($platform, ['iOS', 'Android', 'web'])) {
96101
throw CouldNotCreateMessage::invalidPlatformGiven($platform);
97102
}
98103

@@ -125,6 +130,18 @@ public function android()
125130
return $this;
126131
}
127132

133+
/**
134+
* Set the platform to web.
135+
*
136+
* @return $this
137+
*/
138+
public function web()
139+
{
140+
$this->platform = 'web';
141+
142+
return $this;
143+
}
144+
128145
/**
129146
* Set an extra message to be sent to Android.
130147
*
@@ -151,6 +168,19 @@ public function withiOS(self $message)
151168
return $this;
152169
}
153170

171+
/**
172+
* Set an extra message to be sent to web.
173+
*
174+
* @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
175+
* @return $this
176+
*/
177+
public function withWeb(self $message)
178+
{
179+
$this->withExtra($message->web());
180+
181+
return $this;
182+
}
183+
154184
/**
155185
* Set an extra message to be sent to another platform.
156186
*
@@ -236,6 +266,20 @@ public function badge($value)
236266
return $this;
237267
}
238268

269+
/**
270+
* Set the message link.
271+
*
272+
* @param string $value
273+
*
274+
* @return $this
275+
*/
276+
public function link($value)
277+
{
278+
$this->link = $value;
279+
280+
return $this;
281+
}
282+
239283
/**
240284
* @param string $key
241285
* @param mixed $value
@@ -256,9 +300,14 @@ public function setOption($key, $value)
256300
*/
257301
public function toArray()
258302
{
259-
return $this->platform === 'iOS'
260-
? $this->toiOS()
261-
: $this->toAndroid();
303+
switch ($this->platform) {
304+
case 'Android':
305+
return $this->toAndroid();
306+
case 'web':
307+
return $this->toWeb();
308+
default:
309+
return $this->toiOS();
310+
}
262311
}
263312

264313
/**
@@ -309,6 +358,30 @@ public function toAndroid()
309358
return $message;
310359
}
311360

361+
/**
362+
* Format the message for web.
363+
*
364+
* @return array
365+
*/
366+
public function toWeb()
367+
{
368+
$message = [
369+
'web' => [
370+
'notification' => array_filter([
371+
'title' => $this->title,
372+
'body' => $this->body,
373+
'sound' => $this->sound,
374+
'icon' => $this->icon,
375+
'deep_link' => $this->link,
376+
]),
377+
],
378+
];
379+
380+
$this->formatMessage($message);
381+
382+
return $message;
383+
}
384+
312385
/**
313386
* Return the current platform.
314387
*

0 commit comments

Comments
 (0)