1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Firebase Service bundle. |
5
|
|
|
* |
6
|
|
|
* (c) Gary Houbre <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ghoubre\FirebaseServiceBundle\Service; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class FirebaseService |
18
|
|
|
* |
19
|
|
|
* @author Gary HOUBRE <[email protected]> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class FirebaseNotificationService |
23
|
|
|
{ |
24
|
|
|
protected $server_key; |
25
|
|
|
|
26
|
|
|
protected $content_available; |
27
|
|
|
|
28
|
|
|
protected $time_to_live; |
29
|
|
|
|
30
|
|
|
protected $message; |
31
|
|
|
|
32
|
|
|
const FCM_URL = "https://fcm.googleapis.com/fcm/send"; |
33
|
|
|
|
34
|
|
|
public function __construct($server_key, $content_available = true, $time_to_live = 30) |
35
|
|
|
{ |
36
|
|
|
$this->server_key = $server_key; |
37
|
|
|
$this->content_available = $content_available; |
38
|
|
|
$this->time_to_live = $time_to_live; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function createMessage($message) |
42
|
|
|
{ |
43
|
|
|
if (!array_key_exists("to", $message)) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
$this->message["to"] = $message["to"]; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
if (array_key_exists("title", $message)) { |
50
|
|
|
$this->message["notification"]["title"] = $message["title"]; |
51
|
|
|
} |
52
|
|
|
if (array_key_exists("body", $message)) { |
53
|
|
|
$this->message["notification"]["body"] = $message["body"]; |
54
|
|
|
} |
55
|
|
|
if (array_key_exists("badge", $message)) { |
56
|
|
|
$this->message["notification"]["badge"] = $message["badge"]; |
57
|
|
|
} |
58
|
|
|
if (array_key_exists("sound", $message)) { |
59
|
|
|
$this->message["notification"]["sound"] = $message["sound"]; |
60
|
|
|
} |
61
|
|
|
if (array_key_exists("data", $message)) { |
62
|
|
|
$this->message["data"] = $message["data"]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->message["content_available"] = true; |
66
|
|
|
|
67
|
|
|
$this->message["time_to_live"] = 30; |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function sendNotification() |
73
|
|
|
{ |
74
|
|
|
$content = json_encode($this->message); |
75
|
|
|
|
76
|
|
|
$curl = curl_init(self::FCM_URL); |
77
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); |
78
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
79
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
80
|
|
|
"Content-type: application/json", |
81
|
|
|
"Authorization: key=".$this->server_key |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
$curlConf["header"] = curl_getinfo($curl, CURLOPT_HEADER); |
|
|
|
|
85
|
|
|
|
86
|
|
|
curl_setopt($curl, CURLOPT_POST, true); |
87
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $content); |
88
|
|
|
// @codeCoverageIgnoreStart |
89
|
|
|
$json_response = curl_exec($curl); |
90
|
|
|
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
91
|
|
|
// @codeCoverageIgnoreEnd |
92
|
|
|
curl_close($curl); |
93
|
|
|
|
94
|
|
|
return [ |
95
|
|
|
'curlConf' => $curlConf, |
96
|
|
|
'status' => $status, |
97
|
|
|
'response' => $json_response |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function parseResult(array $result) |
102
|
|
|
{ |
103
|
|
|
if ($result["status"] == 200) { |
104
|
|
|
return json_decode($result["response"], true); |
105
|
|
|
} elseif ($result["status"] == 400) { |
106
|
|
|
throw new Exception("Invalid fields or request not parsed as JSON"); |
107
|
|
|
} elseif ($result["status"] == 401) { |
108
|
|
|
throw new Exception("Authenticate Error"); |
109
|
|
|
} elseif ($result["status"] >= 500 && $result["status"] < 600) { |
110
|
|
|
throw new Exception("Internal error in the FCM connection server"); |
111
|
|
|
} else { |
112
|
|
|
throw new Exception("Look status and check status in firebase.google.com"); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|