Skip to content

Commit 43ebafb

Browse files
committed
1 parent 17b68b6 commit 43ebafb

File tree

6 files changed

+267
-68
lines changed

6 files changed

+267
-68
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All Notable changes to `laravel-notification-channels/webpush` will be documented in this file
44

5+
## 3.0.0 - 2017-11-15
6+
7+
- Removed `id` and `create` methods from `WebPushMessage`.
8+
- Added `badge`, `dir`, `image`, `lang`, `renotify`, `requireInteraction`, `tag`, `vibrate`, `data` methods on `WebPushMessage`.
9+
510
## 2.0.0 - 2017-10-23
611

712
- Added support for package discovery.

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,20 @@ class AccountApproved extends Notification
8888

8989
public function toWebPush($notifiable, $notification)
9090
{
91-
return WebPushMessage::create()
92-
// ->id($notification->id)
91+
return (new WebPushMessage)
9392
->title('Approved!')
9493
->icon('/approved-icon.png')
9594
->body('Your account was approved!')
9695
->action('View account', 'view_account');
96+
// ->data(['id' => $notification->id])
97+
// ->badge()
98+
// ->dir()
99+
// ->image()
100+
// ->lang()
101+
// ->renotify()
102+
// ->requireInteraction()
103+
// ->tag()
104+
// ->vibrate()
97105
}
98106
}
99107
```

src/WebPushMessage.php

Lines changed: 172 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,104 +2,144 @@
22

33
namespace NotificationChannels\WebPush;
44

5+
/**
6+
* @link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#Parameters
7+
*/
58
class WebPushMessage
69
{
710
/**
8-
* The notification id.
9-
*
1011
* @var string
1112
*/
12-
protected $id = null;
13+
protected $title;
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $actions = [];
1319

1420
/**
15-
* The notification title.
16-
*
1721
* @var string
1822
*/
19-
protected $title;
23+
protected $badge;
2024

2125
/**
22-
* The notification body.
23-
*
2426
* @var string
2527
*/
2628
protected $body;
2729

2830
/**
29-
* The notification icon.
30-
*
3131
* @var string
3232
*/
33-
protected $icon = null;
33+
protected $dir;
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $icon;
39+
40+
/**
41+
* @var string
42+
*/
43+
protected $image;
44+
45+
/**
46+
* @var string
47+
*/
48+
protected $lang;
49+
50+
/**
51+
* @var bool
52+
*/
53+
protected $renotify;
54+
55+
/**
56+
* @var bool
57+
*/
58+
protected $requireInteraction;
59+
60+
/**
61+
* @var string
62+
*/
63+
protected $tag;
3464

3565
/**
36-
* The notification actions.
37-
*
3866
* @var array
3967
*/
40-
protected $actions = [];
68+
protected $vibrate;
69+
70+
/**
71+
* @var mixed
72+
*/
73+
protected $data;
4174

4275
/**
43-
* @param string $body
76+
* Set the notification title.
4477
*
45-
* @return static
78+
* @param string $value
79+
* @return $this
4680
*/
47-
public static function create($body = '')
81+
public function title($value)
4882
{
49-
return new static($body);
83+
$this->title = $value;
84+
85+
return $this;
5086
}
5187

5288
/**
53-
* @param string $body
89+
* Add a notification action.
90+
*
91+
* @param string $title
92+
* @param string $action
93+
* @return $this
5494
*/
55-
public function __construct($body = '')
95+
public function action($title, $action)
5696
{
57-
$this->title = '';
97+
$this->actions[] = compact('title', 'action');
5898

59-
$this->body = $body;
99+
return $this;
60100
}
61101

62102
/**
63-
* Set the notification id.
103+
* Set the notification badge.
64104
*
65105
* @param string $value
66106
* @return $this
67107
*/
68-
public function id($value)
108+
public function badge($value)
69109
{
70-
$this->id = $value;
110+
$this->badge = $value;
71111

72112
return $this;
73113
}
74114

75115
/**
76-
* Set the notification title.
116+
* Set the notification body.
77117
*
78118
* @param string $value
79119
* @return $this
80120
*/
81-
public function title($value)
121+
public function body($value)
82122
{
83-
$this->title = $value;
123+
$this->body = $value;
84124

85125
return $this;
86126
}
87127

88128
/**
89-
* Set the notification body.
129+
* Set the notification direction.
90130
*
91131
* @param string $value
92132
* @return $this
93133
*/
94-
public function body($value)
134+
public function dir($value)
95135
{
96-
$this->body = $value;
136+
$this->dir = $value;
97137

98138
return $this;
99139
}
100140

101141
/**
102-
* Set the notification icon.
142+
* Set the notification icon url.
103143
*
104144
* @param string $value
105145
* @return $this
@@ -112,15 +152,88 @@ public function icon($value)
112152
}
113153

114154
/**
115-
* Set an action.
155+
* Set the notification image url.
116156
*
117-
* @param string $title
118-
* @param string $action
157+
* @param string $value
119158
* @return $this
120159
*/
121-
public function action($title, $action)
160+
public function image($value)
122161
{
123-
$this->actions[] = compact('title', 'action');
162+
$this->image = $value;
163+
164+
return $this;
165+
}
166+
167+
/**
168+
* Set the notification language.
169+
*
170+
* @param string $value
171+
* @return $this
172+
*/
173+
public function lang($value)
174+
{
175+
$this->lang = $value;
176+
177+
return $this;
178+
}
179+
180+
/**
181+
* @param bool $value
182+
* @return $this
183+
*/
184+
public function renotify($value = true)
185+
{
186+
$this->renotify = $value;
187+
188+
return $this;
189+
}
190+
191+
/**
192+
* @param bool $value
193+
* @return $this
194+
*/
195+
public function requireInteraction($value = true)
196+
{
197+
$this->requireInteraction = $value;
198+
199+
return $this;
200+
}
201+
202+
/**
203+
* Set the notification tag.
204+
*
205+
* @param string $value
206+
* @return $this
207+
*/
208+
public function tag($value)
209+
{
210+
$this->tag = $value;
211+
212+
return $this;
213+
}
214+
215+
/**
216+
* Set the notification vibration pattern.
217+
*
218+
* @param array $value
219+
* @return $this
220+
*/
221+
public function vibrate($value)
222+
{
223+
$this->vibrate = $value;
224+
225+
return $this;
226+
}
227+
228+
/**
229+
* Set the notification arbitrary data.
230+
*
231+
* @param mixed $value
232+
* @return $this
233+
*/
234+
public function data($value)
235+
{
236+
$this->data = $value;
124237

125238
return $this;
126239
}
@@ -132,12 +245,27 @@ public function action($title, $action)
132245
*/
133246
public function toArray()
134247
{
135-
return [
136-
'id' => $this->id,
137-
'title' => $this->title,
138-
'body' => $this->body,
139-
'actions' => $this->actions,
140-
'icon' => $this->icon,
141-
];
248+
return collect([
249+
'title',
250+
'actions',
251+
'badge',
252+
'body',
253+
'dir',
254+
'icon',
255+
'image',
256+
'lang',
257+
'renotify',
258+
'requireInteraction',
259+
'tag',
260+
'vibrate',
261+
'data',
262+
])
263+
->mapWithKeys(function ($option) {
264+
return [$option => $this->{$option}];
265+
})
266+
->reject(function ($value) {
267+
return is_null($value);
268+
})
269+
->toArray();
142270
}
143271
}

tests/ChannelTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function it_can_send_a_notification()
4444
$this->testUser->updatePushSubscription('endpoint', 'key', 'token');
4545

4646
$this->channel->send($this->testUser, new TestNotification);
47+
48+
$this->assertTrue(true);
4749
}
4850

4951
/** @test */
@@ -81,6 +83,14 @@ public function it_will_delete_invalid_subscriptions()
8183
*/
8284
protected function getPayload()
8385
{
84-
return '{"id":1,"title":"Title","body":"Body","actions":[{"title":"Title","action":"Action"}],"icon":"Icon"}';
86+
return json_encode([
87+
'title' => 'Title',
88+
'actions' => [
89+
['title' => 'Title', 'action' => 'Action']
90+
],
91+
'body' => 'Body',
92+
'icon' => 'Icon',
93+
'data' => ['id' => 1],
94+
]);
8595
}
8696
}

0 commit comments

Comments
 (0)