-
Notifications
You must be signed in to change notification settings - Fork 21
/
Fcm.php
202 lines (173 loc) · 5.17 KB
/
Fcm.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* FCM simple server side implementation in PHP
*
* @author Abhishek
*/
class Fcm
{
/** @var string push message title */
private $title;
/** @var string message */
private $message;
/** @var string URL String */
private $image;
/** @var array Custom payload */
private $data;
/**
* flag indicating whether to show the push notification or not
* this flag will be useful when perform some opertation
* in background when push is recevied
*/
/** @var bool set background or not */
private $is_background;
/**
* Function to set the title
*
* @param string $title The title of the push message
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Function to set the message
*
* @param string $message Message
*/
public function setMessage($message)
{
$this->message = $message;
}
/**
* Function to set the image (optional)
*
* @param string $imageUrl URI string of image
*/
public function setImage($imageUrl)
{
$this->image = $imageUrl;
}
/**
* Function to set the custom payload (optional)
*
* eg:
* $payload = array('user' => 'user1');
*
* @param array $data Custom data array
*/
public function setPayload($data)
{
$this->data = $data;
}
/**
* Function to specify if is set background (optional)
*
* @param bool $is_background
*/
public function setIsBackground($is_background)
{
$this->is_background = $is_background;
}
/**
* Generating the push message array
*
* @return array array of the push notification data to be send
*/
public function getPush()
{
$res = array();
$res['data']['title'] = $this->title;
$res['data']['is_background'] = $this->is_background;
$res['data']['message'] = $this->message;
$res['data']['image'] = $this->image;
$res['data']['payload'] = $this->data;
$res['data']['timestamp'] = date('Y-m-d G:i:s');
return $res;
}
/**
* Function to send notification to a single device
*
* @param string $to registration id of device (device token)
* @param array $message push notification array returned from getPush()
*
* @return array array of notification data and to address
*/
public function send($to, $message)
{
$fields = array(
'to' => $to,
'data' => $message,
);
return $this->sendPushNotification($fields);
}
/**
* Function to send notification to a topic by topic name
*
* @param string $to topic
* @param array $message push notification array returned from getPush()
*
* @return array array of notification data and to address (topic)
*/
public function sendToTopic($to, $message)
{
$fields = array(
'to' => '/topics/' . $to,
'data' => $message,
);
return $this->sendPushNotification($fields);
}
/**
* Function to send notification to multiple users by firebase registration ids
*
* @param array $to array of registration ids of devices (device tokens)
* @param array $message push notification array returned from getPush()
*
* @return array array of notification data and to addresses
*/
public function sendMultiple($registration_ids, $message)
{
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
return $this->sendPushNotification($fields);
}
/**
* Function makes curl request to firebase servers
*
* @param array $fields array of registration ids of devices (device tokens)
*
* @return string returns result from FCM server as json
*/
private function sendPushNotification($fields)
{
$CI = &get_instance();
$CI->load->config('androidfcm'); //loading of config file
// Set POST variables
$url = $CI->config->item('fcm_url');
$headers = array(
'Authorization: key=' . $CI->config->item('key'),
'Content-Type: application/json',
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === false) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
return $result;
}
}