Skip to content

Commit 881e641

Browse files
committed
add device api example
1 parent 2259592 commit 881e641

File tree

2 files changed

+175
-47
lines changed

2 files changed

+175
-47
lines changed

examples/DeviceExample.php

Lines changed: 164 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,178 @@
2020

2121
$br = '<br/>';
2222
$TAG1 = "tag1";
23+
$TAG2 = "tag2";
24+
$TAG3 = "tag3";
25+
$TAG4 = "tag4";
2326
$ALIAS1 = "alias1";
2427
$ALIAS2 = "alias2";
2528
$REGISTRATION_ID1 = "0900e8d85ef";
2629
$REGISTRATION_ID2 = "0a04ad7d8b4";
2730

28-
/*----Devices Example----*/
29-
30-
/*----Tags Example----*/
31-
32-
/*----Alias Example----*/
33-
34-
/*$result = $client->getDeviceTagAlias($REGISTRATION_ID1);
35-
echo $result;*/
36-
37-
//{"alias": "alias1", "tags": ["tag1", "tag2"]}
38-
/*$result = $client->getTags();
39-
echo $result;*/
31+
/*----Common Method----*/
32+
function printAPIRequestErrorStack($e) {
33+
$br = '<br/>';
34+
echo 'Push Fail.' . $br;
35+
echo 'Http Code : ' . $e->httpCode . $br;
36+
echo 'code : ' . $e->code . $br;
37+
echo 'Error Message : ' . $e->message . $br;
38+
echo 'Response JSON : ' . $e->json . $br;
39+
echo 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
40+
echo 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
41+
echo 'rateLimitReset : ' . $e->rateLimitReset . $br;
42+
}
43+
44+
function printAPIConnectionErrorStack($e) {
45+
$br = '<br/>';
46+
echo 'Push Fail: ' . $br;
47+
echo 'Error Message: ' . $e->getMessage() . $br;
48+
//response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
49+
echo 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
50+
}
4051

41-
/*$result = ;
42-
echo $result;*/
4352

44-
/*$result = $client->getAliasDevices($ALIAS1, array('android'));
45-
echo $result;*/
46-
/*
47-
$result = $client->updateDeviceTagAlias($REGISTRATION_ID1, '_alias', array('tag1', 'tag2'));
48-
echo $result;*/
49-
50-
//$result = $client->removeDeviceTag($REGISTRATION_ID1);
51-
//echo $result;
52-
53-
//$result = $client->removeDeviceAlias($REGISTRATION_ID1);
54-
55-
/*echo $client->isDeviceInTag($REGISTRATION_ID2, $TAG1) . $br;
56-
echo $client->updateTagDevices($TAG1, null, array($REGISTRATION_ID2)) . $br;
57-
echo $client->isDeviceInTag($REGISTRATION_ID2, $TAG1) . $br;*/
53+
/*----Devices Example----*/
54+
try {
55+
$result = $client->getDeviceTagAlias($REGISTRATION_ID1);
56+
$payload = $result->body;
57+
echo '<b>getDeviceTagAlias</b>' . $br;
58+
echo '----Alias:' . $payload['alias'] . $br;
59+
echo '----Tags:' . json_encode($payload['tags']) . $br;
60+
echo $br;
61+
} catch (APIRequestException $e) {
62+
printAPIRequestErrorStack($e);
63+
} catch (APIConnectionException $e) {
64+
printAPIConnectionErrorStack($e);
65+
}
66+
67+
try {
68+
$result = $client->removeDeviceAlias($REGISTRATION_ID1);
69+
echo '<b>removeDeviceAlias</b>' . $br;
70+
if ($result->isOk) {
71+
echo 'Remove Device Alias Success' . $br;
72+
} else {
73+
echo 'Remove Device Alias Fail' . $br;
74+
}
75+
echo $br;
76+
} catch (APIRequestException $e) {
77+
printAPIRequestErrorStack($e);
78+
} catch (APIConnectionException $e) {
79+
printAPIConnectionErrorStack($e);
80+
}
81+
82+
83+
try {
84+
$result = $client->removeDeviceTag($REGISTRATION_ID1);
85+
echo '<b>removeDeviceTag</b>' . $br;
86+
if ($result->isOk) {
87+
echo 'Remove Device Tag Success' . $br;
88+
} else {
89+
echo 'Remove Device Tag Fail' . $br;
90+
}
91+
echo $br;
92+
} catch (APIRequestException $e) {
93+
printAPIRequestErrorStack($e);
94+
} catch (APIConnectionException $e) {
95+
printAPIConnectionErrorStack($e);
96+
}
97+
98+
99+
try {
100+
$result = $client->updateDeviceTagAlias($REGISTRATION_ID1, $ALIAS1, array($TAG1, $TAG2), array($TAG3));
101+
echo '<b>updateTagDevices</b>' . $br;
102+
if ($result->isOk) {
103+
echo 'Update Device Tag and Alias Success' . $br;
104+
} else {
105+
echo 'Update Device Tag and Alias Fail' . $br;
106+
}
107+
echo $br;
108+
} catch (APIRequestException $e) {
109+
printAPIRequestErrorStack($e);
110+
} catch (APIConnectionException $e) {
111+
printAPIConnectionErrorStack($e);
112+
}
58113

