Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 58c1171

Browse files
Zoran Lorkovicbarryvdh
authored andcommitted
Adding sound option to message based on GCM docs (#19)
* Adding sound option to message based on GCM docs * Fixing sound test * Fixing problem with closure cannot be serialised. * Fixing code styling
1 parent 34780a1 commit 58c1171

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/GcmChannel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ protected function getPacket($tokens, $message)
8080
$packet->setNotification([
8181
'title' => $message->title,
8282
'body' => $message->message,
83+
'sound' => $message->sound,
8384
] + $message->data);
8485

8586
return $packet;
@@ -100,7 +101,7 @@ protected function handleFailedNotifications($notifiable, Notification $notifica
100101
}
101102

102103
$this->events->fire(
103-
new NotificationFailed($notifiable, $notification, $this, [
104+
new NotificationFailed($notifiable, $notification, get_class($this), [
104105
'token' => $token,
105106
'error' => $result['error'],
106107
])

src/GcmMessage.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class GcmMessage
77
const PRIORITY_NORMAL = 'normal';
88
const PRIORITY_HIGH = 'high';
99

10+
const DEFAULT_SOUND = 'default';
11+
1012
/**
1113
* The title of the notification.
1214
*
@@ -36,6 +38,13 @@ class GcmMessage
3638
*/
3739
public $priority = self::PRIORITY_NORMAL;
3840

41+
/**
42+
* Notification sound
43+
*
44+
* @var string
45+
*/
46+
public $sound = self::DEFAULT_SOUND;
47+
3948
/**
4049
* Additional data of the notification.
4150
*
@@ -51,23 +60,25 @@ class GcmMessage
5160
*
5261
* @return static
5362
*/
54-
public static function create($title = null, $message = null, $data = [], $priority = self::PRIORITY_NORMAL)
63+
public static function create($title = null, $message = null, $data = [], $priority = self::PRIORITY_NORMAL, $sound = self::DEFAULT_SOUND)
5564
{
56-
return new static($title, $message, $data, $priority);
65+
return new static($title, $message, $data, $priority, $sound);
5766
}
5867

5968
/**
6069
* @param string|null $title
6170
* @param string|null $message
6271
* @param array $data
6372
* @param string $priority
73+
* @param string $sound
6474
*/
65-
public function __construct($title = null, $message = null, $data = [], $priority = self::PRIORITY_NORMAL)
75+
public function __construct($title = null, $message = null, $data = [], $priority = self::PRIORITY_NORMAL, $sound = self::DEFAULT_SOUND)
6676
{
6777
$this->title = $title;
6878
$this->message = $message;
6979
$this->data = $data;
7080
$this->priority = $priority;
81+
$this->sound = $sound;
7182
}
7283

7384
/**
@@ -126,6 +137,20 @@ public function priority($priority)
126137
return $this;
127138
}
128139

140+
/**
141+
* Set the sound for notification
142+
*
143+
* @param string $sound
144+
*
145+
* @return $this
146+
*/
147+
public function sound($sound)
148+
{
149+
$this->sound = $sound;
150+
151+
return $this;
152+
}
153+
129154
/**
130155
* Add data to the notification.
131156
*

tests/GcmMessageTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ public function setUp()
1919
/** @test */
2020
public function it_can_accept_parameters_when_constructing_a_message()
2121
{
22-
$message = new GcmMessage('myTitle', 'myMessage', ['foo' => 'bar'], GcmMessage::PRIORITY_HIGH);
22+
$message = new GcmMessage('myTitle', 'myMessage', ['foo' => 'bar'], GcmMessage::PRIORITY_HIGH, GcmMessage::DEFAULT_SOUND);
2323
$this->assertEquals('myTitle', $message->title);
2424
$this->assertEquals('myMessage', $message->message);
2525
$this->assertEquals('bar', $message->data['foo']);
2626
$this->assertEquals(GcmMessage::PRIORITY_HIGH, $message->priority);
27+
$this->assertEquals(GcmMessage::DEFAULT_SOUND, $message->sound);
2728
}
2829

2930
/** @test */
3031
public function it_provides_a_create_method()
3132
{
32-
$message = GcmMessage::create('myTitle', 'myMessage', ['foo' => 'bar'], GcmMessage::PRIORITY_HIGH);
33+
$message = GcmMessage::create('myTitle', 'myMessage', ['foo' => 'bar'], GcmMessage::PRIORITY_HIGH, GcmMessage::DEFAULT_SOUND);
3334
$this->assertEquals('myTitle', $message->title);
3435
$this->assertEquals('myMessage', $message->message);
3536
$this->assertEquals('bar', $message->data['foo']);
3637
$this->assertEquals(GcmMessage::PRIORITY_HIGH, $message->priority);
38+
$this->assertEquals(GcmMessage::DEFAULT_SOUND, $message->sound);
3739
}
3840

3941
/** @test */
@@ -69,4 +71,17 @@ public function it_can_set_the_priority()
6971
$this->message->priority(GcmMessage::PRIORITY_HIGH);
7072
$this->assertEquals(GcmMessage::PRIORITY_HIGH, $this->message->priority);
7173
}
74+
75+
/** @test */
76+
public function it_has_default_sound()
77+
{
78+
$this->assertEquals(GcmMessage::DEFAULT_SOUND, $this->message->sound);
79+
}
80+
81+
/** @test */
82+
public function it_can_set_the_sound()
83+
{
84+
$this->message->sound(GcmMessage::DEFAULT_SOUND);
85+
$this->assertEquals(GcmMessage::DEFAULT_SOUND, $this->message->sound);
86+
}
7287
}

0 commit comments

Comments
 (0)