Skip to content

Commit

Permalink
🐛 Allow to send message with data only
Browse files Browse the repository at this point in the history
fix #26
  • Loading branch information
ker0x committed Feb 15, 2024
1 parent 54495ea commit 1f792c1
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 88 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

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

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

- name: phpstan
run: vendor/bin/phpstan analyse --memory-limit 256M
run: composer lint

- name: phpunit
env:
FCM_OAUTH_TOKEN: ${{ secrets.FCM_OAUTH_TOKEN }}
FCM_PROJECT_ID: ${{ secrets.FCM_PROJECT_ID }}
run: vendor/bin/phpunit
run: composer tests

- name: coverage
uses: codecov/codecov-action@v3
Expand Down
169 changes: 107 additions & 62 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,112 @@

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

## 3.x

> [!NOTE]
> Version `3.x` of this library is a full rewrite using [PSR-18 HTTP Client](https://www.php-fig.org/psr/psr-18/) interface,
> which means that **no** HTTP Client, like [Guzzle](https://github.com/guzzle/guzzle) or [httplug](https://github.com/php-http/httplug),
> are provided within. If you already have one in your project, the package will **automatically discover it** and use it.
> Otherwise You will need to require one separately.
### 3.2

> [!WARNING]
> Version `3.2` introduce a BC break.
> 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.
```diff
final class Message
{
- public Notification $notification;
+ public ?Notification $notification = null;
public ?string $token = null;
public ?string $topic = null;
public ?string $condition = null;

/**
* @param array<string, string> $data
*/
public function __construct(
- Notification|string $notification,
Token|Topic|Condition $target,
public array $data = [],
+ Notification|string|null $notification = null,
public ?AndroidConfig $android = null,
public ?WebpushConfig $webpush = null,
public ?ApnsConfig $apns = null,
public ?FcmOptions $fcmOptions = null,
) {
+ if (null !== $notification) {
$this->notification = \is_string($notification)
? new Notification($notification)
: $notification
;
+ }

match (true) {
$target instanceof Token => $this->token = $target->__toString(),
$target instanceof Topic => $this->topic = $target->__toString(),
$target instanceof Condition => $this->condition = $target->__toString(),
};
}
}
```
#### Before

````php
$message = new Message(
notification: 'Breaking News',
target: new Topic('TopicA'),
data: [
'story_id' => 'story_12345',
],
);
````

#### After

````php
$message = new Message(
target: new Topic('TopicA'),
data: [
'story_id' => 'story_12345',
],
notification: 'Breaking News',
);
````

### 3.1

#### What's Changed
* :bug: Fix README by @ker0x in https://github.com/ker0x/fcm/pull/23 and https://github.com/ker0x/fcm/pull/25
* :bug: Fix .gitattributes by @ker0x in https://github.com/ker0x/fcm/pull/24
* :arrow_up: Bump Symfony components to `6.4`, allow Symfony 7 by @ker0x in https://github.com/ker0x/fcm/pull/25
* :green_heart: Update CI workflow by @ker0x in https://github.com/ker0x/fcm/pull/25
* :rotating_light: Fix PHP-CS-Fixer and PHPStan warning by @ker0x in https://github.com/ker0x/fcm/pull/25

**Full Changelog**: https://github.com/ker0x/fcm/compare/3.0.0...3.1.0

### 3.0

#### What's Changed
* :art: Full package refactoring by @ker0x in https://github.com/ker0x/fcm/pull/21
* :memo: Update README by @ker0x in https://github.com/ker0x/fcm/pull/22

**Full Changelog**: https://github.com/ker0x/fcm/compare/2.4.0...3.0.0

## 2.x

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
you are on Legacy HTTP API, then you should consider using version `1.x`

**Changelog** (since [`2.2.0`](https://github.com/ker0x/fcm/compare/2.2.0...2.3.0))

- 2.3.0 (2021-02)
- Bump minimal PHP version to `7.3`
- Bump Guzzle version to `7.2`
- Bump PHPUnit version to `8.`
- Update `.gitattributes`
- Update CI workflow

**Changelog** (since [`2.1.0`](https://github.com/ker0x/fcm/compare/2.1.0...2.2.0))

- 2.2.0 (2020-10)
- Add direct_book_ok parameter for Android configuration.
- Change test namespace from `Tests\Kerox\Fcm` to `Kerox\Fcm\Tests`
- Update PHPStan to `0.12`

**Changelog** (since [`2.0.0`](https://github.com/ker0x/fcm/compare/2.0.0...2.1.0))

- 2.1.0 (2020-01)
- Change class properties visibility from `protected` to `private`.
- Add new configurations classes
* `Kerox\Fcm\Model\Message\Notification\AndroidNotification\Color::class`
* `Kerox\Fcm\Model\Message\Notification\AndroidNotification\LightSettings::class`
* `Kerox\Fcm\Model\Message\Notification\ApnsNotification\Sound::class`
- Add new options classes
* `Kerox\Fcm\Model\Message\Options\AndroidOptions::class`
* `Kerox\Fcm\Model\Message\Options\ApnsOptions::class`
* `Kerox\Fcm\Model\Message\Options\WebpushOptions::class`
- `Kerox\Fcm\Model\Message\Notification\AndroidNotification::class`: add new properties
* `$channelId`
* `$ticker`
* `$sticky`
* `$eventTime`
* `$localOnly`
* `$notificationPriority`
* `$defaultSound`
* `$defaultVibrateTimings`
* `$defaultLightSettings`
* `$vibrateTimings`
* `$visibility`
* `$lightSettings`
* `$image`
- `Kerox\Fcm\Model\Message\Notification\ApnsNotification\Alert::class`: add new properties
* `$subTitle`
* `$subTitleLocKey`
* `$subTitleLocArgs`
- `Kerox\Fcm\Model\Message\Android::class`: add new property `$options`.
- `Kerox\Fcm\Model\Message\Apns::class`: add new property `$options`.
- Method `Kerox\Fcm\Model\Message\Webpush::setOptions()`, type `array` is deprecated, use class `Kerox\Fcm\Model\Message\Options\WebpushOptions::class`
instead.
- Method `Kerox\Fcm\Model\Message\AbstractNotification\Alert::setActionLocKey()` is deprecated and will be removed in 3.0 with no replacement.

**Changelog** (since [`1.0.1`](https://github.com/ker0x/fcm/compare/1.0.1...2.0.0))

- 2.0.0 (2018-09)
- Rewrite library to be compatible with [HTTP v1 API](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages)
- Improve tests
- Refactor code
- Move documentation to the [Wiki](https://github.com/ker0x/fcm/wiki) section of the repo
> [!NOTE]
> 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
> you are on Legacy HTTP API, then you should consider using version `1.x`
### 2.4

#### What's Changed
* Typo by @tin-cat in https://github.com/ker0x/fcm/pull/17
* Add PHP 8.1 to CI by @ker0x in https://github.com/ker0x/fcm/pull/18
* fix: setBadge var type by @demmmmios in https://github.com/ker0x/fcm/pull/19
* Fix tests by @ker0x in https://github.com/ker0x/fcm/pull/20

**Full Changelog**: https://github.com/ker0x/fcm/compare/2.3.0...2.4.0
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ A PHP library to send push notification with [Firebase Cloud Messaging](https://
> are provided within. If you already have one in your project, the package will **automatically discover it** and use it.
> Otherwise You will need to require one separately.
> [!WARNING]
> Version `3.2` introduce a BC break.
> 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.
## Installation

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

The documentation is available [here](https://github.com/ker0x/fcm/wiki)

## Testing

https://developers.google.com/oauthplayground/

Firebase Cloud Messaging API v1
https://www.googleapis.com/auth/firebase.messaging
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@
}
},
"scripts": {
"php-cs-fixer": "vendor/bin/php-cs-fixer fix --diff --verbose --ansi",
"phpstan": "vendor/bin/phpstan",
"phpunit": "vendor/bin/phpunit",
"cs": "vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --ansi",
"cs:fix": "vendor/bin/php-cs-fixer fix --diff --verbose --ansi",
"lint": "vendor/bin/phpstan analyse --memory-limit 256M",
"tests": "vendor/bin/phpunit",
"rector": "vendor/bin/rector process src"
}
}
4 changes: 2 additions & 2 deletions src/Model/Config/ApnsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @param array<string, string> $headers
*/
public function __construct(
ApnsNotification $notification = null,
?ApnsNotification $notification = null,
public array $headers = [],
public ?ApnsFcmOptions $fcmOptions = null,
) {
Expand All @@ -29,7 +29,7 @@ public function __construct(
) {
}
}
: null
: null

Check warning on line 32 in src/Model/Config/ApnsConfig.php

View check run for this annotation

Codecov / codecov/patch

src/Model/Config/ApnsConfig.php#L32

Added line #L32 was not covered by tests
;
}
}
14 changes: 8 additions & 6 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
final class Message
{
public Notification $notification;
public ?Notification $notification = null;
public ?string $token = null;
public ?string $topic = null;
public ?string $condition = null;
Expand All @@ -27,18 +27,20 @@ final class Message
* @param array<string, string> $data
*/
public function __construct(
Notification|string $notification,
Token|Topic|Condition $target,
public array $data = [],
Notification|string|null $notification = null,
public ?AndroidConfig $android = null,
public ?WebpushConfig $webpush = null,
public ?ApnsConfig $apns = null,
public ?FcmOptions $fcmOptions = null,
) {
$this->notification = \is_string($notification)
? new Notification($notification)
: $notification
;
if (null !== $notification) {
$this->notification = \is_string($notification)
? new Notification($notification)
: $notification
;
}

match (true) {
$target instanceof Token => $this->token = $target->__toString(),
Expand Down
21 changes: 17 additions & 4 deletions tests/FcmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Kerox\Fcm\Tests;

use Kerox\Fcm\Api\Send;
use Kerox\Fcm\Fcm;
use Kerox\Fcm\Model\Message;
use Kerox\Fcm\Model\Target\Topic;
Expand All @@ -24,14 +23,28 @@ protected function tearDown(): void
$this->fcm = null;
}

public function testItCanGetAnInstanceOfSendApi(): void
public function testItCanSendMessageWithNotification(): void
{
$sendApi = $this->fcm->send();

self::assertInstanceOf(Send::class, $sendApi);

$response = $sendApi->message(new Message(
target: new Topic('TopicA'),
data: [
'story_id' => 'story_12345',
],
notification: 'Breaking News',
));

self::assertSame(200, $response->getStatusCode());
self::assertStringContainsString('projects/'.getenv('FCM_PROJECT_ID').'/messages/', $response->getBody()->getContents());
self::assertArrayHasKey('content-type', $response->getHeaders());
}

public function testItCanSendMessageWithDataOnly(): void
{
$sendApi = $this->fcm->send();

$response = $sendApi->message(new Message(
target: new Topic('TopicA'),
data: [
'story_id' => 'story_12345',
Expand Down
6 changes: 6 additions & 0 deletions tests/Fixtures/message_with_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"topic": "TopicA",
"data": {
"story_id": "story_12345"
}
}
Loading

0 comments on commit 1f792c1

Please sign in to comment.