59-
/*
60-
echo $client->getDeviceTagAlias($REGISTRATION_ID1);
61114

62-
echo $client->deleteAlias('_alias') . $br;
63-
echo $client->deleteTag('tag1') . $br;
115+
/*----Tags Example----*/
116+
try {
117+
$result = $client->getTags();
118+
$payload = $result->body;
119+
echo '<b>getTags</b>' . $br;
120+
echo 'Tags:' . json_encode($payload['tags']) . $br;
121+
echo $br;
122+
} catch (APIRequestException $e) {
123+
printAPIRequestErrorStack($e);
124+
} catch (APIConnectionException $e) {
125+
printAPIConnectionErrorStack($e);
126+
}
127+
128+
try {
129+
$result = $client->isDeviceInTag($REGISTRATION_ID1, $TAG1);
130+
$payload = $result->body;
131+
echo '<b>isDeviceInTag</b>' . $br;
132+
echo 'isDeviceInTag:' . json_encode($payload['result']) . $br;
133+
echo $br;
134+
} catch (APIRequestException $e) {
135+
printAPIRequestErrorStack($e);
136+
} catch (APIConnectionException $e) {
137+
printAPIConnectionErrorStack($e);
138+
}
139+
140+
try {
141+
$result = $client->updateTagDevices($TAG1, array($REGISTRATION_ID1), array($REGISTRATION_ID2));
142+
echo '<b>updateTagDevices</b>' . $br;
143+
if ($result->isOk) {
144+
echo 'Update Tag Devices Success' . $br;
145+
} else {
146+
echo 'Update Tag Devices Fail' . $br;
147+
}
148+
echo $br;
149+
} catch (APIRequestException $e) {
150+
printAPIRequestErrorStack($e);
151+
} catch (APIConnectionException $e) {
152+
printAPIConnectionErrorStack($e);
153+
}
154+
155+
try {
156+
$result = $client->deleteTag($TAG2);
157+
echo '<b>deleteTag</b>' . $br;
158+
if ($result->isOk) {
159+
echo 'Delete Tag Success' . $br;
160+
} else {
161+
echo 'Delete Tag Fail' . $br;
162+
}
163+
echo $br;
164+
} catch (APIRequestException $e) {
165+
printAPIRequestErrorStack($e);
166+
} catch (APIConnectionException $e) {
167+
printAPIConnectionErrorStack($e);
168+
}
64169

65-
echo $client->getDeviceTagAlias($REGISTRATION_ID1);*/
170+
/*----Alias Example----*/
171+
try {
172+
$result = $client->getAliasDevices($ALIAS1, array('ios', 'android'));
173+
$payload = $result->body;
174+
echo '<b>getAliasDevices</b>' . $br;
175+
echo 'Registration_ids:' . json_encode($payload['registration_ids']) . $br;
176+
echo $br;
177+
} catch (APIRequestException $e) {
178+
printAPIRequestErrorStack($e);
179+
} catch (APIConnectionException $e) {
180+
printAPIConnectionErrorStack($e);
181+
}
182+
183+
try {
184+
$result = $client->deleteAlias($ALIAS2);
185+
echo '<b>deleteAlias</b>' . $br;
186+
if ($result->isOk) {
187+
echo 'Delete Alias Success' . $br;
188+
} else {
189+
echo 'Delete Alias Fail' . $br;
190+
}
191+
echo $br;
192+
} catch (APIRequestException $e) {
193+
printAPIRequestErrorStack($e);
194+
} catch (APIConnectionException $e) {
195+
printAPIConnectionErrorStack($e);
196+
}
66197

