-
Notifications
You must be signed in to change notification settings - Fork 1
/
EJPush.php
88 lines (73 loc) · 2.06 KB
/
EJPush.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
<?php
/**
* Created by PhpStorm.
* User: leoliang
* Date: 14-6-25
* Time: 上午12:01
*/
class EJPush extends CApplicationComponent
{
const PLATFORM_IOS = 'ios';
const PLATFORM_ANDROID = 'android';
const PLATFORM_ALL = 'ios,android';
public $key;
public $secret;
public $mode;
public $clientPath = 'application.vendor.jpush.JPushClient';
private $last_error;
public function init()
{
Yii::import($this->clientPath);
parent::init();
}
private function getClient($platform = self::PLATFORM_ALL)
{
static $client = null;
if ($client === null) {
$client = new JPushClient($this->key, $this->secret, 864000, $platform, $this->mode ? true : false); //10天
}
return $client;
}
private function send($params, EJPushMessage $msg)
{
$client = self::getClient();
$params = array_merge(array(
'receiver_type' => 1,
'receiver_value' => '',
'sendno' => $msg->getId(),
'send_description' => '',
'override_msg_id' => ''
), $params);
$this->last_error = $client->sendNotification($msg->getContent(), $params, $msg->getExtras());
return $this->last_error->getCode() == 0;
}
public function getLastErrors()
{
return $this->last_error;
}
public function sendByTag($tag, EJPushMessage $msg)
{
if (is_array($tag)) $tag = join(',', $tag);
$params = array(
'receiver_type' => 2,
'receiver_value' => $tag,
);
return self::send($params, $msg);
}
public function sendByAlice($alice, EJPushMessage $msg)
{
if (is_array($alice)) $alice = join(',', $alice);
$params = array(
'receiver_type' => 3,
'receiver_value' => $alice,
);
return self::send($params, $msg);
}
public function sendAll(EJPushMessage $msg)
{
$params = array(
'receiver_type' => 4
);
return self::send($params, $msg);
}
}