|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WZRD\Push; |
|
4
|
|
|
|
|
5
|
|
|
use WZRD\Contracts\Push\Notification as NotificationContract; |
|
6
|
|
|
use WZRD\Contracts\Push\Pusher; |
|
7
|
|
|
use Zend\Http\Client as Http; |
|
8
|
|
|
use ZendService\Google\Gcm\Client; |
|
9
|
|
|
use ZendService\Google\Gcm\Message; |
|
10
|
|
|
|
|
11
|
|
|
class ZendGcmPusher implements Pusher |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Zend GCM. |
|
15
|
|
|
* |
|
16
|
|
|
* @var ZendService\Google\Gcm\Client |
|
17
|
|
|
*/ |
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Construct with Zend GCM Client instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @param ZendService\Google\Gcm\Client $client |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Client $client) |
|
26
|
|
|
{ |
|
27
|
|
|
$client->setHttpClient(new Http(null, [ |
|
28
|
|
|
'adapter' => 'Zend\Http\Client\Adapter\Socket', |
|
29
|
|
|
'sslverifypeer' => false, |
|
30
|
|
|
])); |
|
31
|
|
|
|
|
32
|
|
|
$this->client = $client; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Push message. |
|
37
|
|
|
* |
|
38
|
|
|
* @param WZRD\Contracts\Push\Notification $notification |
|
39
|
|
|
* @param array $options |
|
40
|
|
|
*/ |
|
41
|
|
|
public function push(NotificationContract $notification, array $options = []) |
|
42
|
|
|
{ |
|
43
|
|
|
// Check the supported platforms |
|
44
|
|
|
if (count(array_intersect($notification->getTargetedPlatforms(), $this->getSupportedPlatforms())) == 0) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Platforms & devices options |
|
49
|
|
|
$devices = $platforms_options = []; |
|
50
|
|
View Code Duplication |
foreach ($notification->getDevices() as $platform => $data) { |
|
|
|
|
|
|
51
|
|
|
if (in_array($platform, $this->getSupportedPlatforms())) { |
|
52
|
|
|
$platforms_options = array_merge($platforms_options, $data['options']); |
|
53
|
|
|
$devices = array_merge($devices, $data['devices']); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Prepare message |
|
58
|
|
|
$message = new Message(); |
|
59
|
|
|
$message->setRegistrationIds($devices); |
|
60
|
|
|
$message->setData(array_merge($notification->getData(), [ |
|
61
|
|
|
'message' => $notification->getMessage(), |
|
62
|
|
|
])); |
|
63
|
|
|
|
|
64
|
|
|
if (!empty($platforms_options['collapse_key'])) { |
|
65
|
|
|
$message->setCollapseKey($platforms_options['collapse_key']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!empty($platforms_options['restricted_package_name'])) { |
|
69
|
|
|
$message->setRestrictedPackageName($platforms_options['restricted_package_name']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (!empty($platforms_options['delay_while_idle'])) { |
|
73
|
|
|
$message->setDelayWhileIdle(true); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (!empty($platforms_options['ttl'])) { |
|
77
|
|
|
$message->setTimeToLive($platforms_options['ttl']); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!empty($platforms_options['dry_run'])) { |
|
81
|
|
|
$message->setDryRun(true); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->client->send($message); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get supported platforms. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getSupportedPlatforms() |
|
93
|
|
|
{ |
|
94
|
|
|
return ['android']; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..