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\Service\Message; |
11
|
|
|
|
12
|
|
|
use Jgut\Tify\Recipient\ApnsRecipient; |
13
|
|
|
use Jgut\Tify\Notification\ApnsNotification; |
14
|
|
|
use ZendService\Apple\Apns\Message as ServiceMessage; |
15
|
|
|
use ZendService\Apple\Apns\Message\Alert as ServiceMessageAlert; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ApnsMessageBuilder |
19
|
|
|
*/ |
20
|
|
|
class ApnsMessageBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Get service message from origin. |
24
|
|
|
* |
25
|
|
|
* @param \Jgut\Tify\Recipient\ApnsRecipient $recipient |
26
|
|
|
* @param \Jgut\Tify\Notification\ApnsNotification $notification |
27
|
|
|
* |
28
|
|
|
* @throws \InvalidArgumentException |
29
|
|
|
* @throws \RuntimeException |
30
|
|
|
* |
31
|
|
|
* @return \ZendService\Apple\Apns\Message |
32
|
|
|
*/ |
33
|
|
|
public static function build(ApnsRecipient $recipient, ApnsNotification $notification) |
34
|
|
|
{ |
35
|
|
|
$message = $notification->getMessage(); |
36
|
|
|
|
37
|
|
|
$badge = ((int) $notification->getOption('badge', 0) === 0) |
38
|
|
|
? null |
39
|
|
|
: $notification->getOption('badge') + (int) $recipient->getParameter('badge', 0); |
40
|
|
|
|
41
|
|
|
$pushMessage = new ServiceMessage(); |
42
|
|
|
|
43
|
|
|
$pushMessage |
44
|
|
|
->setId(sha1($recipient->getToken() . $message->getOption('body'))) |
45
|
|
|
->setToken($recipient->getToken()) |
46
|
|
|
->setSound($notification->getOption('sound')) |
47
|
|
|
->setContentAvailable($notification->getOption('content_available')) |
48
|
|
|
->setCategory($notification->getOption('category')) |
49
|
|
|
->setCustom($message->getParameters()) |
50
|
|
|
->setBadge($badge); |
51
|
|
|
|
52
|
|
|
if ($notification->getOption('expire') !== null) { |
53
|
|
|
$pushMessage->setExpire($notification->getOption('expire')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($message->getOption('title') !== null || $message->getOption('body') !== null) { |
57
|
|
|
$pushMessage->setAlert(new ServiceMessageAlert( |
58
|
|
|
$message->getOption('body'), |
59
|
|
|
$message->getOption('action_loc_key'), |
60
|
|
|
$message->getOption('loc_key'), |
61
|
|
|
$message->getOption('loc_args'), |
62
|
|
|
$message->getOption('launch_image'), |
63
|
|
|
$message->getOption('title'), |
64
|
|
|
$message->getOption('title_loc_key'), |
65
|
|
|
$message->getOption('title_loc_args') |
66
|
|
|
)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $pushMessage; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|