-
Notifications
You must be signed in to change notification settings - Fork 399
/
FCMSender.php
118 lines (101 loc) · 3.63 KB
/
FCMSender.php
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
<?php
namespace LaravelFCM\Sender;
use LaravelFCM\Message\Topics;
use LaravelFCM\Request\Request;
use LaravelFCM\Message\Options;
use LaravelFCM\Message\PayloadData;
use LaravelFCM\Response\GroupResponse;
use LaravelFCM\Response\TopicResponse;
use GuzzleHttp\Exception\ClientException;
use LaravelFCM\Response\DownstreamResponse;
use LaravelFCM\Message\PayloadNotification;
/**
* Class FCMSender.
*/
class FCMSender extends HTTPSender
{
const MAX_TOKEN_PER_REQUEST = 1000;
/**
* send a downstream message to.
*
* - a unique device with is registration Token
* - or to multiples devices with an array of registrationIds
*
* @param string|array $to
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
*
* @return DownstreamResponse|null
*/
public function sendTo($to, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
{
$response = null;
if (is_array($to) && !empty($to)) {
$partialTokens = array_chunk($to, self::MAX_TOKEN_PER_REQUEST, false);
foreach ($partialTokens as $tokens) {
$request = new Request($tokens, $options, $notification, $data);
$responseGuzzle = $this->post($request);
$responsePartial = new DownstreamResponse($responseGuzzle, $tokens);
if (!$response) {
$response = $responsePartial;
} else {
$response->merge($responsePartial);
}
}
} else {
$request = new Request($to, $options, $notification, $data);
$responseGuzzle = $this->post($request);
$response = new DownstreamResponse($responseGuzzle, $to);
}
return $response;
}
/**
* Send a message to a group of devices identified with them notification key.
*
* @param $notificationKey
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
*
* @return GroupResponse
*/
public function sendToGroup($notificationKey, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
{
$request = new Request($notificationKey, $options, $notification, $data);
$responseGuzzle = $this->post($request);
return new GroupResponse($responseGuzzle, $notificationKey);
}
/**
* Send message devices registered at a or more topics.
*
* @param Topics $topics
* @param Options|null $options
* @param PayloadNotification|null $notification
* @param PayloadData|null $data
*
* @return TopicResponse
*/
public function sendToTopic(Topics $topics, Options $options = null, PayloadNotification $notification = null, PayloadData $data = null)
{
$request = new Request(null, $options, $notification, $data, $topics);
$responseGuzzle = $this->post($request);
return new TopicResponse($responseGuzzle, $topics);
}
/**
* @internal
*
* @param \LaravelFCM\Request\Request $request
*
* @return null|\Psr\Http\Message\ResponseInterface
*/
protected function post($request)
{
try {
$responseGuzzle = $this->client->request('post', $this->url, $request->build());
} catch (ClientException $e) {
$responseGuzzle = $e->getResponse();
}
return $responseGuzzle;
}
}