Skip to content

Commit e42f1fa

Browse files
authored
Merge pull request #27 from ker0x/bugfix/issue-26
🐛 Allow to send message with data only
2 parents 54495ea + a587e97 commit e42f1fa

File tree

13 files changed

+244
-98
lines changed

13 files changed

+244
-98
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424

2525
- name: composer-cache
2626
id: composer-cache
27-
uses: actions/cache@v3
27+
uses: actions/cache@v4
2828
with:
2929
path: vendor
3030
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.lock') }}
@@ -36,16 +36,16 @@ jobs:
3636
run: composer install --prefer-dist --no-progress --no-interaction
3737

3838
- name: php-cs-fixer
39-
run: vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --ansi
39+
run: composer cs
4040

4141
- name: phpstan
42-
run: vendor/bin/phpstan analyse --memory-limit 256M
42+
run: composer lint
4343

4444
- name: phpunit
4545
env:
4646
FCM_OAUTH_TOKEN: ${{ secrets.FCM_OAUTH_TOKEN }}
4747
FCM_PROJECT_ID: ${{ secrets.FCM_PROJECT_ID }}
48-
run: vendor/bin/phpunit
48+
run: composer tests
4949

5050
- name: coverage
5151
uses: codecov/codecov-action@v3

CHANGELOG.md

Lines changed: 107 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,112 @@
22

