Verified Commit 48e35e8d authored by Richard Weinhold's avatar Richard Weinhold 🔨

breaks backwards compability since send() changed!

- now requires PHP 7.0 at least!
- adds title support for notifications in send() method
parent d4955999
The MIT License (MIT)
Copyright (c) 2016 Richard Weinhold
Copyright (c) 2017 Richard Weinhold
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
......@@ -16,7 +16,7 @@ use ricwein\PushNotification\Handler\GCMHandler;
$push = new PushNotification(new GCMHandler());
$push->setServerToken('ExampleGooglePushToken12345678987654321');
$push->addDevice('device-token');
$push->send('message', ['payload' => 'data']);
$push->send('message', 'title', ['payload' => 'data']);
?>
```
......@@ -36,7 +36,7 @@ $push->setServer([
'url' => 'ssl://gateway.sandbox.push.apple.com:2195',
]);
$push->addDevice('<device-token>');
$push->send('message', ['payload' => 'data']);
$push->send('message', 'title', ['payload' => 'data']);
?>
```
......@@ -59,7 +59,7 @@ $push->addDevice([
'clientID' => 'clientSecret',
'OAuth2-Token',
]);
$push->send('message', ['payload' => 'data']);
$push->send('message', 'title', ['payload' => 'data']);
?>
```
......@@ -193,13 +193,23 @@ $push->send('message');
?>
```
Adding a title?
```php
<?php
$push->send('message', 'title');
?>
```
It's possible to add a payload as an array:
```php
<?php
$payload = ['data'];
$push->send('message', $payload);
$push->send('message', null, $payload);
?>
```
......@@ -218,4 +228,4 @@ if (!$push->send('message')) {
### Exceptions ###
This class can throw some default \Exception() - mostly in case of incorrect configuration or unreachable servers.
This class can throw some default \Exception or \UnexpectedValueException - mostly in case of incorrect configuration or unreachable servers.
{
"name": "ricwein/push-notifications",
"description": "Push Notifications for iOS (APNS), Android (GCM) and Windows (WNS)",
"description": "Push Notifications for iOS (APNS), Android (GCM/FCM) and Windows (WNS)",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Richard Weinhold",
"email": "git@ricwein.com"
}
],
"authors": [{
"name": "Richard Weinhold",
"email": "git@ricwein.com"
}],
"require": {
"php": ">= 5.6.0"
"php": ">= 7.0.0"
}
}
......@@ -22,14 +22,17 @@ class APNSHandler extends PushHandler {
/**
* build binary notification-package
* @param string $deviceToken
* @param string $payload json
* @param array $arbitrary additional settings
* @param int $version push-version (1/2)
*
* @param string $deviceToken
* @param string $payload json
* @param array $arbitrary additional settings
* @param int $version push-version (1/2)
*
* @throws \UnexpectedValueException
*
* @return string
*/
protected function _buildNotification($deviceToken, $payload, array $arbitrary = [], $version = 1) {
protected function _buildNotification(string $deviceToken, string $payload, array $arbitrary = [], int $version = 1): string {
// set default arbitrary settings
$arbitrary = array_merge([
......@@ -71,17 +74,22 @@ class APNSHandler extends PushHandler {
/**
* send notification to Apples APNS servers
* @param string $message
* @param array $payload
* @param array $devices
*
* @param string $message
* @param string|null $title
* @param array $payload
* @param array $devices
*
* @throws \UnexpectedValueException|\RuntimeException
*
* @return bool
*/
public function send($message, array $payload, array $devices) {
public function send(string $message, string $title = null, array $payload, array $devices): bool {
$message = trim(stripslashes($message));
// build payload
$payload = array_replace_recursive(['aps' => [
'alert' => trim(stripslashes($message)),
'alert' => $title !== null ? ['title' => $title, 'body' => $message] : $message,
'badge' => 1,
'sound' => 'default',
]], $payload);
......@@ -91,12 +99,15 @@ class APNSHandler extends PushHandler {
/**
* build and send Notification from raw payload
* @param array $payload
* @param array $devices
*
* @param array $payload
* @param array $devices
*
* @throws \UnexpectedValueException|\RuntimeException
*
* @return bool
*/
public function sendRaw(array $payload, array $devices) {
public function sendRaw(array $payload, array $devices): bool {
// set default values
$result = true;
......
......@@ -21,19 +21,24 @@ class FCMHandler extends PushHandler {
/**
* send notification to Googles FCM servers
* @param string $message
* @param array $payload
* @param array $devices
*
* @param string $message
* @param string|null $title
* @param array $payload
* @param array $devices
*
* @throws \UnexpectedValueException|\RuntimeException
*
* @return bool
*/
public function send($message, array $payload, array $devices) {
public function send(string $message, string $title = null, array $payload, array $devices): bool {
$message = trim(stripslashes($message));
// build payload
$payload = [
'priority' => 'high',
'notification' => [
'title' => (strlen($message) > 64 ? substr($message, 0, 61) . '...' : $message),
'title' => $title ?? (strlen($message) > 64 ? substr($message, 0, 61) . '...' : $message),
'body' => $message,
],
'data' => array_merge([
......@@ -46,12 +51,15 @@ class FCMHandler extends PushHandler {
/**
* build and send Notification from raw payload
* @param array $payload
* @param array $devices
*
* @param array $payload
* @param array $devices
*
* @throws \RuntimeException
*
* @return bool
*/
public function sendRaw(array $payload, array $devices) {
public function sendRaw(array $payload, array $devices): bool {
if (count($devices) <= 1) {
$payload = array_merge([
'to' => current($devices),
......
......@@ -21,18 +21,23 @@ class GCMHandler extends PushHandler {
/**
* send notification to Googles GCM servers
* @param string $message
* @param array $payload
* @param array $devices
*
* @param string $message
* @param string|null $title
* @param array $payload
* @param array $devices
*
* @throws \UnexpectedValueException|\RuntimeException
*
* @return bool
*/
public function send($message, array $payload, array $devices) {
public function send(string $message, string $title = null, array $payload, array $devices): bool {
$message = trim(stripslashes($message));
// build payload
$payload = [
'notification' => [
'title' => (strlen($message) > 64 ? substr($message, 0, 61) . '...' : $message),
'title' => $title ?? (strlen($message) > 64 ? substr($message, 0, 61) . '...' : $message),
'body' => $message,
],
'data' => array_merge([
......@@ -45,12 +50,15 @@ class GCMHandler extends PushHandler {
/**
* build and send Notification from raw payload
* @param array $payload
* @param array $devices
*
* @param array $payload
* @param array $devices
*
* @throws \RuntimeException
*
* @return bool
*/
public function sendRaw(array $payload, array $devices) {
public function sendRaw(array $payload, array $devices): bool {
if (count($devices) <= 1) {
$payload = array_merge([
'to' => current($devices),
......
......@@ -21,12 +21,14 @@ class WNSHandler extends PushHandler {
];
/**
* @param int $clientID
* @param string $clientSecret
* @param int $clientID
* @param string $clientSecret
*
* @throws \Exception
*
* @return string
*/
public function requestOAuthToken($clientID, $clientSecret) {
public function requestOAuthToken(int $clientID, string $clientSecret): string {
// init http-headers
$headers = [
'Content-Type: application/x-www-form-urlencoded',
......@@ -80,11 +82,12 @@ class WNSHandler extends PushHandler {
}
/**
* @param string $message
* @param array $data
* @param string $message
* @param array $data
*
* @return string
*/
protected static function _buildPayloadXML($message, array $data = []) {
protected static function _buildPayloadXML(string $message, array $data = []): string {
$message = trim(stripslashes($message));
if (isset($data['title']) && !isset($data['image'])) {
......@@ -131,14 +134,20 @@ class WNSHandler extends PushHandler {
/**
* send notification to Microsofts live.com WNS servers
* @param string $message
* @param array $payload
* @param array $devices
* @throws \RuntimeException
*
* @param string $message
* @param string|null $title
* @param array $payload
* @param array $devices
*
* @throws \UnexpectedValueException|\RuntimeException
*
* @return bool
*/
public function send($message, array $payload, array $devices) {
$result = true;
public function send(string $message, string $title = null, array $payload, array $devices): bool {
if ($title !== null) {
$payload['title'] = $title;
}
// buil xml-payload
$payload['xml'] = static::_buildPayloadXML($message, $payload);
......@@ -153,8 +162,7 @@ class WNSHandler extends PushHandler {
* @throws \RuntimeException|\UnexpectedValueException
* @return bool
*/
public function sendRaw(array $payload, array $devices) {
$result = true;
public function sendRaw(array $payload, array $devices): bool {
// buil xml-payload
if (isset($payload['xml'])) {
......@@ -184,6 +192,8 @@ class WNSHandler extends PushHandler {
// append payload
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
$result = true;
foreach ($devices as $clientID => $clientSecret) {
// request device-token if necessary
......
......@@ -18,10 +18,10 @@ abstract class PushHandler {
];
/**
* @param string $serverToken
* @param string $url
* @param string|null $serverToken
* @param string|null $url
*/
public function __construct($serverToken = null, $url = null) {
public function __construct(string $serverToken = null, string $url = null) {
if ($serverToken !== null) {
$this->setServerToken($serverToken);
}
......@@ -33,24 +33,30 @@ abstract class PushHandler {
/**
* @param string $url
*
* @return self
*/
public function setServerUrl($url) {
public function setServerUrl(string $url): self {
$this->_server['url'] = $url;
return $this;
}
/**
* @param string $serverToken
*
* @return self
*/
public function setServerToken($serverToken) {
public function setServerToken(string $serverToken): self {
$this->_server['token'] = $serverToken;
return $this;
}
/**
* @param array $server
*
* @return self
*/
public function setServer(array $server) {
public function setServer(array $server): self {
$this->_server = array_merge($this->_server, $server);
return $this;
}
......@@ -60,7 +66,7 @@ abstract class PushHandler {
* @throws \UnexpectedValueException
* @return bool
*/
public function prepare() {
public function prepare(): bool {
if (empty($this->_server['token'])) {
throw new \UnexpectedValueException('server token not set', 500);
} elseif (empty($this->_server['url'])) {
......@@ -72,19 +78,25 @@ abstract class PushHandler {
/**
* build default PushNotification and send via PushHandler to servers
* @param string $message
* @param array $payload
* @param array $devices
* @param string $message
* @param string|null $title
* @param array $payload
* @param array $devices
*
* @throws \UnexpectedValueException|\RuntimeException
*
* @return bool
*
*/
abstract public function send($message, array $payload, array $devices);
abstract public function send(string $message, string $title = null, array $payload, array $devices);
/**
* build and send Notification from raw payload
* @param array $payload
* @param array $devices
* @param array $payload
* @param array $devices
*
* @throws \UnexpectedValueException|\RuntimeException
*
* @return bool
*/
abstract public function sendRaw(array $payload, array $devices);
......
......@@ -29,33 +29,39 @@ class PushNotification {
}
/**
* @param PushHandler $handler
* @return PushNotification
* @param PushHandler $handler
*
* @return self
*/
public function setHandler(PushHandler $handler) {
public function setHandler(PushHandler $handler): self {
$this->_handler = $handler;
return $this;
}
/**
* build payload and send via PushHandler to servers
* @param string $message
* @param array $payload
*
* @param string $message
* @param string|null $title
* @param array $payload
*
* @return bool
*/
public function send($message, array $payload = []) {
public function send(string $message, string $title = null, array $payload = []): bool {
if (!$this->_prepare()) {
return false;
}
return $this->_handler->send($message, $payload, $this->_devices);
return $this->_handler->send($message, $title, $payload, $this->_devices);
}
/**
* send raw payload via PushHandler to servers
* @param array $payload
*
* @param array $payload
*
* @return bool
*/
public function sendRaw(array $payload = []) {
public function sendRaw(array $payload = []): bool {
if (!$this->_prepare()) {
return false;
}
......@@ -64,9 +70,10 @@ class PushNotification {
/**
* prepare PushHandler for sending
*
* @return bool
*/
protected function _prepare() {
protected function _prepare(): bool {
if (count($this->_devices) === 0) {
return false;
} elseif (!$this->_handler->prepare()) {
......@@ -78,20 +85,25 @@ class PushNotification {
/**
* @param mixed $device
*
* @return self
*/
public function addDevice($device) {
public function addDevice($device): self {
$this->_devices = array_merge($this->_devices, (array) $device);
return $this;
}
/**
* wrap handler-methods and return $this
* @param string $name
* @param mixed $arguments
*
* @param string $name
* @param mixed $arguments
*
* @throws \Exception
* @return $this
*
* @return self
*/
public function __call($name, $arguments) {
public function __call(string $name, $arguments): self {
if (method_exists($this->_handler, $name)) {
call_user_func_array([$this->_handler, $name], $arguments);
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment