Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit e1f8f81

Browse files
committed
Fix #3 - Add support for fcmOptions
1 parent 47d1918 commit e1f8f81

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/Message/Options.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ class Options implements Arrayable
6969
*/
7070
protected $directBootOk = false;
7171

72+
/**
73+
* @internal
74+
*
75+
* @var string|null
76+
*/
77+
protected $analyticsLabel = null;
78+
7279
/**
7380
* Options constructor.
7481
*
@@ -85,6 +92,7 @@ public function __construct(OptionsBuilder $builder)
8592
$this->restrictedPackageName = $builder->getRestrictedPackageName();
8693
$this->isDryRun = $builder->isDryRun();
8794
$this->directBootOk = $builder->isDirectBootOk();
95+
$this->analyticsLabel = $builder->getFcmOptionsAnalyticsLabel();
8896
}
8997

9098
/**
@@ -99,6 +107,9 @@ public function toArray()
99107
$delayWhileIdle = $this->delayWhileIdle ? true : null;
100108
$dryRun = $this->isDryRun ? true : null;
101109
$directBootOk = $this->directBootOk ? true : null;
110+
$fcmOptions = $this->analyticsLabel !== null ? [
111+
'analytics_label' => $this->analyticsLabel
112+
] : null;
102113

103114
$options = [
104115
'collapse_key' => $this->collapseKey,
@@ -110,6 +121,7 @@ public function toArray()
110121
'restricted_package_name' => $this->restrictedPackageName,
111122
'dry_run' => $dryRun,
112123
'direct_boot_ok' => $directBootOk,
124+
'fcm_options' => $fcmOptions,
113125
];
114126

115127
return array_filter($options);

src/Message/OptionsBuilder.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class OptionsBuilder
7474
*/
7575
protected $directBootOk;
7676

77+
/**
78+
* @internal
79+
*
80+
* @var string|null
81+
*/
82+
protected $analyticsLabel = null;
83+
7784
/**
7885
* This parameter identifies a group of messages
7986
* A maximum of 4 different collapse keys is allowed at any given time.
@@ -226,6 +233,22 @@ public function setDryRun($isDryRun)
226233
return $this;
227234
}
228235

236+
/**
237+
* This parameter sets the Analytics label
238+
*
239+
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidFcmOptions
240+
*
241+
* @param string $analyticsLabel
242+
*
243+
* @return \LaravelFCM\Message\OptionsBuilder
244+
*/
245+
public function setFcmOptionsAnalyticsLabel($analyticsLabel)
246+
{
247+
$this->analyticsLabel = $analyticsLabel;
248+
249+
return $this;
250+
}
251+
229252
/**
230253
* Get the collapseKey.
231254
*
@@ -246,6 +269,18 @@ public function getPriority()
246269
return $this->priority;
247270
}
248271

272+
/**
273+
* Get the Analytics label
274+
*
275+
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidFcmOptions
276+
*
277+
* @return null|string
278+
*/
279+
public function getFcmOptionsAnalyticsLabel()
280+
{
281+
return $this->analyticsLabel;
282+
}
283+
249284
/**
250285
* is content available.
251286
*

tests/MessageTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,47 @@ public function testBuildOptionsDirectBootOk()
8080
$this->assertJsonStringEqualsJsonString($targetFull, $json);
8181
}
8282

83-
public function testItConstructAValidJson_with_data()
83+
public function testBuildOptionsFcmOptionsAnalyticsLabel()
84+
{
85+
$targetPartial = '{
86+
"direct_boot_ok": true,
87+
"content_available":true
88+
}';
89+
90+
$targetFull = '{
91+
"collapse_key":"collapseKey",
92+
"content_available":true,
93+
"priority":"high",
94+
"delay_while_idle":true,
95+
"time_to_live":200,
96+
"fcm_options": {
97+
"analytics_label": "UA-xxxxxxx"
98+
},
99+
"dry_run": true,
100+
"direct_boot_ok": true
101+
}';
102+
103+
$optionBuilder = new OptionsBuilder();
104+
105+
$optionBuilder->setDirectBootOk(true);
106+
$optionBuilder->setContentAvailable(true);
107+
108+
$json = json_encode($optionBuilder->build()->toArray());
109+
$this->assertJsonStringEqualsJsonString($targetPartial, $json);
110+
$this->assertNull($optionBuilder->getFcmOptionsAnalyticsLabel());
111+
$optionBuilder->setPriority(OptionsPriorities::high)
112+
->setCollapseKey('collapseKey')
113+
->setDelayWhileIdle(true)
114+
->setDryRun(true)
115+
->setFcmOptionsAnalyticsLabel('UA-xxxxxxx')
116+
->setTimeToLive(200);
117+
118+
$this->assertSame('UA-xxxxxxx', $optionBuilder->getFcmOptionsAnalyticsLabel());
119+
$json = json_encode($optionBuilder->build()->toArray());
120+
$this->assertJsonStringEqualsJsonString($targetFull, $json);
121+
}
122+
123+
public function testItConstructAValidJsonWithData()
84124
{
85125
$targetAdd = '{
86126
"first_data":"first",

0 commit comments

Comments
 (0)