Skip to content
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

[Sprint/Higgs] (feat): added support for web notifications #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"name": "Minds/Surge",
"description": "A php library for sending push notifications",
"autoload": {
"psr-4": { "Surge\\": "src/" }
"name": "Minds/Surge",
"description": "A php library for sending push notifications",
"autoload": {
"psr-4": {
"Surge\\": "src/"
}
},
"require": {
"minishlink/web-push": "~4.0.2"
}
}
104 changes: 104 additions & 0 deletions src/Services/Web/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* WebPush Message
*/

namespace Surge\Services\Web;

use Surge\Components\Interfaces;

class Message implements Interfaces\MessageInterface
{

public $bigPicture;
public $badge;
public $group;
public $largeIcon;
public $title;
public $message;
public $token;
public $service = "Web";
public $uri;
public $json = "";

public function setBadge($badge)
{
$this->badge = $badge;
return $this;
}

public function setGroup($group)
{
$this->group = $group;
return $this;
}

public function setBigPicture($bigPicture)
{
$this->bigPicture = $bigPicture;
return $this;
}

public function setLargeIcon($icon)
{
$this->largeIcon = $icon;
return $this;
}

public function getToken()
{
return json_decode(base64_decode($this->token), true);
}

public function setToken($token)
{
$this->token = $token;
return $this;
}

public function setTitle($title)
{
$this->title = $title;
return $this;
}

public function setMessage($message)
{
$this->message = $message;
return $this;
}

public function setURI($uri)
{
$this->uri = $uri;
return $this;
}

public function setSound($sound)
{
return $this;
}

public function setJsonObject($json)
{
$this->json = $json;
return $this;
}

public function toJSON()
{
$payload = [
'notification' => [
'body' => $this->message,
'title' => $this->title ?: ' ',
'uri' => $this->uri,
'json' => $this->json,
'largeIcon' => $this->largeIcon,
'icon' => $this->bigPicture,
'badge' => intval($this->badge)
]
];
return json_encode($payload);
}

}
52 changes: 52 additions & 0 deletions src/Services/Web/Web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php


namespace Surge\Services\Web;

use Surge\Components\Interfaces;
use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;


class Web implements Interfaces\ServiceInterface
{

private $options;

public function __construct($options = array())
{
if (isset($options)) {
$this->options = $options;
}

}

public function request($message)
{
$webPush = new WebPush($this->options);

$token = $message->getToken()['token'];
$token['publicKey'] = $token['keys']['p256dh'];
$token['authToken'] = $token['keys']['auth'];

$result = $webPush->sendNotification(
new Subscription($token['endpoint'], $token['keys']['p256dh'], $token['keys']['auth']),
$message->toJSON(),true, [], $this->options);

//todo check for expiration when $result == false

return $result;
}

/**
* Compile the message and send to the APNS service
* @param Interfaces\MessageInterface $message
* @return void
*/
public function sendMessage($message)
{
$this->request($message);
}


}