|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Push notification services abstraction (http://github.com/juliangut/tify) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/juliangut/tify for the canonical source repository |
|
6
|
|
|
* |
|
7
|
|
|
* @license https://github.com/juliangut/tify/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Jgut\Tify\Adapter\Gcm; |
|
11
|
|
|
|
|
12
|
|
|
use Jgut\Tify\Notification; |
|
13
|
|
|
use Zend\Http\Client\Adapter\Socket; |
|
14
|
|
|
use Zend\Http\Client as HttpClient; |
|
15
|
|
|
use ZendService\Google\Gcm\Client; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class GcmBuilder |
|
19
|
|
|
*/ |
|
20
|
|
|
class GcmBuilder |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Get opened push service client. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $apiKey |
|
26
|
|
|
* |
|
27
|
|
|
* @return \ZendService\Google\Gcm\Client |
|
28
|
|
|
*/ |
|
29
|
|
|
public function buildPushClient($apiKey) |
|
30
|
|
|
{ |
|
31
|
|
|
$client = new Client; |
|
32
|
|
|
$client->setApiKey($apiKey); |
|
33
|
|
|
|
|
34
|
|
|
$httpClient = new HttpClient( |
|
35
|
|
|
null, |
|
36
|
|
|
[ |
|
37
|
|
|
'service' => Socket::class, |
|
38
|
|
|
'strictredirects' => true, |
|
39
|
|
|
'sslverifypeer' => false, |
|
40
|
|
|
] |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$client->setHttpClient($httpClient); |
|
44
|
|
|
|
|
45
|
|
|
return $client; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get configured service message. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $tokens |
|
52
|
|
|
* @param \Jgut\Tify\Notification $notification |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \InvalidArgumentException |
|
55
|
|
|
* @throws \RuntimeException |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Jgut\Tify\Adapter\Gcm\GcmMessage |
|
58
|
|
|
*/ |
|
59
|
|
|
public function buildPushMessage(array $tokens, Notification $notification) |
|
60
|
|
|
{ |
|
61
|
|
|
$message = $notification->getMessage(); |
|
62
|
|
|
|
|
63
|
|
|
$pushMessage = new GcmMessage(); |
|
64
|
|
|
|
|
65
|
|
|
$pushMessage |
|
66
|
|
|
->setRegistrationIds($tokens) |
|
67
|
|
|
->setCollapseKey($notification->getParameter('collapse_key')) |
|
68
|
|
|
->setDelayWhileIdle($notification->getParameter('delay_while_idle')) |
|
69
|
|
|
->setTimeToLive($notification->getParameter('time_to_live')) |
|
70
|
|
|
->setRestrictedPackageName($notification->getParameter('restricted_package_name')) |
|
71
|
|
|
->setDryRun($notification->getParameter('dry_run')) |
|
72
|
|
|
->setData($message->getPayloadData()); |
|
73
|
|
|
|
|
74
|
|
|
if ($message->getParameter('title') !== null || $message->getParameter('body') !== null |
|
75
|
|
|
|| $message->getParameter('title_loc_key') !== null || $message->getParameter('body_loc_key') !== null |
|
76
|
|
|
) { |
|
77
|
|
|
$pushMessage->setNotificationPayload($message->getParameters()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $pushMessage; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|