-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCM.php
62 lines (47 loc) · 1.52 KB
/
GCM.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
<?php
/**
* Google Cloud Messaging (GCM)
*
* @author Mohamed Chaawa
*/
class GCM {
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function sendNotification($registation_ids, $title, $message) {
$success = true;
define("GOOGLE_API_KEY", 'PUT-YOUR-GOOGLE_API_KEY-HERE');
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registation_ids,
'data' => array('message' => $message, 'tickerText' => $message, 'contentTitle' => $title, 'contentText' => $message),
);
$headers = array(
'Authorization: key=' . GOOGLE_API_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) {
$success = false;
}
// Close connection
curl_close($ch);
return $success;
}
}
?>