-
Notifications
You must be signed in to change notification settings - Fork 124
feat: add Declarative Web Push message support #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wapacro
wants to merge
9
commits into
laravel-notification-channels:master
Choose a base branch
from
wapacro:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4569b9c
refactor: implement message interface
wapacro cd7147f
feat: add Declarative Web Push message support
wapacro 38e6fb4
test: add tests for Declarative Web Push messages
wapacro 9670eb2
docs: update readme
wapacro 8db2bb6
fix: styling
wapacro 3933146
docs: update readme
wapacro b035026
fix: styling
wapacro 83d54c0
fix: styling
joostdebruijn bdd71e7
chore: require minishlink/web-push v9.0.3 as minimum
joostdebruijn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,305 @@ | ||
| <?php | ||
|
|
||
| namespace NotificationChannels\WebPush; | ||
|
|
||
| use Illuminate\Support\Arr; | ||
| use NotificationChannels\WebPush\Exceptions\MessageValidationFailed; | ||
|
|
||
| /** | ||
| * @implements \Illuminate\Contracts\Support\Arrayable<string, mixed> | ||
| * | ||
| * @link https://www.w3.org/TR/push-api/#members | ||
| */ | ||
| class DeclarativeWebPushMessage implements WebPushMessageInterface | ||
| { | ||
| protected string $title; | ||
|
|
||
| /** | ||
| * @var array<array-key, array{'title': string, 'action': string, 'navigate': string, 'icon'?: string}> | ||
| */ | ||
| protected array $actions = []; | ||
|
|
||
| protected string $badge; | ||
|
|
||
| protected string $body; | ||
|
|
||
| protected string $dir; | ||
|
|
||
| protected string $icon; | ||
|
|
||
| protected string $image; | ||
|
|
||
| protected string $lang; | ||
|
|
||
| protected bool $mutable; | ||
|
|
||
| protected string $navigate; | ||
|
|
||
| protected bool $renotify; | ||
|
|
||
| protected bool $requireInteraction; | ||
|
|
||
| protected bool $silent; | ||
|
|
||
| protected string $tag; | ||
|
|
||
| protected int $timestamp; | ||
|
|
||
| /** | ||
| * @var array<int> | ||
| */ | ||
| protected array $vibrate; | ||
|
|
||
| protected mixed $data; | ||
|
|
||
| /** | ||
| * @var array<string, mixed> | ||
| */ | ||
| protected array $options = [ | ||
| 'contentType' => 'application/json', | ||
| ]; | ||
|
|
||
| /** | ||
| * Set the notification title. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function title(string $value): static | ||
| { | ||
| $this->title = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Add a notification action. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function action(string $title, string $action, string $navigate, ?string $icon = null): static | ||
| { | ||
| $this->actions[] = array_filter(['title' => $title, 'action' => $action, 'navigate' => $navigate, 'icon' => $icon]); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification badge. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function badge(string $value): static | ||
| { | ||
| $this->badge = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification body. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function body(string $value): static | ||
| { | ||
| $this->body = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification direction. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function dir(string $value): static | ||
| { | ||
| $this->dir = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification icon url. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function icon(string $value): static | ||
| { | ||
| $this->icon = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification image url. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function image(string $value): static | ||
| { | ||
| $this->image = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification language. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function lang(string $value): static | ||
| { | ||
| $this->lang = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function mutable(bool $value = true): static | ||
| { | ||
| $this->mutable = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the navigation target upon activation. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function navigate(string $value): static | ||
| { | ||
| $this->navigate = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function renotify(bool $value = true): static | ||
| { | ||
| $this->renotify = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function requireInteraction(bool $value = true): static | ||
| { | ||
| $this->requireInteraction = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function silent(bool $value = true): static | ||
| { | ||
| $this->silent = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification tag. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function tag(string $value): static | ||
| { | ||
| $this->tag = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the timestamp associated with the notification. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function timestamp(int $value): static | ||
| { | ||
| $this->timestamp = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification vibration pattern. | ||
| * | ||
| * @param array<int> $value | ||
| * @return $this | ||
| */ | ||
| public function vibrate(array $value): static | ||
| { | ||
| $this->vibrate = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification arbitrary data. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function data(mixed $value): static | ||
| { | ||
| $this->data = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the notification options. | ||
| * | ||
| * @link https://github.com/web-push-libs/web-push-php#notifications-and-default-options | ||
| * | ||
| * @param array<string, mixed> $value | ||
| * @return $this | ||
| */ | ||
| public function options(array $value): static | ||
| { | ||
| $this->options = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Get the notification options. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function getOptions(): array | ||
| { | ||
| return $this->options; | ||
| } | ||
|
|
||
| /** | ||
| * Get an array representation of the message. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| if (empty($this->title)) { | ||
| throw MessageValidationFailed::titleRequired(); | ||
| } | ||
|
|
||
| if (empty($this->navigate)) { | ||
| throw MessageValidationFailed::navigateRequired(); | ||
| } | ||
|
|
||
| return Arr::whereNotNull([ | ||
| 'web_push' => 8030, | ||
| 'notification' => Arr::except(array_filter(get_object_vars($this)), ['mutable', 'options']), | ||
| 'mutable' => $this->mutable ?? null, | ||
| ]); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the documentation I saw
application/notification+json, but not sure if this is now a requirement?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this comment, the absence of the content type header in the parser spec draft and that there's been no activity on the RFC 8030 side of things for years I assume the specific header will no longer be taken into account. This can obviously change in the future, but for now
application/jsonis a safe bet that can easily be changed if needed imho. I also tested this with Safari, and it works flawlessly with the regular JSON content type.