Skip to content

Commit 6d3eb6d

Browse files
committed
fix some bug
1 parent cefe917 commit 6d3eb6d

32 files changed

+970
-220
lines changed

.idea/jpush-api-php-client.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 315 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/example.php

Lines changed: 102 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,123 @@
11
<?php
2-
include_once '../jpushv3/JPushClient.php';
3-
include_once '../jpushv3/Notification.php';
4-
include_once '../jpushv3/Message.php';
2+
include_once '../jpush/JPushClient.php';
3+
include_once '../jpush/model/Audience.php';
4+
include_once '../jpush/model/Message.php';
5+
include_once '../jpush/model/notification/Notification.php';
6+
include_once '../jpush/model/notification/IOSNotification.php';
7+
include_once '../jpush/model/notification/AndroidNotification.php';
8+
include_once '../jpush/model/notification/WinphoneNotification.php';
9+
include_once '../jpush/model/Options.php';
10+
include_once '../jpush/model/Platform.php';
11+
include_once '../jpush/model/PushPayload.php';
512

613
$master_secret = 'd94f733358cca97b18b2cb98';
714
$app_key='47a3ddda34b2602fa9e17c01';
15+
$tag = "tag1,tag2";
16+
$tag_and = "tag3,tag4";
17+
$alias = "alias1,alias2";
18+
$registration_id = "id1,id2";
819
$client = new JPushClient($app_key, $master_secret);
920

10-
$notification = new Notification('This is content');
11-
$message = new Message("This is content");
21+
/* init start */
22+
$platform = new Platform();
23+
$audience = new Audience();
24+
$message = new Message();
25+
$options = new Options();
26+
27+
$notification = new Notification();
28+
$ios = new IOSNotification();
29+
$android = new AndroidNotification();
30+
$winphone = new WinphoneNotification();
31+
32+
//set platform params
33+
$platform->ios = true;
34+
$platform->winphone = true;
35+
36+
37+
//set audience params
38+
$audience->tag = $tag;
39+
$audience->tag_and = $tag_and;
40+
$audience->alias = $alias;
41+
$audience->registration_id = $registration_id;
42+
43+
//set options params
44+
$options->sendno = 1;
45+
$options->apns_production = true;
46+
$options->override_msg_id = 2;
47+
$options->time_to_live = 60;
48+
49+
//set message params
50+
$message->msg_content = "message content test";
51+
$message->title = "message title test";
52+
$message->content_type = "message content type test";
53+
$message->extras = array("key1"=>"value1", "key2"=>"value2");
54+
55+
//set options params
56+
$options->sendno = 1;
57+
$options->apns_production = true;
58+
$options->override_msg_id = 2;
59+
$options->time_to_live = 60;
60+
61+
//set notification params
62+
$ios->alert = "ios notification alert test";
63+
$ios->sound = "happy";
64+
$ios->badge = 1;
65+
$ios->extras = array("key1"=>"value1", "key2"=>"value2");
66+
$ios->content_availabe = 1;
67+
68+
$android->alert = "android notification alert test";
69+
$android->title = "android notification title test";
70+
$android->builder_id = 1;
71+
$android->extras = array("key1"=>"value1", "key2"=>"value2");
72+
73+
$winphone->alert = "winphone notification alert test";
74+
$winphone->title = "winphone notification title test";
75+
$winphone->_open_page = "/friends.xaml";
76+
$winphone->extras = array("key1"=>"value1", "key2"=>"value2");
77+
78+
$notification->alert = "notification alert test";
79+
$notification->android = $android;
80+
$notification->ios = $ios;
81+
$notification->winphone = $winphone;
82+
83+
/* init end */
84+
85+
86+
1287

1388
//发送广播通知
14-
$result1 = $client->send($notification);
89+
$payload1 = new PushPayload();
90+
$payload1->notification = $notification;
91+
$result1 = $client->sendPush($payload1);
1592

1693
//发送广播自定义信息
17-
$result2 = $client->send($message);
94+
$payload2 = new PushPayload();
95+
$payload2->message = $message;
96+
$result2 = $client->sendPush($payload2);
1897

1998
//发送Tag通知
20-
$notification->setTag("555,666");
21-
$result3 = $client->send($notification);
22-
23-
//其他属性测试
24-
$notification->setTitle("This is title")
25-
->setBuilderId(1)
26-
->setExtras(array("key"=>"value"))
27-
->setSound("happy")
28-
->setBadge(1)
29-
->setContentAvailabe(true)
30-
->setOpenPage("/friends.xaml");
31-
$result4 = $client->send($notification);
32-
33-
//其他属性测试
34-
$message->setTitle("This is title")->setExtras(array("key"=>"value"))->setContentType("content_type");
35-
$result5 = $client->send($message);
99+
$options3 = new Options();
100+
$audience3 = new Audience();
101+
$notification3 = new Notification();
102+
$notification3->alert = "tag tests";
103+
$audience3->tag = "555";
104+
//$audience3->alias = $alias;
105+
$options3->sendno = 441740752;
106+
107+
$payload3 = new PushPayload();
108+
$payload3->audience = $audience3;
109+
$payload3->notification = $notification3;
110+
$payload3->options = $options3;
111+
$result3 = $client->sendPush($payload3);
112+
113+
36114

