Skip to content

Commit

Permalink
Apns: Use upstream enum for priority
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Nov 22, 2024
1 parent d17b79e commit 27a18c2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 69 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"require": {
"psr/log": ">=1.1",
"m2mobi/apns-php": "~3.0",
"m2mobi/apns-php": "dev-master",
"phpmailer/phpmailer": "~6.4",
"rmccue/requests": "~2.0",
"php": ">=8.1",
Expand Down
4 changes: 2 additions & 2 deletions decomposer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
},
"ApnsPHP": {
"url": "https://github.com/move-backend/apnsphp.git",
"version": "3.0.3",
"revision": "release/3.0.x",
"version": "4.0.0",
"revision": "master",
"psr0": {
"path": "/"
}
Expand Down
15 changes: 5 additions & 10 deletions src/Lunr/Vortex/APNS/APNSPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Lunr\Vortex\APNS;

use ReflectionClass;
use ApnsPHP\Message\Priority;

/**
* Apple Push Notification Service Payload Generator.
Expand All @@ -31,7 +31,7 @@ public function __construct()
{
$this->elements = [];

$this->elements['priority'] = APNSPriority::HIGH;
$this->elements['priority'] = Priority::Immediately;
}

/**
Expand Down Expand Up @@ -88,18 +88,13 @@ public function set_topic(string $topic): self
/**
* Mark the notification priority.
*
* @param int $priority Notification priority value.
* @param Priority $priority Notification priority value.
*
* @return APNSPayload Self Reference
*/
public function set_priority(int $priority): self
public function set_priority(Priority $priority): self
{
$priority_class = new ReflectionClass('\Lunr\Vortex\APNS\APNSPriority');

if (in_array($priority, array_values($priority_class->getConstants())))
{
$this->elements['priority'] = $priority;
}
$this->elements['priority'] = $priority;

return $this;
}
Expand Down
33 changes: 0 additions & 33 deletions src/Lunr/Vortex/APNS/APNSPriority.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use ApnsPHP\Exception as ApnsPHPException;
use ApnsPHP\Message\Exception as MessageException;
use ApnsPHP\Message\Priority;
use ApnsPHP\Push\Exception as PushException;
use Lunr\Vortex\Email\EmailPayload;
use Lunr\Vortex\FCM\FCMPayload;
Expand Down Expand Up @@ -124,7 +125,7 @@ public function testPushConstructsCorrectPayload(): void
'mutable_content' => TRUE,
'content_available' => TRUE,
'topic' => 'com.company.app',
'priority' => 5,
'priority' => Priority::ConsiderPowerUsage,
'collapse_key' => 'key',
'identifier' => 'identifier',
'yo' => 'he',
Expand Down
23 changes: 5 additions & 18 deletions src/Lunr/Vortex/APNS/Tests/APNSPayloadSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Lunr\Vortex\APNS\Tests;

use ApnsPHP\Message\Priority;

/**
* This class contains tests for the setters of the APNSPayload class.
*
Expand Down Expand Up @@ -311,27 +313,12 @@ public function testSetTopicReturnsSelfReference(): void
*/
public function testPriority(): void
{
$this->class->set_priority(5);

$value = $this->get_reflection_property_value('elements');

$this->assertArrayHasKey('priority', $value);
$this->assertEquals(5, $value['priority']);
}

/**
* Test set_priority() works correctly.
*
* @covers \Lunr\Vortex\APNS\APNSPayload::set_priority
*/
public function testPriorityIgnoresWrongValues(): void
{
$this->class->set_priority(7);
$this->class->set_priority(Priority::ConsiderPowerUsage);

$value = $this->get_reflection_property_value('elements');

$this->assertArrayHasKey('priority', $value);
$this->assertEquals(10, $value['priority']);
$this->assertEquals(Priority::ConsiderPowerUsage, $value['priority']);
}

/**
Expand All @@ -341,7 +328,7 @@ public function testPriorityIgnoresWrongValues(): void
*/
public function testSetPriorityReturnsSelfReference(): void
{
$this->assertSame($this->class, $this->class->set_priority(5));
$this->assertSame($this->class, $this->class->set_priority(Priority::ConsiderPowerUsage));
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Lunr/Vortex/FCM/FCMApnsPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Lunr\Vortex\FCM;

use Lunr\Vortex\APNS\APNSPriority;
use ReflectionClass;

/**
Expand Down Expand Up @@ -114,13 +113,13 @@ public function set_mutable_content(bool $mutable): static
*/
public function set_priority(string $priority): static
{
$priority = strtoupper($priority);
$priority = strtolower($priority);

$priority_class = new ReflectionClass(APNSPriority::class);
$priority_class = new ReflectionClass(FCMApnsPriority::class);
$priorities = $priority_class->getConstants();
if (in_array($priority, array_keys($priorities)))
{
$this->elements['headers']['apns-priority'] = $priorities[$priority];
$this->elements['headers']['apns-priority'] = $priorities[$priority]->value;
}

return $this;
Expand Down
36 changes: 36 additions & 0 deletions src/Lunr/Vortex/FCM/FCMApnsPriority.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file contains priority types for Firebase Cloud Messaging Notifications to APNS.
*
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Vortex\FCM;

/**
* Firebase Cloud Messaging APNS Priority Types.
*/
enum FCMApnsPriority: int
{

/**
* Prioritize the device’s power considerations over all other factors for delivery,
* and prevent awakening the device
*/
case low = 1;

/**
* Send the notification based on power considerations on the user’s device
*/
case default = 5;

/**
* Send the notification immediately
*/
case high = 10;

}

?>

0 comments on commit 27a18c2

Please sign in to comment.