-
Notifications
You must be signed in to change notification settings - Fork 1
/
PushNotification.php
67 lines (57 loc) · 1.77 KB
/
PushNotification.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
<?php
namespace VyconsultingGroup;
class PushNotification
{
private $url = 'https://fcm.googleapis.com/fcm/send';
private $key = null;
private $message = null;
private $fields=null;
private $header = null;
private $data = array();
public function __construct($apiKey)
{
$this->key = $apiKey;
$this->header = array(
'Authorization: key='.$this->key,
'Content-Type: application/json'
);
}
public function setMessage($title, $body, $vibration, $sound,$priority){
$this->message = array(
'title'=>$title,
'body'=>$body,
'vibrate'=>$vibration,
'sound'=>$sound,
'priority'=>$priority
);
}
public function setData($data){
$this->data = $data;
}
public function setField($token){
$this->fields = array(
'to'=>$token,
'notification'=>$this->message,
'data'=>array('payload'=>json_encode($this->data,JSON_FORCE_OBJECT))
);
}
public function setFields($tokens){
$this->fields = array(
'registration_ids'=>$tokens,
'notification'=>$this->message,
'data'=>array('payload'=>json_encode($this->data,JSON_FORCE_OBJECT))
);
}
public function execute(){
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL,$this->url);
curl_setopt( $ch,CURLOPT_POST,true);
curl_setopt( $ch,CURLOPT_HTTPHEADER,$this->header);
curl_setopt( $ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt( $ch,CURLOPT_POSTFIELDS,json_encode($this->fields));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}