diff --git a/composer.json b/composer.json index 4e51569..cee93a8 100644 --- a/composer.json +++ b/composer.json @@ -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" + } } \ No newline at end of file diff --git a/src/Services/Web/Message.php b/src/Services/Web/Message.php new file mode 100644 index 0000000..e9796da --- /dev/null +++ b/src/Services/Web/Message.php @@ -0,0 +1,104 @@ +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); + } + +} diff --git a/src/Services/Web/Web.php b/src/Services/Web/Web.php new file mode 100644 index 0000000..fcfde6c --- /dev/null +++ b/src/Services/Web/Web.php @@ -0,0 +1,52 @@ +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); + } + + +} \ No newline at end of file