37115
//组装查询统计信息字符串
38116
$msg_ids1 = '636946851';
39117
$msg_ids2 = '636946851,1173817748,636946865';
40118
$result6 = $client->getReport($msg_ids1);
41119
$result7 = $client->getReport($msg_ids2);
42120

43-
//错误测试
44-
$notification->setTitle(["This is title"]);
45-
$result8 = $client->send($notification);
46-
$notification->setTitle("This is title");
47-
48-
$notification->setBadge("1");
49-
$result9 = $client->send($notification);
50-
$notification->setBadge(1);
51121

52122
?>
53123

@@ -81,14 +151,7 @@
81151
<td>发送Tag通知</td>
82152
<td><?php echo $result3; ?></td>
83153
</tr>
84-
<tr>
85-
<td>其他属性测试</td>
86-
<td><?php echo $result4; ?></td>
87-
</tr>
88-
<tr>
89-
<td>其他属性测试(自定义)</td>
90-
<td><?php echo $result5; ?></td>
91-
</tr>
154+
92155
</table>
93156

94157
<h3>Report Example</h3>
@@ -104,23 +167,7 @@
104167
</tr>
105168
</table>
106169

107-
<h3>Error Example</h3>
108-
<table>
109-
<tr><th>错误信息</th><th>返回JSON</th></tr>
110-
<tr>
111-
<td>标题错误</td>
112-
<td><?php echo $result8; ?></td>
113-
</tr>
114-
<tr>
115-
<td>Badge错误</td>
116-
<td><?php echo $result9; ?></td>
117-
</tr>
118-
<tr>
119-
<td>更多错误</td>
120-
<td>待添加</td>
121-
</tr>
122170

123-
</table>
124171

125172

126173

examples/example2.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
include_once '../jpush/model/Audience.php';
3+
include_once '../jpush/model/Message.php';
4+
include_once '../jpush/model/notification/Notification.php';
5+
include_once '../jpush/model/notification/IOSNotification.php';
6+
include_once '../jpush/model/notification/AndroidNotification.php';
7+
include_once '../jpush/model/notification/WinphoneNotification.php';
8+
include_once '../jpush/model/Options.php';
9+
include_once '../jpush/model/Platform.php';
10+
include_once '../jpush/model/PushPayload.php';
11+
12+
$android = new AndroidNotification();
13+
$android->alert = "android alert";
14+
$android->title = "android title";
15+
$android->builder_id = 1;
16+
$android->extras = array("key1"=>"value1", "key2"=>"value2");
17+
$json_arr = $android->toJSON();
18+
19+
$winphone = new WinphoneNotification();
20+
$winphone->alert = "winphone alert";
21+
$winphone->title = "winphone title";
22+
$winphone->_open_page = "/abc.fmal";
23+
$winphone->extras = array("key1"=>"value1", "key2"=>"value2");
24+
$json_arr = $winphone->toJSON();
25+
26+
$ios = new IOSNotification();
27+
$ios->alert = "ios alert";
28+
$ios->badge = 1;
29+
$ios->content_availabe = 1;
30+
$ios->sound = "happy";
31+
$ios->extras = array("key1"=>"value1", "key2"=>"value2");
32+
$json_arr = $ios->toJSON();
33+
34+
$notification = new Notification();
35+
$notification->alert = "notification alert";
36+
$notification->ios = $ios;
37+
$notification->winphone = $winphone;
38+
$notification->android = $android;
39+
$json_arr = $notification->toJSON();
40+
41+
$options = new Options();
42+
$options->apns_production = 1;
43+
$options->override_msg_id = 123456;
44+
$options->sendno = 654321;
45+
$options->time_to_live = 60;
46+
$json_arr = $options->toJSON();
47+
48+
$platform = new Platform();
49+
$platform->ios = true;
50+
$platform->android = true;
51+
$platform->winphone = true;
52+
$json_arr = $platform->toJSON();
53+
54+
$audience = new Audience();
55+
$audience->tag = "tag1,tag2";
56+
$audience->tag_and = "tag3";
57+
$audience->alias = "alias1,alias2";
58+
$audience->registration_id = "id1,id2";
59+
$json_arr = $audience->toJSON();
60+
61+
62+
$message = new Message();
63+
$message->title = "message title";
64+
$message->content_type = "message content tpye";
65+
$message->msg_content = "message msg content";
66+
$message->extras = array("key1"=>"value1", "key2"=>"value2");
67+
$json_arr = $message->toJSON();
68+
69+
$payload = new PushPayload();
70+
//$payload->platform = $platform;
71+
//$payload->audience = $audience;
72+
$payload->message = $message;
73+
$payload->notification = $notification;
74+
$payload->options = $options;
75+
$json_arr = $payload->toJSON();
76+
77+
78+
$json_str = json_encode($json_arr);
79+
echo $json_str;

