-
Notifications
You must be signed in to change notification settings - Fork 0
/
Push.php
executable file
·72 lines (52 loc) · 1.64 KB
/
Push.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
<?php
/**
* This class sends the notifications through GCM
*
* @author Kalpit Pandit <panditkalpit@gmail.com>
*/
class Push {
protected $apikey; // GCM API key
public function __construct($db){
$this->apikey = "Your API Key";
}
/*
* This Will call GCM url to send notifications
*/
public function sendGoogleCloudMessage($data) {
// GCM url
$url = 'https://android.googleapis.com/gcm/send';
// Message goes here
$msg = array(
'message' => array(
'name' => $data['name'],
'message' => $data['message'],
'location' => $data['location']
)
);
// Device id where we want to send notification
$ids = array($data['apid']);
$post = array(
'registration_ids' => $ids,
'data' => $msg,
'time_to_live' => 15
);
// Applicaiton registration key
$headers = array(
'Authorization: key=' . $this->apikey,
'Content-Type: application/json'
);
// Hiting GCM api via CURL..
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$result = curl_exec($ch);
if (curl_errno($ch)) {
Ajax::error(curl_errno($ch));
}
print_r($ch);
curl_close($ch);
}
}