Skip to content

Commit

Permalink
Restore Notification resource
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightwatson committed Oct 13, 2023
1 parent a1b2947 commit 3040d9d
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 6 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ it via the `FcmChannel::class`. Here is an example:
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;

class AccountActivated extends Notification
{
Expand All @@ -61,13 +62,14 @@ class AccountActivated extends Notification
public function toFcm($notifiable)
{
return FcmMessage::create()
->notification(
FcmNotification::create()
->title('Account Activated')
->body('Your account has been activated.')
->image('http://example.com/url-to-image-here.png')
)
->setData(['data1' => 'value', 'data2' => 'value2'])
->custom([
'notification' => [
'title' => 'Account Activated',
'body' => 'Your account has been activated',
'image' => 'http://example.com/url-to-image-here.png',
],
'android' => [
'notification' => [
'color' => '#0A0A0A',
Expand Down
19 changes: 18 additions & 1 deletion src/FcmMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NotificationChannels\Fcm;

use NotificationChannels\Fcm\Resources\Notification;
use Illuminate\Support\Traits\Macroable;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Messaging\Message;
Expand Down Expand Up @@ -40,10 +41,15 @@ class FcmMessage implements Message
*/
public array $custom = [];

/**
* The message notification.
*/
public ?Notification $notification = null;

/**
* The custom messaging client.
*/
public ?Messaging $client;
public ?Messaging $client = null;

/**
* Create a new message instance.
Expand Down Expand Up @@ -113,6 +119,16 @@ public function custom(?array $custom): self
return $this;
}

/**
* Set the message notification.
*/
public function notification(Notification $notification): self
{
$this->notification = $notification;

return $this;
}

/**
* Set the message Firebase Messaging client instance.
*/
Expand All @@ -131,6 +147,7 @@ public function toArray()
'token' => $this->token,
'topic' => $this->topic,
'condition' => $this->condition,
'notification' => $this->notification?->toArray(),
...$this->custom,
]);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Resources/FcmResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace NotificationChannels\Fcm\Resources;

abstract class FcmResource
{
/**
* @return static
*/
public static function create(): static
{
return new static;
}

/**
* Map the resource to an array.
*/
abstract public function toArray(): array;
}
63 changes: 63 additions & 0 deletions src/Resources/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace NotificationChannels\Fcm\Resources;

class Notification extends FcmResource
{
/**
* The notification title.
*/
public ?string $title = null;

/**
* The notification body.
*/
public ?string $body = null;

/**
* The notification image.
*/
public ?string $image = null;

/**
* Set the notification title.
*/
public function title(?string $title): self
{
$this->title = $title;

return $this;
}

/**
* Set the notification body.
*/
public function body(?string $body): self
{
$this->body = $body;

return $this;
}

/**
* Set the notification image.
*/
public function image(?string $image): self
{
$this->image = $image;

return $this;
}

/**
* Map the resource to an array.
*/
public function toArray(): array
{
return array_filter([
'title' => $this->title,
'body' => $this->body,
'image' => $this->image,
]);
}
}
12 changes: 12 additions & 0 deletions tests/FcmMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Kreait\Firebase\Contract\Messaging;
use Mockery;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\Notification;
use PHPUnit\Framework\TestCase;

class FcmMessageTest extends TestCase
Expand Down Expand Up @@ -71,6 +72,17 @@ public function test_it_can_set_custom_attributes()
$this->assertEquals($expected, $message->toArray());
}

public function test_it_can_set_notification()
{
$notification = Notification::create()->title('title');

$message = FcmMessage::create()->notification($notification);

$this->assertEquals([
'notification' => ['title' => 'title'],
], $message->toArray());
}

public function test_it_can_set_client()
{
$client = Mockery::mock(Messaging::class);
Expand Down
37 changes: 37 additions & 0 deletions tests/Resources/NotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace NotificationChannels\Fcm\Test\Resources;

use NotificationChannels\Fcm\Resources\Notification;
use PHPUnit\Framework\TestCase;

class NotificationTest extends TestCase
{
public function test_it_can_be_instantiated()
{
$message = Notification::create();

$this->assertInstanceOf(Notification::class, $message);
}

public function test_it_can_set_title()
{
$message = Notification::create()->title('title');

$this->assertEquals(['title' => 'title'], $message->toArray());
}

public function test_it_can_set_body()
{
$message = Notification::create()->body('body');

$this->assertEquals(['body' => 'body'], $message->toArray());
}

public function test_it_can_set_image()
{
$message = Notification::create()->image('image');

$this->assertEquals(['image' => 'image'], $message->toArray());
}
}

0 comments on commit 3040d9d

Please sign in to comment.