forked from bryglen/yii2-apns-gcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApnsGcm.php
118 lines (99 loc) · 2.74 KB
/
ApnsGcm.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
<?php
/**
* @author Bryan Jayson Tan <[email protected]>
* @link http://bryantan.info
*/
namespace bryglen\apnsgcm;
use yii\base\Component;
class ApnsGcm extends Component
{
const TYPE_GCM = 'gcm';
const TYPE_APNS = 'apns';
/**
* component name for apns
*
* @var string
*/
public $apns = 'apns';
/**
* component name for gcm
*
* @var string
*/
public $gcm = 'gcm';
public $errors = [];
public $success = false;
private $_gcmClient;
private $_apnsClient;
/**
* @return Gcm
*/
public function getGcmClient()
{
if ($this->_gcmClient === null) {
$component = $this->gcm;
$client = \Yii::$app->get($component);
$this->_gcmClient = $client;
}
return $this->_gcmClient;
}
/**
* @return Apns
*/
public function getApnsClient()
{
if ($this->_apnsClient === null) {
$component = $this->apns;
$client = \Yii::$app->get($component);
$this->_apnsClient = $client;
}
return $this->_apnsClient;
}
/**
* send a push notification depending on type
* @param $type
* @param $token
* @param $text
* @param array $payloadData
* @param array $args
* @return null|\PHP_GCM\Message
*/
public function send($type, $token, $text, $payloadData = [], $args = [])
{
$client = null;
$result = null;
$this->errors = [];
if ($type == self::TYPE_GCM) {
$client = $this->getGcmClient();
$result = $client->send($token, $text, $payloadData, $args);
$this->success = $client->success;
} elseif ($type == self::TYPE_APNS) {
$client = $this->getApnsClient();
$result = $client->send($token, $text, $payloadData, $args);
$this->success = $client->success;
}
if (!$this->success) {
$this->errors = $client->errors;
}
return $result;
}
public function sendMulti($type, $tokens, $text, $payloadData = [], $args = [])
{
$client = null;
$result = null;
$this->errors = [];
if ($type == self::TYPE_GCM) {
$client = $this->getGcmClient();
$result = $client->sendMulti($tokens, $text, $payloadData, $args);
$this->success = $client->success;
} elseif ($type == self::TYPE_APNS) {
$client = $this->getApnsClient();
$result = $client->sendMulti($tokens, $text, $payloadData, $args);
$this->success = $client->success;
}
if (!$this->success) {
$this->errors = $client->errors;
}
return $result;
}
}