src/JPush/JPushClient.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use JPush\Model\ReportResponse;
1010
use JPush\Model\MessageResponse;
1111
use JPush\Model\UserResponse;
12+
use JPush\Model\DeviceResponse;
1213

1314
use InvalidArgumentException;
1415

@@ -98,8 +99,7 @@ public function getDeviceTagAlias($registrationId) {
9899
'Content-Type' => 'application/json');
99100
$url = str_replace('{registration_id}' , $registrationId, self::DEVICES_URL);
100101
$response = $this->request($url, null, $header, 'GET');
101-
return $response;
102-
//{"alias": "alias1", "tags": ["tag1", "tag2"]}
102+
return new DeviceResponse($response);
103103
}
104104

105105

@@ -114,7 +114,7 @@ public function removeDeviceTag($registrationId) {
114114
'Content-Type' => 'application/json');
115115
$url = str_replace('{registration_id}' , $registrationId, self::DEVICES_URL);
116116
$response = $this->request($url, json_encode($payload), $header, 'POST');
117-
return $response;
117+
return new DeviceResponse($response);
118118
}
119119

120120
public function removeDeviceAlias($registrationId) {
@@ -128,7 +128,7 @@ public function removeDeviceAlias($registrationId) {
128128
'Content-Type' => 'application/json');
129129
$url = str_replace('{registration_id}' , $registrationId, self::DEVICES_URL);
130130
$response = $this->request($url, json_encode($payload), $header, 'POST');
131-
return $response;
131+
return new DeviceResponse($response);
132132
}
133133

134134
/**
@@ -191,7 +191,7 @@ public function updateDeviceTagAlias($registrationId, $alias = null, $addTags =
191191
'Content-Type' => 'application/json');
192192
$url = str_replace('{registration_id}' , $registrationId, self::DEVICES_URL);
193193
$response = $this->request($url, json_encode($payload), $header, 'POST');
194-
return $response;
194+
return new DeviceResponse($response);
195195
}
196196

197197
/**
@@ -204,8 +204,7 @@ public function getTags() {
204204
'Charset' => 'UTF-8',
205205
'Content-Type' => 'application/json');
206206
$response = $this->request(self::ALL_TAGS_URL, null, $header, 'GET');
207-
return $response;
208-
//{"tags":["555","tag1","tag2","0900e8d85ef"]}
207+
return new DeviceResponse($response);
209208
}
210209

211210
/**
@@ -232,8 +231,7 @@ public function isDeviceInTag($registrationId, $tag) {
232231
$url = str_replace('{tag}', $tag, self::IS_IN_TAG_URL);
233232
$url = str_replace('{registration_id}', $registrationId, $url);
234233
$response = $this->request($url, null, $header, 'GET');
235-
return $response;
236-
//{"result": false}
234+
return new DeviceResponse($response);
237235
}
238236

239237
/**
@@ -281,7 +279,7 @@ public function updateTagDevices($tag, $addDevices = null, $removeDevices = null
281279
'Content-Type' => 'application/json');
282280
$url = str_replace('{tag}', $tag, self::TAG_URL);
283281
$response = $this->request($url, json_encode($payload), $header, 'POST');
284-
return $response;
282+
return new DeviceResponse($response);
285283
}
286284

287285
/**
@@ -299,7 +297,7 @@ public function deleteTag($tag) {
299297
'Content-Type' => 'application/json');
300298
$url = str_replace('{tag}', $tag, self::TAG_URL);
301299
$response = $this->request($url, null, $header, 'DELETE');
302-
return $response;
300+
return new DeviceResponse($response);
303301
}
304302

305303
/**
@@ -337,8 +335,7 @@ public function getAliasDevices($alias, $platform = null) {
337335
'Charset' => 'UTF-8',
338336
'Content-Type' => 'application/json');
339337
$response = $this->request($url, null, $header, 'GET');
340-
return $response;
341-
//{"registration_ids":["0900e8d85ef"]}
338+
return new DeviceResponse($response);
342339
}
343340

344341
/**
@@ -357,7 +354,7 @@ public function deleteAlias($alias) {
357354
'Content-Type' => 'application/json');
358355
$url = str_replace('{alias}', $alias, self::ALIAS_URL);
359356
$response = $this->request($url, null, $header, 'DELETE');
360-
return $response;
357+
return new DeviceResponse($response);
361358
}
362359

363360
/*----Device API end----*/

0 commit comments

Comments
 (0)