1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Unified push notification services abstraction (http://github.com/juliangut/tify). |
5
|
|
|
* |
6
|
|
|
* @license BSD-3-Clause |
7
|
|
|
* @link https://github.com/juliangut/tify |
8
|
|
|
* @author Julián Gutiérrez <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Jgut\Tify\Adapter\Gcm; |
12
|
|
|
|
13
|
|
|
use Jgut\Tify\Message as NotificationMessage; |
14
|
|
|
use Jgut\Tify\Notification; |
15
|
|
|
use Zend\Http\Client\Adapter\Socket; |
16
|
|
|
use Zend\Http\Client as HttpClient; |
17
|
|
|
use ZendService\Google\Gcm\Client as PushClient; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* GCM default service factory. |
21
|
|
|
*/ |
22
|
|
|
class DefaultFactory implements Factory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Notification parameters list. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected static $notificationParams = [ |
30
|
|
|
NotificationMessage::PARAMETER_TITLE, |
31
|
|
|
NotificationMessage::PARAMETER_BODY, |
32
|
|
|
NotificationMessage::PARAMETER_ICON, |
33
|
|
|
NotificationMessage::PARAMETER_SOUND, |
34
|
|
|
NotificationMessage::PARAMETER_TAG, |
35
|
|
|
NotificationMessage::PARAMETER_COLOR, |
36
|
|
|
NotificationMessage::PARAMETER_CLICK_ACTION, |
37
|
|
|
NotificationMessage::PARAMETER_TITLE_LOC_KEY, |
38
|
|
|
NotificationMessage::PARAMETER_TITLE_LOC_ARGS, |
39
|
|
|
NotificationMessage::PARAMETER_BODY_LOC_KEY, |
40
|
|
|
NotificationMessage::PARAMETER_BODY_LOC_ARGS, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function buildPushClient($apiKey) |
47
|
|
|
{ |
48
|
|
|
$client = new PushClient; |
49
|
|
|
$client->setApiKey($apiKey); |
50
|
|
|
|
51
|
|
|
$httpClient = new HttpClient( |
52
|
|
|
null, |
53
|
|
|
[ |
54
|
|
|
'service' => Socket::class, |
55
|
|
|
'strictredirects' => true, |
56
|
|
|
'sslverifypeer' => false, |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$client->setHttpClient($httpClient); |
61
|
|
|
|
62
|
|
|
return $client; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
* |
68
|
|
|
* @throws \ZendService\Google\Exception\InvalidArgumentException |
69
|
|
|
* @throws \ZendService\Google\Exception\RuntimeException |
70
|
|
|
*/ |
71
|
|
|
public function buildPushMessage(array $tokens, Notification $notification) |
72
|
|
|
{ |
73
|
|
|
$message = $notification->getMessage(); |
74
|
|
|
|
75
|
|
|
$pushMessage = new Message; |
76
|
|
|
$pushMessage |
77
|
|
|
->setRegistrationIds($tokens) |
78
|
|
|
->setCollapseKey($notification->getParameter(Notification::PARAMETER_COLLAPSE_KEY)) |
79
|
|
|
->setDelayWhileIdle($notification->getParameter(Notification::PARAMETER_DELAY_WHILE_IDLE)) |
80
|
|
|
->setTimeToLive($notification->getParameter(Notification::PARAMETER_TTL)) |
81
|
|
|
->setRestrictedPackageName($notification->getParameter(Notification::PARAMETER_RESTRICTED_PACKAGE_NAME)) |
82
|
|
|
->setDryRun($notification->getParameter(Notification::PARAMETER_DRY_RUN)) |
83
|
|
|
->setData($message->getPayloadData()); |
84
|
|
|
|
85
|
|
|
if ($this->shouldHavePayload($message)) { |
86
|
|
|
$pushMessage->setNotificationPayload($message->getParameters()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $pushMessage; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Message should have notification data. |
94
|
|
|
* |
95
|
|
|
* @param NotificationMessage $message |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
private function shouldHavePayload(NotificationMessage $message) |
100
|
|
|
{ |
101
|
|
|
$shouldHavePayload = false; |
102
|
|
|
|
103
|
|
|
foreach (static::$notificationParams as $parameter) { |
104
|
|
|
if ($message->hasParameter($parameter)) { |
105
|
|
|
$shouldHavePayload = true; |
106
|
|
|
|
107
|
|
|
break; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $shouldHavePayload; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|