-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCMPush.php
92 lines (74 loc) · 2.31 KB
/
FCMPush.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
<?php
/*
Class to send push notifications using Firebase Cloud Messaging
Esta clase usa para el envio de notificaciones Firebase Cloud Messaging
Example usage
Ejemplos de uso
-----------------------
$obj = new FCMPush($apiKey); //the key server is only one
$obj->setDevices($devices); //devices is array
$response = $obj->send($title,$body,$data); //data is array
-----------------------
$apiKey Your FCM api key // Es la clave de servidor que encuentra en la consola de Firebase
$devices An array or string of registered device tokens // un array con los token de los dispositivos a los cuales desea mandarle una notificacion
$message The mesasge you want to push out //mensaje que desea mandar
@author Federico Penin
*/
class FCMPush{
var $url = "https://fcm.googleapis.com/fcm/send";
var $serverApiKey = "";
var $devices = array();
function __construct($serverKey){
$this->serverApiKey = $serverKey;
}
//here are the devices (array or individual hash)
function setDevices($deviceHash){
if(is_array($deviceHash)){
$this->devices = $deviceHash;
} else {
$this->devices = array($deviceHash);
}
}
//the function that send the message
function send($title, $body, $data = false){
if(!is_array($this->devices) || count($this->devices) == 0){
return "No devices set";
}
if(strlen($this->serverApiKey) < 8){
return "Server API Key not set";
}
if(is_array($data)){
foreach ($data as $key => $value) {
$valores[$key] = $value;
}
}
$fcmMsg = array(
'title' => $title,
'body'=>$body
);
$fcmFields = array(
'registration_ids' => $this->devices,
'priority' => 'high',
'notification' => $fcmMsg
);
if(is_array($data)){
foreach ($data as $key => $value) {
$fcmFields['data'][$key] = $value;
}
}
$headers = array(
'Authorization: key='.$this->serverApiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt($ch,CURLOPT_POST, true );
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fcmFields));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}