-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathpush_example.php
140 lines (123 loc) · 5.04 KB
/
push_example.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
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
129
130
131
132
133
134
135
136
137
138
139
140
<?php
// 这只是使用样例,不应该直接用于实际生产环境中 !!
require __DIR__ . '/../autoload.php';
use JPush\Client as JPush;
// 这里填写appKey,masterSecret以及registration_id
$app_key = 'e5c0d34f58732cf09b2d4d74';
$master_secret = '4cdda6d3c8b029941dbc5cb3';
$registration_id = ('registration_id');
$client = new JPush($app_key, $master_secret);
// 简单推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
// $push_payload = $client->push()
// ->setPlatform('all')
// ->addAllAudience()
// ->setNotificationAlert('Hi, JPush');
// try {
// $response = $push_payload->send();
// print_r($response);
// } catch (\JPush\Exceptions\APIConnectionException $e) {
// // try something here
// print $e;
// } catch (\JPush\Exceptions\APIRequestException $e) {
// // try something here
// print $e;
// }
// 完整的推送示例
// 这只是使用样例,不应该直接用于实际生产环境中 !!
try {
$response = $client->push()
->setPlatform(array('ios', 'android'))
// 一般情况下,关于 audience 的设置只需要调用 addAlias、addTag、addTagAnd 或 addRegistrationId
// 这四个方法中的某一个即可,这里仅作为示例,当然全部调用也可以,多项 audience 调用表示其结果的交集
// 即是说一般情况下,下面三个方法和没有列出的 addTagAnd 一共四个,只适用一个便可满足大多数的场景需求
// ->addAlias('alias')
// ->addTag(array('tag1', 'tag2'))
// ->addRegistrationId($registration_id)
->addAllAudience()
->setNotificationAlert('Test custom')
->iosNotification('Hello IOS', array(
'sound' => 'sound.caf',
// 'badge' => '+1',
// 'content-available' => true,
// 'mutable-content' => true,
'category' => 'jiguang',
'extras' => array(
'key' => 'value',
'jiguang'
),
))
->androidNotification('Hello Android', array(
'title' => 'hello jpush',
// 'builder_id' => 2,
'extras' => array(
'key' => 'value',
'jiguang'
),
))
// voip可以传输任意键值对,可用作自定义
->voip(array(
'test123' => 'val1',
'jsontest' => 2,
'booleantest' => true
))
->message('message content', array(
'title' => 'hello jpush',
// 'content_type' => 'text',
'extras' => array(
'key' => 'value',
'jiguang'
),
))
->options(array(
// sendno: 表示推送序号,纯粹用来作为 API 调用标识,
// API 返回时被原样返回,以方便 API 调用方匹配请求与返回
// 这里设置为 100 仅作为示例
// 'sendno' => 100,
// time_to_live: 表示离线消息保留时长(秒),
// 推送当前用户不在线时,为该用户保留多长时间的离线消息,以便其上线时再次推送。
// 默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到
// 这里设置为 1 仅作为示例
'time_to_live' => 1,
// apns_production: 表示APNs是否生产环境,
// True 表示推送生产环境,False 表示要推送开发环境;如果不指定则默认为推送开发环境
// 'apns_production' => false,
// big_push_duration: 表示定速推送时长(分钟),又名缓慢推送,把原本尽可能快的推送速度,降低下来,
// 给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为1400.未设置则不是定速推送
// 这里设置为 1 仅作为示例
// 'big_push_duration' => 1
))
->setSmsMessage(array(
'delay_time' => 60,
'signid' => 154,
'temp_id' => 1,
'temp_para' => array(
'code' => 357
),
'active_filter' => false
))
// custom可自定义最外层参数,如skd未支持部分文档功能,用户可自行写入
// 这里仅作为例子展示
// ->custom(array(
// 'sms_message' => array(
// 'active_filter' => false,
// 'delay_time' => 60,
// 'signid' => 154,
// 'temp_id' => 1,
// 'temp_para' => array(
// 'code' => 357
// )),
// 'options' => array(
// 'apns_production' => false,
// 'time_to_live' => 62000,
// )
// ))
->send();
print_r($response);
} catch (\JPush\Exceptions\APIConnectionException $e) {
// try something here
print $e;
} catch (\JPush\Exceptions\APIRequestException $e) {
// try something here
print $e;
}