examples/example4.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
2-
include_once '../jpushv3/JPushClient.php';
3-
4-
include_once '../jpushv3/model/Audience.php';
5-
include_once '../jpushv3/model/Message.php';
6-
include_once '../jpushv3/model/notification/Notification.php';
7-
include_once '../jpushv3/model/notification/IOSNotification.php';
8-
include_once '../jpushv3/model/notification/AndroidNotification.php';
9-
include_once '../jpushv3/model/notification/WinphoneNotification.php';
10-
include_once '../jpushv3/model/Options.php';
11-
include_once '../jpushv3/model/Platform.php';
12-
include_once '../jpushv3/model/PushPayload.php';
2+
include_once '../jpush/JPushClient.php';
3+
4+
include_once '../jpush/model/Audience.php';
5+
include_once '../jpush/model/Message.php';
6+
include_once '../jpush/model/notification/Notification.php';
7+
include_once '../jpush/model/notification/IOSNotification.php';
8+
include_once '../jpush/model/notification/AndroidNotification.php';
9+
include_once '../jpush/model/notification/WinphoneNotification.php';
10+
include_once '../jpush/model/Options.php';
11+
include_once '../jpush/model/Platform.php';
12+
include_once '../jpush/model/PushPayload.php';
1313
/**
1414
* Created by PhpStorm.
1515
* User: xiezefan

jpush/NativeHttpClient.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Http请求类
4+
5+
*/
6+
class NativeHttpClient {
7+
8+
public function sendRequest($url, $content) {
9+
$data = @file_get_contents($url, false, $content);
10+
$rs = array("header"=>$http_response_header,"body"=>$data);
11+
return $rs;
12+
}
13+
14+
public function sendRequestByCurl($url, $data, $header, $method = 'POST') {
15+
$curl = curl_init(); // 启动一个CURL会话
16+
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
17+
if ($method === 'POST') {
18+
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
19+
}
20+
21+
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
22+
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
23+
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
24+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
25+
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
26+
$tmpInfo = curl_exec($curl); // 执行操作
27+
if (curl_errno($curl)) {
28+
//echo 'Errno'.curl_error($curl);//捕抓异常
29+
return json_encode(array("code"=>"400", "message"=>"Maybe connect error. Retry laster."));
30+
}
31+
curl_close($curl); // 关闭CURL会话
32+
return $tmpInfo; // 返回数据
33+
34+
}
35+
36+
function vpost($url,$data,$cookie){ // 模拟提交数据函数
37+
38+
}
39+
}
40+
41+
?>
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function validatePayload($payload, $result) {
4444

4545
//验证Message
4646
public function validateMessageParams($msg, $result) {
47-
if (is_null($msg->content) == false && is_string($msg->content) == false) {
47+
if (is_null($msg->msg_content) == false && is_string($msg->msg_content) == false) {
4848
$result->init(1003,"Parameters 'message->msg_content' must be a string");
4949
return false;
5050
}
@@ -135,7 +135,7 @@ public function validateNotificationParams($msg, $result) {
135135
}
136136

137137
//验证Notification参数合法性
138-
public function validateOptions($platform, $result) {
138+
public function validatePlatform($platform, $result) {
139139
if (is_bool($platform->ios) === false) {
140140
$result->init(1003,"Parameters 'platform->ios' must be bool");
141141
return false;
@@ -174,13 +174,13 @@ public function validateAudience($audience, $result) {
174174
}
175175

176176
//验证Options参数合法性
177-
public function valicateOptions($options, $result) {
177+
public function validateOptions($options, $result) {
178178
// validate options params
179-
if (is_int($options->time_to_live) === false || $options->time_to_live < 0 || $options->time_to_live > 864000) {
179+
if (is_null($options->time_to_live) == false && (is_int($options->time_to_live) === false || $options->time_to_live < 0 || $options->time_to_live > 864000)) {
180180
$result->init(1003,"Parameters 'options->timeToLive' must be a int and in [0, 864000]");
181181
return false;
182182
}
183-
if (is_bool($options->apns_production) === false) {
183+
if (is_null($options->time_to_live) == false && is_bool($options->apns_production) === false) {
184184
$result->init(1003,"Parameters 'options->apnsProduction' must be bool");
185185
return false;
186186
}

0 commit comments

Comments
 (0)