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\Fcm; |
12
|
|
|
|
13
|
|
|
use Jgut\Tify\Message; |
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
|
|
|
use ZendService\Google\Gcm\Message as ServiceMessage; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* FCM default service factory. |
22
|
|
|
*/ |
23
|
|
|
class DefaultFactory implements Factory |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Notification parameters list. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected static $notificationParams = [ |
31
|
|
|
Message::PARAMETER_TITLE, |
32
|
|
|
Message::PARAMETER_BODY, |
33
|
|
|
Message::PARAMETER_ICON, |
34
|
|
|
Message::PARAMETER_SOUND, |
35
|
|
|
Message::PARAMETER_TAG, |
36
|
|
|
Message::PARAMETER_COLOR, |
37
|
|
|
Message::PARAMETER_CLICK_ACTION, |
38
|
|
|
Message::PARAMETER_TITLE_LOC_KEY, |
39
|
|
|
Message::PARAMETER_TITLE_LOC_ARGS, |
40
|
|
|
Message::PARAMETER_BODY_LOC_KEY, |
41
|
|
|
Message::PARAMETER_BODY_LOC_ARGS, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function buildPushClient($apiKey) |
48
|
|
|
{ |
49
|
|
|
$client = new PushClient; |
50
|
|
|
$client->setApiKey($apiKey); |
51
|
|
|
|
52
|
|
|
$httpClient = new HttpClient( |
53
|
|
|
null, |
54
|
|
|
[ |
55
|
|
|
'service' => Socket::class, |
56
|
|
|
'strictredirects' => true, |
57
|
|
|
'sslverifypeer' => false, |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$client->setHttpClient($httpClient); |
62
|
|
|
|
63
|
|
|
return $client; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
* |
69
|
|
|
* @throws \ZendService\Google\Exception\InvalidArgumentException |
70
|
|
|
* @throws \ZendService\Google\Exception\RuntimeException |
71
|
|
|
*/ |
72
|
|
|
public function buildPushMessage(array $tokens, Notification $notification) |
73
|
|
|
{ |
74
|
|
|
$message = $notification->getMessage(); |
75
|
|
|
|
76
|
|
|
$pushMessage = (new ServiceMessage) |
77
|
|
|
->setRegistrationIds($tokens) |
78
|
|
|
->setPriority($notification->getParameter(Notification::PARAMETER_PRIORITY)) |
79
|
|
|
->setCollapseKey($notification->getParameter(Notification::PARAMETER_COLLAPSE_KEY)) |
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->shouldAddNotification($message)) { |
86
|
|
|
$pushMessage->setNotification($message->getParameters()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $pushMessage; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Message should have notification data. |
94
|
|
|
* |
95
|
|
|
* @param Message $message |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
private function shouldAddNotification(Message $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
|
|
|
|