forked from tomasell/push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
116 lines (105 loc) · 3.99 KB
/
Copy pathController.php
File metadata and controls
116 lines (105 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Bread\Push\Notification;
use Bread\Push\Device;
use Bread\Configuration\Manager as Configuration;
use Exception;
class Controller
{
const URL_ANDROID = 'https://android.googleapis.com/gcm/send';
const URL_IOS = 'ssl://gateway.sandbox.apple.com:2195';
private $devices;
private $domain;
public function __construct($devices, $domain = '__default__')
{
$this->devices = $devices;
$this->domain = $domain;
}
public function notify($message = null, $badge = null, $sound = null, $fields = array())
{
$android = array();
$ios = array();
foreach ($this->devices as $device) {
switch ($device->type) {
case Device\Model::TYPE_ANDROID:
$android[] = $device->itemId;
break;
case Device\Model::TYPE_IOS:
$ios[] = $device->itemId;
break;
}
}
if ($android) {
return $this->notifyAndroid($android, $message, $badge, $sound, $fields);
}
if ($ios) {
return $this->notifyIos($ios, $message, $badge, $sound, $fields);
}
}
protected function notifyAndroid($devices, $message, $badge, $sound, $data = array())
{
if (null === $message) {
return false;
}
$data['message'] = $message;
$fields = array(
'registration_ids' => (array) $devices,
'data' => $data
);
$payload = json_encode($fields, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
$ch = curl_init(self::URL_ANDROID);
if (Configuration::get(__CLASS__, 'proxy', $this->domain)) {
curl_setopt($ch, CURLOPT_PROXY, Configuration::get(__CLASS__, 'proxy', $this->domain));
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getAndroidHeaders(Configuration::get(__CLASS__, 'android.apikey', $this->domain)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
if ($result === false) {
error_log(curl_error($ch));
throw new Exception(curl_error($ch));
}
curl_close($ch);
return $result;
}
protected function notifyIos($devices, $message, $badge, $sound, $data = array())
{
$stream = stream_context_create();
stream_context_set_option($stream, 'ssl', 'local_cert', Configuration::get(__CLASS__, 'ios.local_cert', $this->domain));
stream_context_set_option($stream, 'ssl', 'passphrase', Configuration::get(__CLASS__, 'ios.passphrase', $this->domain));
$fp = stream_socket_client(self::URL_IOS, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $stream);
if (!$fp) {
throw new Exception("Failed to connect: $err $errstr" . PHP_EOL);
}
$data['aps'] = array();
if (is_string($message)) {
$data['aps']['alert'] = $message;
}
if (is_int($badge)) {
$data['aps']['badge'] = $badge;
}
if (is_string($sound) || is_string($message)) {
$data['aps']['sound'] = is_string($sound) ? $sound : 'default';
}
$payload = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
$msg = '';
foreach ($devices as $device) {
$msg .= chr(0)
. pack('n', 32)
. pack('H*', $device)
. pack('n', strlen($payload))
. $payload;
}
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);
return $result;
}
protected function getAndroidHeaders($apikey)
{
return array(
'Authorization: key=' . $apikey,
'Content-Type: application/json'
);
}
}