forked from sngrl/php-firebase-cloud-messaging
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
130 lines (115 loc) · 3.56 KB
/
Copy pathClient.php
File metadata and controls
130 lines (115 loc) · 3.56 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
<?php
namespace sngrl\PhpFirebaseCloudMessaging;
use GuzzleHttp;
/**
* @author sngrl
*/
class Client implements ClientInterface
{
const DEFAULT_API_URL = 'https://fcm.googleapis.com/fcm/send';
const DEFAULT_TOPIC_ADD_SUBSCRIPTION_API_URL = 'https://iid.googleapis.com/iid/v1:batchAdd';
const DEFAULT_TOPIC_REMOVE_SUBSCRIPTION_API_URL = 'https://iid.googleapis.com/iid/v1:batchRemove';
private $apiKey;
private $proxyApiUrl;
private $guzzleClient;
public function injectGuzzleHttpClient(GuzzleHttp\ClientInterface $client)
{
$this->guzzleClient = $client;
}
/**
* add your server api key here
* read how to obtain an api key here: https://firebase.google.com/docs/server/setup#prerequisites
*
* @param string $apiKey
*
* @return \sngrl\PhpFirebaseCloudMessaging\Client
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
return $this;
}
/**
* people can overwrite the api url with a proxy server url of their own
*
* @param string $url
*
* @return \sngrl\PhpFirebaseCloudMessaging\Client
*/
public function setProxyApiUrl($url)
{
$this->proxyApiUrl = $url;
return $this;
}
/**
* sends your notification to the google servers and returns a guzzle repsonse object
* containing their answer.
*
* @param Message $message
*
* @return \Psr\Http\Message\ResponseInterface
* @throws \GuzzleHttp\Exception\RequestException
*/
public function send(Message $message)
{
return $this->guzzleClient->post(
$this->getApiUrl(),
[
'headers' => [
'Authorization' => sprintf('key=%s', $this->apiKey),
'Content-Type' => 'application/json'
],
'body' => json_encode($message)
]
);
}
/**
* @param integer $topic_id
* @param array|string $recipients_tokens
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function addTopicSubscription($topic_id, $recipients_tokens)
{
return $this->processTopicSubscription($topic_id, $recipients_tokens, self::DEFAULT_TOPIC_ADD_SUBSCRIPTION_API_URL);
}
/**
* @param integer $topic_id
* @param array|string $recipients_tokens
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function removeTopicSubscription($topic_id, $recipients_tokens)
{
return $this->processTopicSubscription($topic_id, $recipients_tokens, self::DEFAULT_TOPIC_REMOVE_SUBSCRIPTION_API_URL);
}
/**
* @param integer $topic_id
* @param array|string $recipients_tokens
* @param string $url
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function processTopicSubscription($topic_id, $recipients_tokens, $url)
{
if (!is_array($recipients_tokens))
$recipients_tokens = [$recipients_tokens];
return $this->guzzleClient->post(
$url,
[
'headers' => [
'Authorization' => sprintf('key=%s', $this->apiKey),
'Content-Type' => 'application/json'
],
'body' => json_encode([
'to' => '/topics/' . $topic_id,
'registration_tokens' => $recipients_tokens,
])
]
);
}
private function getApiUrl()
{
return isset($this->proxyApiUrl) ? $this->proxyApiUrl : self::DEFAULT_API_URL;
}
}