-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirebaseNotification-class.php
53 lines (41 loc) · 1.24 KB
/
FirebaseNotification-class.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
<?php
class FirebaseNotification {
// Replace with your Firebase project's server key
private $serverKey;
// Replace with the token of the device you want to send the notification to
private $deviceToken;
public function __construct($serverKey, $deviceToken) {
$this->serverKey = $serverKey;
$this->deviceToken = $deviceToken;
}
public function send($title, $body) {
// Create the message payload
$data = [
"notification" => [
"title" => $title,
"body" => $body
],
"to" => $this->deviceToken
];
// Encode the payload as JSON
$jsonData = json_encode($data);
// Create a new cURL request
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
// Set the request headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: key={$this->serverKey}"
]);
// Set the request body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
// Set the cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Send the request and get the response
$response = curl_exec($ch);
// Close the cURL request
curl_close($ch);
return $response;
}
}
?>