Commit a4ccc979 authored by Richard Weinhold's avatar Richard Weinhold 🔨

some code cleanup and refactoring

- fixed typo in WNSHandler
- improved payload handling
- default payload is now array() instead of NULL
- some small changes in the readme.md
parent 19e9f02f
......@@ -25,35 +25,30 @@ class APNSHandler extends PushHandler {
/**
* send notification to Apples APNS servers
* @param string $message
* @param array $payload (optional)
* @param array $devices
* @param array $data (optional)
* @return bool
*/
public function send($message, array $devices, array $data = null) {
public function send($message, array $payload = [], array $devices) {
$result = true;
$arbitrary = ['command' => 1];
// init payload
$payload = ['aps' => [
'alert' => stripslashes($message),
'badge' => 1,
'sound' => 'default',
]];
// handle arbitrary settings
foreach (['expire', 'messageID', 'priority', 'command'] as $key) {
if (isset($data[$key])) {
$arbitrary[$key] = (int) abs($data[$key]);
unset($data[$key]);
if (isset($payload[$key])) {
$arbitrary[$key] = (int) abs($payload[$key]);
unset($payload[$key]);
} elseif (isset($this->_server[$key])) {
$arbitrary[$key] = (int) abs($this->_server[$key]);
}
}
// apply additional data to payload
if (is_array($data)) {
$payload['aps'] = array_merge($payload['aps'], $data);
}
// build payload
$payload = array_merge(['aps' => [
'alert' => trim(stripslashes($message)),
'badge' => 1,
'sound' => 'default',
]], $payload);
// open context
$ctx = stream_context_create();
......
......@@ -24,23 +24,20 @@ class GCMHandler extends PushHandler {
/**
* send notification to Googles GCM servers
* @param string $message
* @param array $payload (optional)
* @param array $devices
* @param array $data (optional)
* @return bool
*/
public function send($message, array $devices, array $data = null) {
public function send($message, array $payload = [], array $devices) {
// init payload
// build payload
$payload = [
'registration_ids' => $devices,
'data' => ['message' => $message],
'data' => array_merge([
'message' => $message,
], $payload),
];
// apply additional data to payload
if (is_array($data)) {
$payload['data'] = array_merge($payload['data'], $data);
}
// init http-headers
$headers = [
'Authorization: key=' . $this->_server['token'],
......
......@@ -83,35 +83,35 @@ class WNSHandler extends PushHandler {
* @param $message
* @param array $data
*/
protected static function _createPayload($message, array $data = null) {
if (is_array($data) && isset($data['title']) && !isset($data['image'])) {
protected static function _buildPayload($message, array $data = []) {
if (isset($data['title']) && !isset($data['image'])) {
return '<?xml version="1.0" encoding="utf-8"?>' .
'<toast>' .
'<visual>' .
'<binding template="ToastText02">' .
'<text id="1">' . $messages . '</text>' .
'<text id="1">' . $message . '</text>' .
'<text id="2">' . $data['title'] . '</text>' .
'</binding>' .
'</visual>' .
'</toast>';
} elseif (is_array($data) && isset($data['title'])) {
} elseif (isset($data['title'])) {
return '<?xml version="1.0" encoding="utf-8"?>' .
'<toast>' .
'<visual>' .
'<binding template="ToastImageAndText02">' .
'<image id="1" src="' . $data['image'] . '" alt="' . $data['image'] . '"/>' .
'<text id="1">' . $messages . '</text>' .
'<text id="1">' . $message . '</text>' .
'<text id="2">' . $data['title'] . '</text>' .
'</binding>' .
'</visual>' .
'</toast>';
} elseif (is_array($data) && isset($data['image'])) {
} elseif (isset($data['image'])) {
return '<?xml version="1.0" encoding="utf-8"?>' .
'<toast>' .
'<visual>' .
'<binding template="ToastImageAndText01">' .
'<image id="1" src="' . $data['image'] . '" alt="' . $data['image'] . '"/>' .
'<text id="1">' . $messages . '</text>' .
'<text id="1">' . $message . '</text>' .
'</binding>' .
'</visual>' .
'</toast>';
......@@ -120,7 +120,7 @@ class WNSHandler extends PushHandler {
'<toast>' .
'<visual>' .
'<binding template="ToastText01">' .
'<text id="1">' . $messages . '</text>' .
'<text id="1">' . $message . '</text>' .
'</binding>' .
'</visual>' .
'</toast>';
......@@ -129,15 +129,15 @@ class WNSHandler extends PushHandler {
/**
* send notification to Microsofts live.com WNS servers
* @param string $message
* @param array $payload (optional)
* @param array $devices
* @param array $data (optional)
* @return bool
*/
public function send($message, array $devices, array $data = null) {
public function send($message, array $payload = [], array $devices) {
$result = true;
// init payload
$payload = static::_createPayload($message, $data);
// buil xml-payload
$xml = static::_buildPayload($message, $payload);
// open curl connection
$curl = curl_init();
......@@ -156,7 +156,7 @@ class WNSHandler extends PushHandler {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
// append payload
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
foreach ($devices as $clientID => $clientSecret) {
......@@ -171,12 +171,12 @@ class WNSHandler extends PushHandler {
$headers = [
'Authorization: Bearer ' . $token,
'Content-Type: text/xml',
'Content-Length: ' . strlen($payload),
'Content-Length: ' . strlen($xml),
'X-WNS-Type: wns/toast',
];
if (isset($data['tag'])) {
$headers[] = 'X-WNS-Tag: ' . $data['tag'];
if (isset($payload['tag'])) {
$headers[] = 'X-WNS-Tag: ' . $payload['tag'];
}
// apply headers
......
......@@ -74,5 +74,5 @@ abstract class PushHandler {
* @param array $data (optional)
* @return bool
*/
abstract public function send($message, array $devices, array $data = null);
abstract public function send($message, array $data = [], array $devices);
}
......@@ -47,11 +47,11 @@ class PushNotification {
* @param array $payload (optional)
* @return bool
*/
public function send($message, array $payload = null) {
public function send($message, array $payload = []) {
if (!$this->_prepare()) {
return false;
}
return $this->_handler->send($message, $this->_devices, $payload);
return $this->_handler->send($message, $payload, $this->_devices);
}
protected function _prepare() {
......
......@@ -113,7 +113,7 @@ They're all extending `PushNotification\PushHandler`
Since all push-settings are push-handler specific, the according handler has to been added to the PushNotification class before applying the configuration at the PushNotification.
Settings like *server-token* and *server-url* can be set like:
Settings as *server-token* and *server-url* can be set like:
```php
<?php
......@@ -183,7 +183,7 @@ $push->addDevice([
### sending ###
Sending a messages is simple as that:
Sending a messages is as simple as that:
```php
<?php
......
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