Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
googlemessage.php
1<?php
2
4
6{
8 public function __construct($sDeviceToken = null)
9 {
10 if (isset($sDeviceToken))
11 {
12 $this->addRecipient($sDeviceToken);
13 }
14 }
15
20 public function getBatch()
21 {
22 $data = $this->getPayload();
23 $batch = "Content-type: application/json\r\n";
24 $batch .= "Content-length: " . strlen($data) . "\r\n";
25 $batch .= "\r\n";
26 $batch .= $data;
27
28 return base64_encode($batch);
29 }
30
31 public function getPayload(): string
32 {
33 $data = [
34 "data" => [
35 'contentTitle' => $this->title,
36 "contentText" => $this->text,
37 "badge" => $this->badge,
38 "messageParams" => $this->customProperties,
39 "category" => $this->getCategory(),
40 "sound" => $this->getSound(),
41 ],
42 "time_to_live" => $this->expiryValue,
43 "priority" => "high",
44 "registration_ids" => $this->deviceTokens
45 ];
46
47 return $this->strippedPayload($data);
48 }
49
50 public function strippedPayload($data): string {
51 $jsonPayload = json_encode($data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE);
52 $payloadLength = strlen($jsonPayload);
53 if ($payloadLength > self::DEFAULT_PAYLOAD_MAXIMUM_SIZE)
54 {
56 $useSenderText = false;
57 if(array_key_exists("senderMessage", $this->customProperties))
58 {
59 $useSenderText = true;
60 $text = $this->customProperties["senderMessage"];
61 }
62 $maxTextLength = $nTextLen = strlen($text) - ($payloadLength - self::DEFAULT_PAYLOAD_MAXIMUM_SIZE);
63 if ($maxTextLength <= 0)
64 {
65 return false;
66 }
67 while (strlen($text = mb_substr($text, 0, --$nTextLen)) > $maxTextLength) ;
68 if($useSenderText)
69 {
70 $this->setCustomProperty("senderMessage", $text);
71 }
72 else
73 {
74 $this->setText($text);
75 }
76
77
78 return $this->getPayload();
79 }
80
81 return $jsonPayload;
82 }
83
84}