Skip to content

Commit c015baa

Browse files
authored
Merge pull request #19 from mgcastelberg/android-image-notification
Co-authored-by: Manuel Gómez <mgcastelberg@hotmail.com> feat: set image for android notification
2 parents 4e8caab + f621989 commit c015baa

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ service_account.json
8585
report/
8686

8787
# PHP Unit Local Test Configureation
88-
phpunit.local.xml
88+
phpunit.local.xml
89+
90+
# PHP Unit Local Test Cache
91+
.phpunit.result.cache
92+
93+
# Discard local php unit test
94+
phpunit.phar

src/Config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ function setPriority($priority) {
5757
return null;
5858
}
5959

60+
/**
61+
* @param $image : string
62+
* @return mixed
63+
*/
64+
function setImage($image_url) {
65+
$this -> androidConfig -> setImage($image_url);
66+
return null;
67+
}
68+
6069
/**
6170
* @param $time : seconds
6271
* @return mixed

src/Config/AndroidConfig.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ function setPriority($priority) {
3838
return null;
3939
}
4040

41+
/**
42+
* @param $image : string
43+
* @return mixed
44+
*/
45+
function setImage($image) {
46+
$payload = array_merge($this -> payload, array('notification' => ['image'=>$image]));
47+
$this -> payload = $payload;
48+
return null;
49+
}
50+
4151
/**
4252
* @param $time
4353
* @return mixed

tests/AndroidConfigTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public function testSetPriority() {
3333
$this -> assertArrayHasKey('priority', $payload['android']);
3434
}
3535

36+
function testSetImage() {
37+
$instance = new AndroidConfig();
38+
$instance -> setImage(FCMTest::TEST_IMAGE);
39+
$payload = $instance -> getPayload();
40+
41+
$this -> assertArrayHasKey('android',$payload);
42+
$this -> assertArrayHasKey('notification', $payload['android']);
43+
$this -> assertArrayHasKey('image', $payload['android']['notification']);
44+
}
45+
3646
public function testSetTimeToLive() {
3747
$instance = new AndroidConfig();
3848
$instance -> setTimeToLive(4.0);

tests/FCMTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use phpFCMv1\Client;
1616
use phpFCMv1\Notification;
1717
use phpFCMv1\Recipient;
18+
use phpFCMv1\Config;
1819
use \PHPUnit\Framework\TestCase;
1920

2021
class FCMTest extends TestCase {
@@ -24,6 +25,7 @@ class FCMTest extends TestCase {
2425

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

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

54+
public function testBuildWithImage() {
55+
$fcm = $this -> buildNotificationImage(self::TEST_TITLE, self::TEST_BODY);
56+
$payload = $fcm -> getPayload();
57+
58+
$expected = array(
59+
'token' => self::DEVICE_TOKEN,
60+
'notification' => array(
61+
'title' => self::TEST_TITLE,
62+
'body' => self::TEST_BODY
63+
),
64+
'android' => array(
65+
'notification' => array(
66+
'image' => self::TEST_IMAGE
67+
)
68+
)
69+
);
70+
$this -> assertArrayHasKey('message', $payload);
71+
$this -> assertEquals($expected, $payload['message']);
72+
}
73+
74+
public function testFireImage() {
75+
76+
$fcm = $this -> buildNotificationImage(self::TEST_TITLE, self::TEST_BODY);
77+
$result = $fcm -> fire();
78+
echo $result;
79+
80+
$this -> assertTrue($result);
81+
}
82+
5283
public function testFireWithIncorrectPayload() {
5384
// $this -> markTestSkipped(__METHOD__ . ' already passed');
5485
$fcm = $this -> buildNotification(self::TEST_TITLE, self::TEST_BODY);
@@ -82,6 +113,29 @@ public function buildNotification($TEST_TITLE, $TEST_BODY, CommonConfig $config
82113
return $fcm;
83114
}
84115

116+
/**
117+
* @param $TEST_TITLE
118+
* @param $TEST_BODY
119+
* @param CommonConfig|null $config
120+
* @return Client
121+
*/
122+
public function buildNotificationImage($TEST_TITLE, $TEST_BODY, CommonConfig $config = null) {
123+
$recipient = new Recipient();
124+
$recipient -> setSingleRecipient(self::DEVICE_TOKEN);
125+
126+
$notification = new Notification();
127+
$notification -> setNotification($TEST_TITLE, $TEST_BODY);
128+
129+
$config = new Config();
130+
$config -> setImage(self::TEST_IMAGE);
131+
132+
$fcm = new Client(self::KEY_FILE);
133+
$fcm -> setValidateOnly(true);
134+
$fcm -> build($recipient, $notification, null, $config);
135+
136+
return $fcm;
137+
}
138+
85139
/**
86140
* @param $config
87141
* @return bool

0 commit comments

Comments
 (0)