1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: User |
5
|
|
|
* Date: 4/28/2019 |
6
|
|
|
* Time: 10:30 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace crocodicstudio\crudbooster\helpers; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class FCM |
13
|
|
|
{ |
14
|
|
|
private static $title; |
15
|
|
|
private $message; |
16
|
|
|
private $data; |
17
|
|
|
private $fields; |
18
|
|
|
|
19
|
|
|
public static function title($title) { |
20
|
|
|
static::$title = $title; |
|
|
|
|
21
|
|
|
return new static; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function message($message) { |
25
|
|
|
$this->message = $message; |
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function data(array $data) { |
30
|
|
|
$this->data = $data; |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function tokenIOS(array $tokens) { |
35
|
|
|
$data['title'] = static::$title; |
|
|
|
|
36
|
|
|
$data['message'] = $this->message; |
37
|
|
|
$this->fields = [ |
38
|
|
|
'registration_ids' => $tokens, |
39
|
|
|
'data' => $this->data, |
40
|
|
|
'content_available' => true, |
41
|
|
|
'notification' => [ |
42
|
|
|
'sound' => 'default', |
43
|
|
|
'badge' => 0, |
44
|
|
|
'title' => trim(strip_tags(static::$title)), |
45
|
|
|
'body' => trim(strip_tags($this->message)), |
46
|
|
|
], |
47
|
|
|
'priority' => 'high', |
48
|
|
|
]; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function tokenAndroid(array $tokens) { |
53
|
|
|
$data['title'] = static::$title; |
|
|
|
|
54
|
|
|
$data['message'] = $this->message; |
55
|
|
|
$this->fields = [ |
56
|
|
|
'registration_ids' => $tokens, |
57
|
|
|
'data' => $this->data, |
58
|
|
|
'content_available' => true, |
59
|
|
|
'priority' => 'high', |
60
|
|
|
]; |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function send() { |
65
|
|
|
$url = 'https://fcm.googleapis.com/fcm/send'; |
66
|
|
|
$headers = [ |
67
|
|
|
'Authorization:key='.cbConfig('GOOGLE_FCM_SERVER_KEY'), |
68
|
|
|
'Content-Type:application/json', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
if(static::$title && $this->message) { |
|
|
|
|
72
|
|
|
$ch = curl_init($url); |
73
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
|
|
|
|
74
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
75
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
76
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
77
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->fields)); |
78
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
79
|
|
|
$result = curl_exec($ch); |
|
|
|
|
80
|
|
|
curl_close($ch); |
|
|
|
|
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |