-
Notifications
You must be signed in to change notification settings - Fork 8
/
firebase.php
183 lines (126 loc) · 5.4 KB
/
firebase.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Firebase Notification Class
*
* @category Firebase FCM Notification
* @package FCM
* @author Emmanuel O. Sobande <eosobande@gmail.com>
* @copyright Copyright (c) 2017
* @link http://github.com/eosobande/php-firebase-class
* @version 1.0
*/
class FCM {
const CONTENT_JSON = 'application/json';
const CONTENT_TEXT = 'application/x-www-form-urlencoded;charset=UTF-8';
const ERROR = 'error';
const SUCCESS = 'success';
const FAILURE = 'failure';
const CANONICAL_IDS = 'canonical_ids';
const REGISTRATION_ID = 'registration_id';
const REGISTRATION_IDS = 'registration_ids';
const RESULTS = 'results';
const UNAVAILABLE = 'Unavailable';
const INVALID = 'InvalidRegistration';
const NOT_REGISTERED = 'NotRegistered';
const EXCEEDED = 'TopicsMessageRateExceeded';
private $header;
private $content_type;
private $message;
private $response;
private $token;
private $time_to_live;
private $collapse_key;
function __construct($content_type, $time_to_live, $collapse_key) {
$this->content_type = $content_type;
$this->time_to_live = (int) $time_to_live;
$this->collapse_key = $collapse_key;
$this->headers();
}
private function headers() {
$this->headers = [
'Content-Type:' . $this->content_type,
'Authorization:key=' . FB_API_KEY
];
}
public function topics($to, $condition, $body=null, $data=null, $title=null) {
if ($to != null) {
$this->message['to'] = '/topics/' . $to;
} elseif ($condition != null) {
$this->message['condition'] = $condition;
}
$this->message($body, $data, $title);
}
public function notification($token, $body, $data=null, $title=null) {
$this->token = $token;
$this->message[self::REGISTRATION_IDS] = $this->token;
$this->message($body, $data, $title);
}
private function message($body, $data, $title) {
if ($body != null) {
$this->message['notification'] = ['title' => $title ? $title : APP_NAME, 'body' => $body];
}
if ($data != null) {
$this->message['data'] = $data;
}
if ($this->time_to_live != null) {
$this->message['time_to_live'] = $this->time_to_live;
}
if ($this->collapse_key != null) {
$this->message['collapse_key'] = $this->collapse_key;
}
$this->send();
}
private function response() {
if ((isset($this->response[self::FAILURE]) && $this->response[self::FAILURE] > 0) ||
(isset($this->response[self::CANONICAL_IDS]) && $this->response[self::CANONICAL_IDS] > 0)) {
foreach ($this->response[self::RESULTS] as $key => $result) {
if (isset($result[self::ERROR])) {
switch ($result[self::ERROR]) {
case self::UNAVAILABLE:
// do something
break;
case self::INVALID:
// do something
unset($this->token[$key]);
break;
case self::NOT_REGISTERED:
$this->remove_registration_id($this->token[$key]);
unset($this->token[$key]);
break;
case self::EXCEEDED:
// do something
break;
}
} elseif (isset($result[self::REGISTRATION_ID])) {
$this->update_registration_id($this->token[$key], $result[self::REGISTRATION_ID]);
unset($this->token[$key]);
} else {
unset($this->token[$key]);
}
}
$this->send(); // resending
}
}
private function remove_registration_id($token) {
// do something
}
private function update_registration_id($old, $new) {
// do something
}
private function send() {
if ($this->message != null) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => FB_URL,
CURLOPT_HTTPHEADER => $this->headers,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => json_encode($this->message)
]);
$this->response = json_decode(curl_exec($ch), true);
curl_close($ch);
$this->response();
}
}
}