This repository was archived by the owner on Jan 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathClient.php
More file actions
146 lines (123 loc) · 3.46 KB
/
Client.php
File metadata and controls
146 lines (123 loc) · 3.46 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/*
* (c) Jeroen van den Enden <info@endroid.nl>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Endroid\Gcm;
use Buzz\Browser;
use Buzz\Client\MultiCurl;
use Buzz\Message\Response;
class Client
{
/**
* @var string
*/
protected $apiUrl = 'https://android.googleapis.com/gcm/send';
/**
* @var string
*/
protected $apiKey;
/**
* @var string
*/
protected $registrationIdMaxCount = 1000;
/**
* @var MultiCurl
*/
protected $client;
/**
* @var Browser
*/
protected $browser;
/**
* @var Response[]
*/
protected $responses;
/**
* Class constructor.
*
* @param $apiKey
* @param null $apiUrl
*/
public function __construct($apiKey, $apiUrl = null)
{
$this->apiKey = $apiKey;
if ($apiUrl) {
$this->apiUrl = $apiUrl;
}
$this->client = new MultiCurl();
$this->client->setVerifyPeer(false);
$this->browser = new Browser($this->client);
}
/**
* Sends the message via the GCM server.
*
* @param mixed $data
* @param array $registrationIds
* @param array $options
*
* @return bool
*/
public function send($data, array $registrationIds = [], array $options = [])
{
$this->responses = [];
$data = !empty($data) ? array_merge($options, array('data' => $data)) : $options;
if (isset($options['to'])) {
$this->responses[] = $this->browser->post($this->apiUrl, $this->getHeaders(), json_encode($data));
} elseif (count($registrationIds) > 0) {
// Chunk number of registration ID's according to the maximum allowed by GCM
$chunks = array_chunk($registrationIds, $this->registrationIdMaxCount);
// Perform the calls (in parallel)
foreach ($chunks as $registrationIds) {
$data['registration_ids'] = $registrationIds;
$this->responses[] = $this->browser->post($this->apiUrl, $this->getHeaders(), json_encode($data));
}
}
$this->client->flush();
foreach ($this->responses as $response) {
$message = json_decode($response->getContent());
if ($message === null || $message->success == 0 || $message->failure > 0) {
return false;
}
}
return true;
}
/**
* Sends the data to the given registration token, notification key, or topic via the GCM server.
*
* @param mixed $data
* @param string $topic The value must be a registration token, notification key, or topic. Default global topic.
* @param array $options to add along with message, such as collapse_key, time_to_live, delay_while_idle
*
* @return bool
*/
public function sendTo($data, $topic = '/topics/global', array $options = [])
{
$options['to'] = $topic;
return $this->send($data, [], $options);
}
/**
* Returns the headers.
*
* @return array
*/
protected function getHeaders()
{
$headers = [
'Authorization: key='.$this->apiKey,
'Content-Type: application/json',
];
return $headers;
}
/**
* Returns the responses.
*
* @return Response[]
*/
public function getResponses()
{
return $this->responses;
}
}