1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPHub\Services\PushService; |
4
|
|
|
|
5
|
|
|
use Config; |
6
|
|
|
use JPush\JPushClient; |
7
|
|
|
|
8
|
|
|
class JPush |
9
|
|
|
{ |
10
|
|
|
private $client; |
11
|
|
|
private $payload; |
12
|
|
|
private $extras; |
13
|
|
|
private $notification; |
14
|
|
|
private $sound = 'default'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* 初始化. |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$app_key = config('services.jpush.app_key'); |
22
|
|
|
$master_secret = config('services.jpush.secret'); |
23
|
|
|
|
24
|
|
|
$this->client = new JPushClient($app_key, $master_secret); |
25
|
|
|
$this->payload = $this->client->push(); |
26
|
|
|
|
27
|
|
|
$this->payload->setOptions([ |
28
|
|
|
'apns_production' => config('services.jpush.apns_production'), |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 指定推送的平台. |
34
|
|
|
* |
35
|
|
|
* @param $platform |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function platform($platform) |
40
|
|
|
{ |
41
|
|
|
if (! in_array($platform, ['ios', 'android', 'winphone', 'all'])) { |
42
|
|
|
throw new \InvalidArgumentException('Invalid device type: '.$platform); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->payload->setPlatform($platform); |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 推送消息. |
52
|
|
|
* |
53
|
|
|
* @param $msg |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function message($msg) |
58
|
|
|
{ |
59
|
|
|
$this->notification = $msg; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* 通过别名推送. |
66
|
|
|
* |
67
|
|
|
* @param array $alias |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function toAlias($alias) |
72
|
|
|
{ |
73
|
|
|
$alias = (array) $alias; |
74
|
|
|
$this->payload->setAudience(['alias' => $alias]); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 通知全部用户. |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function toAll() |
85
|
|
|
{ |
86
|
|
|
$this->payload->setAudience('all'); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* 开始发布. |
93
|
|
|
*/ |
94
|
|
|
public function send() |
95
|
|
|
{ |
96
|
|
|
$this->payload->setNotification([ |
97
|
|
|
'ios' => [ |
98
|
|
|
'alert' => $this->notification, |
99
|
|
|
'extras' => $this->extras, |
100
|
|
|
'sound' => $this->sound, |
101
|
|
|
], |
102
|
|
|
'android' => [ |
103
|
|
|
'alert' => $this->notification, |
104
|
|
|
'extras' => $this->extras, |
105
|
|
|
], |
106
|
|
|
]); |
107
|
|
|
|
108
|
|
|
$this->payload->send(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* 附带信息. |
113
|
|
|
* |
114
|
|
|
* @param array $extras |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function extras(array $extras) |
119
|
|
|
{ |
120
|
|
|
$this->extras = $extras; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|