|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Firebase\Push; |
| 4 | + |
| 5 | +class Push |
| 6 | +{ |
| 7 | + private $title; |
| 8 | + private $message; |
| 9 | + private $to; |
| 10 | + private $deeplink = null; |
| 11 | + private $payload = []; |
| 12 | + |
| 13 | + /** |
| 14 | + * @param $title |
| 15 | + */ |
| 16 | + public function setTitle(string $title) |
| 17 | + { |
| 18 | + $this->title = $title; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @param $message |
| 23 | + */ |
| 24 | + public function setMessage(string $message) |
| 25 | + { |
| 26 | + $this->message = $message; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param array $payload |
| 31 | + */ |
| 32 | + public function setPayload(array $payload) |
| 33 | + { |
| 34 | + $this->payload = $payload; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param string $to |
| 39 | + */ |
| 40 | + public function setTo(string $to) |
| 41 | + { |
| 42 | + $this->to = $to; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param string|null $deeplink |
| 47 | + */ |
| 48 | + public function setDeeplink(?string $deeplink) |
| 49 | + { |
| 50 | + $this->deeplink = $deeplink; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return string |
| 55 | + */ |
| 56 | + public function send(): string |
| 57 | + { |
| 58 | + // |
| 59 | + $request = array(); |
| 60 | + // |
| 61 | + $request['notification'] = $this->makeNotification(); |
| 62 | + $request['data'] = $this->makeData(); |
| 63 | + $request['apns']['payload']['aps'] = $this->makeApns(); |
| 64 | + // |
| 65 | + $request['content_available'] = true; |
| 66 | + $request['priority'] = 5; |
| 67 | + // |
| 68 | + if (count($this->payload) > 0) { |
| 69 | + $request['registration_ids'] = $this->payload; |
| 70 | + } else { |
| 71 | + $request['to'] = $this->to; |
| 72 | + } |
| 73 | + // |
| 74 | + return $this->makeSend($request); |
| 75 | + } |
| 76 | + |
| 77 | + private function makeSend($request): string |
| 78 | + { |
| 79 | + $headers = [ |
| 80 | + 'Authorization: key=' . env('FCM_SERVER_KEY'), |
| 81 | + 'Content-Type: application/json', |
| 82 | + ]; |
| 83 | + |
| 84 | + $ch = curl_init(); |
| 85 | + |
| 86 | + curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); |
| 87 | + curl_setopt($ch, CURLOPT_POST, true); |
| 88 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 89 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 90 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 91 | + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request)); |
| 92 | + return curl_exec($ch); |
| 93 | + } |
| 94 | + |
| 95 | + private function makeNotification(): array |
| 96 | + { |
| 97 | + $notification = array(); |
| 98 | + if ($this->title !== null) { |
| 99 | + $notification['title'] = $this->title; |
| 100 | + } |
| 101 | + |
| 102 | + if ($this->message !== null) { |
| 103 | + $notification['body'] = $this->message; |
| 104 | + } |
| 105 | + |
| 106 | + if ($this->deeplink !== null) { |
| 107 | + $notification['deeplink'] = $this->deeplink; |
| 108 | + } |
| 109 | + |
| 110 | + $notification['badge'] = 1; |
| 111 | + $notification['priority'] = 'high'; |
| 112 | + $notification['mutable_content'] = true; |
| 113 | + return $notification; |
| 114 | + } |
| 115 | + |
| 116 | + private function makeData(): array |
| 117 | + { |
| 118 | + $data = array(); |
| 119 | + if ($this->title !== null) { |
| 120 | + $data['title'] = $this->title; |
| 121 | + } |
| 122 | + |
| 123 | + if ($this->message !== null) { |
| 124 | + $data['body'] = $this->message; |
| 125 | + } |
| 126 | + |
| 127 | + if ($this->deeplink !== null) { |
| 128 | + $data['deeplink'] = $this->deeplink; |
| 129 | + } |
| 130 | + |
| 131 | + $data['timestamp'] = date('Y-m-d G:i:s'); |
| 132 | + return $data; |
| 133 | + } |
| 134 | + |
| 135 | + private function makeApns(): array |
| 136 | + { |
| 137 | + $apns = array(); |
| 138 | + if ($this->deeplink !== null) { |
| 139 | + $apns['mutableContent'] = true; |
| 140 | + $apns['deeplink'] = $this->deeplink; |
| 141 | + } |
| 142 | + return $apns; |
| 143 | + } |
| 144 | +} |
0 commit comments