Skip to content

Commit

Permalink
Merge pull request #37 from nonanerz/apns_critical_sound
Browse files Browse the repository at this point in the history
apns critical alerts
  • Loading branch information
mcfedr authored Jun 22, 2022
2 parents e704389 + 61a7013 commit 44eebd7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Mcfedr/AwsPushBundle/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ class Message implements \JsonSerializable
*/
private $sound;

/**
* Use these keys to configure the sound for a critical alert.
* APNS only. It will be in aps.sound.
* https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification#2990112.
*
* @var ?array
*/
private $apnsSound;

/**
* The background notification flag.
* Including this key means that when your app is launched in the background or resumed.
Expand Down Expand Up @@ -377,6 +386,18 @@ public function setSound(?string $sound): self
return $this;
}

public function getApnsSound(): ?array
{
return $this->apnsSound;
}

public function setApnsSound(?array $apnsSound): self
{
$this->apnsSound = $apnsSound;

return $this;
}

public function getTitle(): ?string
{
return $this->title;
Expand Down Expand Up @@ -783,6 +804,10 @@ private function getApnsJsonInner(?string $text)
$apns['aps']['sound'] = $this->sound;
}

if (null !== $this->apnsSound) {
$apns['aps']['sound'] = $this->apnsSound;
}

if (null !== $this->threadId) {
$apns['aps']['thread-id'] = $this->threadId;
}
Expand Down Expand Up @@ -1013,7 +1038,7 @@ private function getTrimmedData(callable $inner, int $limit, string $error)
$innerData = $inner($this->text);
$innerJson = json_encode($innerData, JSON_UNESCAPED_UNICODE);
if (($innerJsonLength = \strlen($innerJson)) > $limit) {
//Note that strlen returns the byte length of the string
// Note that strlen returns the byte length of the string
if ($this->allowTrimming && $this->text && ($textLength = \strlen($this->text)) > ($cut = $innerJsonLength - $limit)) {
$innerData = $inner(mb_strcut($this->text, 0, $textLength - $cut - 3, 'utf8').'...');
} else {
Expand Down
25 changes: 25 additions & 0 deletions tests/Mcfedr/AwsPushBundle/Tests/Message/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1666,4 +1666,29 @@ public function testPlatforms(): void
$this->assertEquals([Message::PLATFORM_FCM], $message->getPlatforms());
$this->assertTrue($message->isPlatformsCustomized());
}

public function testApnsSound(): void
{
$message = new Message(Lorem::text(1000));
$message->setApnsSound([
'critical' => 1,
'name' => 'sound.caf',
'volume' => 1,
]);

$message->setSound('sound.caf');

$string = (string) $message;
$data = json_decode($string, true);

$gcmData = json_decode($data['GCM'], true);
$this->assertEquals($gcmData['data']['sound'], 'sound.caf');

$apnsData = json_decode($data['APNS'], true);
$this->assertIsArray($apnsData['aps']['sound']);
$this->assertEquals($apnsData['aps']['sound']['name'], 'sound.caf');

$admData = json_decode($data['ADM'], true);
$this->assertEquals($admData['data']['sound'], 'sound.caf');
}
}

0 comments on commit 44eebd7

Please sign in to comment.