forked from parse-community/parse-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParsePush.php
More file actions
128 lines (117 loc) · 4.02 KB
/
Copy pathParsePush.php
File metadata and controls
128 lines (117 loc) · 4.02 KB
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
<?php
/**
* Class ParsePush | Parse/ParsePush.php
*/
namespace Parse;
use Exception;
/**
* Class ParsePush - Handles sending push notifications with Parse.
*
* @author Fosco Marotto <fjm@fb.com>
* @package Parse
*/
class ParsePush
{
/**
* Sends a push notification.
*
* @param array $data The data of the push notification. Valid fields
* are:
* channels - An Array of channels to push to.
* push_time - A Date object for when to send the push.
* expiration_time - A Date object for when to expire
* the push.
* expiration_interval - The seconds from now to expire the push.
* where - A ParseQuery over ParseInstallation that is used to match
* a set of installations to push to.
* data - The data to send as part of the push
* @param bool $useMasterKey Whether to use the Master Key for the request
*
* @throws \Exception, ParseException
*
* @return mixed
*/
public static function send($data, $useMasterKey = false)
{
if (isset($data['expiration_time'])
&& isset($data['expiration_interval'])
) {
throw new Exception(
'Both expiration_time and expiration_interval can\'t be set.',
138
);
}
if (isset($data['where'])) {
if ($data['where'] instanceof ParseQuery) {
$where_options = $data['where']->_getOptions();
if (!isset($where_options['where'])) {
$data['where'] = '{}';
} else {
$data['where'] = $data['where']->_getOptions()['where'];
}
} else {
throw new Exception(
'Where parameter for Parse Push must be of type ParseQuery',
111
);
}
}
if (isset($data['push_time'])) {
//Local push date format is different from iso format generally used in Parse
//Schedule does not work if date format not correct
$data['push_time'] = ParseClient::getPushDateFormat($data['push_time'], isset($data['local_time']));
}
if (isset($data['expiration_time'])) {
$data['expiration_time'] = ParseClient::_encode(
$data['expiration_time'],
false
)['iso'];
}
return ParseClient::_request(
'POST',
'push',
null,
json_encode(ParseClient::_encode($data, true)),
$useMasterKey,
false,
'application/json',
true
);
}
/**
* Returns whether or not the given response has a push status
* Checks to see if X-Push-Status-Id is present in $response
*
* @param array $response Response from ParsePush::send
* @return bool
*/
public static function hasStatus($response)
{
return(
isset($response['_headers']) &&
isset($response['_headers']['X-Parse-Push-Status-Id'])
);
}
/**
* Returns the PushStatus for a response from ParsePush::send
*
* @param array $response Response from ParsePush::send
* @return null|ParsePushStatus
*/
public static function getStatus($response)
{
if (!isset($response['_headers'])) {
// missing headers
return null;
}
$headers = $response['_headers'];
if (!isset($headers['X-Parse-Push-Status-Id'])) {
// missing push status id
return null;
}
// get our push status id
$pushStatusId = $response['_headers']['X-Parse-Push-Status-Id'];
// return our push status if it exists
return ParsePushStatus::getFromId($pushStatusId);
}
}