33
The Fcm library follows [SemVer](http://semver.org/).
44

5+
## 3.x
6+
7+
> [!NOTE]
8+
> Version `3.x` of this library is a full rewrite using [PSR-18 HTTP Client](https://www.php-fig.org/psr/psr-18/) interface,
9+
> which means that **no** HTTP Client, like [Guzzle](https://github.com/guzzle/guzzle) or [httplug](https://github.com/php-http/httplug),
10+
> are provided within. If you already have one in your project, the package will **automatically discover it** and use it.
11+
> Otherwise You will need to require one separately.
12+
13+
### 3.2
14+
15+
> [!WARNING]
16+
> Version `3.2` introduce a BC break.
17+
> The signature of the `__construct()` method of the `Kerox\Fcm\Model\Message` class has changed, with the `$notification` parameter becoming the third argument and being optional.
18+
19+
```diff
20+
final class Message
21+
{
22+
- public Notification $notification;
23+
+ public ?Notification $notification = null;
24+
public ?string $token = null;
25+
public ?string $topic = null;
26+
public ?string $condition = null;
27+
28+
/**
29+
* @param array<string, string> $data
30+
*/
31+
public function __construct(
32+
- Notification|string $notification,
33+
Token|Topic|Condition $target,
34+
public array $data = [],
35+
+ Notification|string|null $notification = null,
36+
public ?AndroidConfig $android = null,
37+
public ?WebpushConfig $webpush = null,
38+
public ?ApnsConfig $apns = null,
39+
public ?FcmOptions $fcmOptions = null,
40+
) {
41+
+ if (null !== $notification) {
42+
$this->notification = \is_string($notification)
43+
? new Notification($notification)
44+
: $notification
45+
;
46+
+ }
47+
48+
match (true) {
49+
$target instanceof Token => $this->token = $target->__toString(),
50+
$target instanceof Topic => $this->topic = $target->__toString(),
51+
$target instanceof Condition => $this->condition = $target->__toString(),
52+
};
53+
}
54+
}
55+
```
56+
#### Before
57+
58+
````php
59+
$message = new Message(
60+
notification: 'Breaking News',
61+
target: new Topic('TopicA'),
62+
data: [
63+
'story_id' => 'story_12345',
64+
],
65+
);
66+
````
67+
68+
#### After
69+
70+
````php
71+
$message = new Message(
72+
target: new Topic('TopicA'),
73+
data: [
74+
'story_id' => 'story_12345',
75+
],
76+
notification: 'Breaking News',
77+
);
78+
````
79+
80+
### 3.1
81+
82+
#### What's Changed
83+
* :bug: Fix README by @ker0x in https://github.com/ker0x/fcm/pull/23 and https://github.com/ker0x/fcm/pull/25
84+
* :bug: Fix .gitattributes by @ker0x in https://github.com/ker0x/fcm/pull/24
85+
* :arrow_up: Bump Symfony components to `6.4`, allow Symfony 7 by @ker0x in https://github.com/ker0x/fcm/pull/25
86+
* :green_heart: Update CI workflow by @ker0x in https://github.com/ker0x/fcm/pull/25
87+
* :rotating_light: Fix PHP-CS-Fixer and PHPStan warning by @ker0x in https://github.com/ker0x/fcm/pull/25
88+
89+
**Full Changelog**: https://github.com/ker0x/fcm/compare/3.0.0...3.1.0
90+
91+
### 3.0
92+
93+
#### What's Changed
94+
* :art: Full package refactoring by @ker0x in https://github.com/ker0x/fcm/pull/21
95+
* :memo: Update README by @ker0x in https://github.com/ker0x/fcm/pull/22
96+
97+
**Full Changelog**: https://github.com/ker0x/fcm/compare/2.4.0...3.0.0
98+
599
## 2.x
6100

7-
Version `2.x` of this library is a full rewrite to be compliant with [HTTP v1 API](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages). If
8-
you are on Legacy HTTP API, then you should consider using version `1.x`
9-
10-
**Changelog** (since [`2.2.0`](https://github.com/ker0x/fcm/compare/2.2.0...2.3.0))
11-
12-
- 2.3.0 (2021-02)
13-
- Bump minimal PHP version to `7.3`
14-
- Bump Guzzle version to `7.2`
15-
- Bump PHPUnit version to `8.`
16-
- Update `.gitattributes`
17-
- Update CI workflow
18-
19-
**Changelog** (since [`2.1.0`](https://github.com/ker0x/fcm/compare/2.1.0...2.2.0))
20-
21-
- 2.2.0 (2020-10)
22-
- Add direct_book_ok parameter for Android configuration.
23-
- Change test namespace from `Tests\Kerox\Fcm` to `Kerox\Fcm\Tests`
24-
- Update PHPStan to `0.12`
25-
26-
**Changelog** (since [`2.0.0`](https://github.com/ker0x/fcm/compare/2.0.0...2.1.0))
27-
28-
- 2.1.0 (2020-01)
29-
- Change class properties visibility from `protected` to `private`.
30-
- Add new configurations classes
31-
* `Kerox\Fcm\Model\Message\Notification\AndroidNotification\Color::class`
32-
* `Kerox\Fcm\Model\Message\Notification\AndroidNotification\LightSettings::class`
33-
* `Kerox\Fcm\Model\Message\Notification\ApnsNotification\Sound::class`
34-
- Add new options classes
35-
* `Kerox\Fcm\Model\Message\Options\AndroidOptions::class`
36-
* `Kerox\Fcm\Model\Message\Options\ApnsOptions::class`
37-
* `Kerox\Fcm\Model\Message\Options\WebpushOptions::class`
38-
- `Kerox\Fcm\Model\Message\Notification\AndroidNotification::class`: add new properties
39-
* `$channelId`
40-
* `$ticker`
41-
* `$sticky`
42-
* `$eventTime`
43-
* `$localOnly`
44-
* `$notificationPriority`
45-
* `$defaultSound`
46-
* `$defaultVibrateTimings`
47-
* `$defaultLightSettings`
48-
* `$vibrateTimings`
49-
* `$visibility`
50-
* `$lightSettings`
51-
* `$image`
52-
- `Kerox\Fcm\Model\Message\Notification\ApnsNotification\Alert::class`: add new properties
53-
* `$subTitle`
54-
* `$subTitleLocKey`
55-
* `$subTitleLocArgs`
56-
- `Kerox\Fcm\Model\Message\Android::class`: add new property `$options`.
57-
- `Kerox\Fcm\Model\Message\Apns::class`: add new property `$options`.
58-
- Method `Kerox\Fcm\Model\Message\Webpush::setOptions()`, type `array` is deprecated, use class `Kerox\Fcm\Model\Message\Options\WebpushOptions::class`
59-
instead.
60-
- Method `Kerox\Fcm\Model\Message\AbstractNotification\Alert::setActionLocKey()` is deprecated and will be removed in 3.0 with no replacement.
61-
62-
**Changelog** (since [`1.0.1`](https://github.com/ker0x/fcm/compare/1.0.1...2.0.0))
63-
64-
- 2.0.0 (2018-09)
65-
- Rewrite library to be compatible with [HTTP v1 API](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages)
66-
- Improve tests
67-
- Refactor code
68-
- Move documentation to the [Wiki](https://github.com/ker0x/fcm/wiki) section of the repo
101+
> [!NOTE]
102+
> Version `2.x` of this library is a full rewrite to be compliant with [HTTP v1 API](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages). If
103+
> you are on Legacy HTTP API, then you should consider using version `1.x`
104+
105+
### 2.4
106+
107+
#### What's Changed
108+
* Typo by @tin-cat in https://github.com/ker0x/fcm/pull/17
109+
* Add PHP 8.1 to CI by @ker0x in https://github.com/ker0x/fcm/pull/18
110+
* fix: setBadge var type by @demmmmios in https://github.com/ker0x/fcm/pull/19
111+
* Fix tests by @ker0x in https://github.com/ker0x/fcm/pull/20
112+
113+
**Full Changelog**: https://github.com/ker0x/fcm/compare/2.3.0...2.4.0

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ A PHP library to send push notification with [Firebase Cloud Messaging](https://
1515
> are provided within. If you already have one in your project, the package will **automatically discover it** and use it.
1616
> Otherwise You will need to require one separately.
1717
18+
> [!WARNING]
19+
> Version `3.2` introduce a BC break.
20+
> The signature of the `__construct()` method of the `Kerox\Fcm\Model\Message` class has changed, with the `$notification` parameter becoming the third argument and being optional.
21+
1822
## Installation
1923

2024
You can install Fcm using Composer:
@@ -56,3 +60,10 @@ $response = $fcm->send()->message($message);
5660
## Documentation
5761

5862
The documentation is available [here](https://github.com/ker0x/fcm/wiki)
63+
64+
## Testing
65+
66+
To live test the package, you must first generate an OAuth token.
67+
Go to https://developers.google.com/oauthplayground/ and select **Firebase Cloud Messaging API v1** from the list of APIs.
68+
Then select https://www.googleapis.com/auth/firebase.messaging and generate the OAuth token.
69+
Finally, define an environment variable named `FCM_OAUTH_TOKEN` and assign it the value of the access token.

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@
5454
}
5555
},
5656
"scripts": {
57-
"php-cs-fixer": "vendor/bin/php-cs-fixer fix --diff --verbose --ansi",
58-
"phpstan": "vendor/bin/phpstan",
59-
"phpunit": "vendor/bin/phpunit",
57+
"cs": "vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --ansi",
58+
"cs:fix": "vendor/bin/php-cs-fixer fix --diff --verbose --ansi",
59+
"lint": "vendor/bin/phpstan analyse --memory-limit 256M",
60+
"tests": "vendor/bin/phpunit",
6061
"rector": "vendor/bin/rector process src"
6162
}
6263
}

src/Model/Config/ApnsConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @param array<string, string> $headers
1919
*/
2020
public function __construct(
21-
ApnsNotification $notification = null,
21+
?ApnsNotification $notification = null,
2222
public array $headers = [],
2323
public ?ApnsFcmOptions $fcmOptions = null,
2424
) {
@@ -29,7 +29,7 @@ public function __construct(
2929
) {
3030
}
3131
}
32-
: null
32+
: null
3333
;
3434
}
3535
}

src/Model/Message.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
final class Message
2020
{
21-
public Notification $notification;
21+
public ?Notification $notification = null;
2222
public ?string $token = null;
2323
public ?string $topic = null;
2424
public ?string $condition = null;
@@ -27,18 +27,20 @@ final class Message
2727
* @param array<string, string> $data
2828
*/
2929
public function __construct(
30-
Notification|string $notification,
3130
Token|Topic|Condition $target,
3231
public array $data = [],
32+
Notification|string|null $notification = null,
3333
public ?AndroidConfig $android = null,
3434
public ?WebpushConfig $webpush = null,
3535
public ?ApnsConfig $apns = null,
3636
public ?FcmOptions $fcmOptions = null,
3737
) {
38-
$this->notification = \is_string($notification)
39-
? new Notification($notification)
40-
: $notification
41-
;
38+
if (null !== $notification) {
39+
$this->notification = \is_string($notification)
40+
? new Notification($notification)
41+
: $notification
42+
;
43+
}
4244

4345
match (true) {
4446
$target instanceof Token => $this->token = $target->__toString(),

tests/FcmTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Kerox\Fcm\Tests;
66

7-
use Kerox\Fcm\Api\Send;
87
use Kerox\Fcm\Fcm;
98
use Kerox\Fcm\Model\Message;
109
use Kerox\Fcm\Model\Target\Topic;
@@ -24,14 +23,28 @@ protected function tearDown(): void
2423
$this->fcm = null;
2524
}
2625

27-
public function testItCanGetAnInstanceOfSendApi(): void
26+
public function testItCanSendMessageWithNotification(): void
2827
{
2928
$sendApi = $this->fcm->send();
3029

31-
self::assertInstanceOf(Send::class, $sendApi);
32-
3330
$response = $sendApi->message(new Message(
31+
target: new Topic('TopicA'),
32+
data: [
33+
'story_id' => 'story_12345',
34+
],
3435
notification: 'Breaking News',
36+
));
37+
38+
self::assertSame(200, $response->getStatusCode());
39+
self::assertStringContainsString('projects/'.getenv('FCM_PROJECT_ID').'/messages/', $response->getBody()->getContents());
40+
self::assertArrayHasKey('content-type', $response->getHeaders());
41+
}
42+
43+
public function testItCanSendMessageWithDataOnly(): void
44+
{
45+
$sendApi = $this->fcm->send();
46+
47+
$response = $sendApi->message(new Message(
3548
target: new Topic('TopicA'),
3649
data: [
3750
'story_id' => 'story_12345',

tests/Fixtures/message.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"notification": {
3-
"title": "Breaking News",
4-
"body": "New news story available."
5-
},
62
"condition": "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)",
73
"data": {
84
"story_id": "story_12345"
95
},
6+
"notification": {
7+
"title": "Breaking News",
8+
"body": "New news story available."
9+
},
1010
"android": {
1111
"collapse_key": "collapse_key",
1212
"priority": "normal",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"topic": "TopicA",
3+
"data": {
4+
"story_id": "story_12345"
5+
}
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"topic": "TopicA",
3+
"data": {
4+
"story_id": "story_12345"
5+
},
6+
"notification": {
7+
"title": "Breaking News",
8+
"body": "New news story available."
9+
},
10+
"apns": {
11+
"headers": {
12+
"apns-priority": "5"
13+
},
14+
"fcm_options": {
15+
"analytics_label": "apns",
16+
"image": "https:\/\/example.com\/image.jpg"
17+
}
18+
},
19+
"fcm_options": {
20+
"analytics_label": "fcm"
21+
}
22+
}

0 commit comments

Comments
 (0)