Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set image for android notification #19

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ service_account.json
report/

# PHP Unit Local Test Configureation
phpunit.local.xml
phpunit.local.xml

# PHP Unit Local Test Cache
.phpunit.result.cache

# Discard local php unit test
phpunit.phar
9 changes: 9 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ function setPriority($priority) {
return null;
}

/**
* @param $image : string
* @return mixed
*/
function setImage($image_url) {
$this -> androidConfig -> setImage($image_url);
return null;
}

/**
* @param $time : seconds
* @return mixed
Expand Down
10 changes: 10 additions & 0 deletions src/Config/AndroidConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ function setPriority($priority) {
return null;
}

/**
* @param $image : string
* @return mixed
*/
function setImage($image) {
$payload = array_merge($this -> payload, array('notification' => ['image'=>$image]));
$this -> payload = $payload;
return null;
}

/**
* @param $time
* @return mixed
Expand Down
10 changes: 10 additions & 0 deletions tests/AndroidConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function testSetPriority() {
$this -> assertArrayHasKey('priority', $payload['android']);
}

function testSetImage() {
$instance = new AndroidConfig();
$instance -> setImage(FCMTest::TEST_IMAGE);
$payload = $instance -> getPayload();

$this -> assertArrayHasKey('android',$payload);
$this -> assertArrayHasKey('notification', $payload['android']);
$this -> assertArrayHasKey('image', $payload['android']['notification']);
}

public function testSetTimeToLive() {
$instance = new AndroidConfig();
$instance -> setTimeToLive(4.0);
Expand Down
54 changes: 54 additions & 0 deletions tests/FCMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use phpFCMv1\Client;
use phpFCMv1\Notification;
use phpFCMv1\Recipient;
use phpFCMv1\Config;
use \PHPUnit\Framework\TestCase;

class FCMTest extends TestCase {
Expand All @@ -24,6 +25,7 @@ class FCMTest extends TestCase {

const TEST_TITLE = 'Testing from Code';
const TEST_BODY = 'Using phpFCMv1!';
const TEST_IMAGE = 'https://fastly.picsum.photos/id/982/300/200.jpg?hmac=rd0sm-A6tmsIiavEE2p9ynoCVr9RDUCjJMjqOH7_pvA';

public function testBuild() {
$fcm = $this -> buildNotification(self::TEST_TITLE, self::TEST_BODY);
Expand All @@ -49,6 +51,35 @@ public function testFire() {
$this -> assertTrue($result);
}

public function testBuildWithImage() {
$fcm = $this -> buildNotificationImage(self::TEST_TITLE, self::TEST_BODY);
$payload = $fcm -> getPayload();

$expected = array(
'token' => self::DEVICE_TOKEN,
'notification' => array(
'title' => self::TEST_TITLE,
'body' => self::TEST_BODY
),
'android' => array(
'notification' => array(
'image' => self::TEST_IMAGE
)
)
);
$this -> assertArrayHasKey('message', $payload);
$this -> assertEquals($expected, $payload['message']);
}

public function testFireImage() {

$fcm = $this -> buildNotificationImage(self::TEST_TITLE, self::TEST_BODY);
$result = $fcm -> fire();
echo $result;

$this -> assertTrue($result);
}

public function testFireWithIncorrectPayload() {
// $this -> markTestSkipped(__METHOD__ . ' already passed');
$fcm = $this -> buildNotification(self::TEST_TITLE, self::TEST_BODY);
Expand Down Expand Up @@ -82,6 +113,29 @@ public function buildNotification($TEST_TITLE, $TEST_BODY, CommonConfig $config
return $fcm;
}

/**
* @param $TEST_TITLE
* @param $TEST_BODY
* @param CommonConfig|null $config
* @return Client
*/
public function buildNotificationImage($TEST_TITLE, $TEST_BODY, CommonConfig $config = null) {
$recipient = new Recipient();
$recipient -> setSingleRecipient(self::DEVICE_TOKEN);

$notification = new Notification();
$notification -> setNotification($TEST_TITLE, $TEST_BODY);

$config = new Config();
$config -> setImage(self::TEST_IMAGE);

$fcm = new Client(self::KEY_FILE);
$fcm -> setValidateOnly(true);
$fcm -> build($recipient, $notification, null, $config);

return $fcm;
}

/**
* @param $config
* @return bool
Expand Down
Loading