|
| 1 | +<?php |
| 2 | +namespace FirebaseBackend; |
| 3 | + |
| 4 | +require(dirname(__FILE__) . '/../config/config.php'); |
| 5 | + |
| 6 | +use FirebaseBackend\API; |
| 7 | +use Kreait\Firebase\Factory; |
| 8 | +use Kreait\Firebase\Messaging\CloudMessage; |
| 9 | +use Kreait\Firebase\Messaging\RawMessageFromArray; |
| 10 | + |
| 11 | +class CloudMessaging |
| 12 | +{ |
| 13 | + public static function sendMulticast() |
| 14 | + { |
| 15 | + // SET POST PARAMS |
| 16 | + // --------------- |
| 17 | + $postParams = API::getPostParams(); |
| 18 | + |
| 19 | + if ($postParams === null) { |
| 20 | + API::response(false, null, ['POST body must be a properly formatted JSON']); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + // SET API PARAMS |
| 25 | + // -------------- |
| 26 | + $deviceTokens = isset($postParams->deviceTokens) ? $postParams->deviceTokens : null; |
| 27 | + $notificationTitle = isset($postParams->title) ? $postParams->title : null; |
| 28 | + $notificationText = isset($postParams->text) ? $postParams->text : null; |
| 29 | + $notificationimageUrl = isset($postParams->imageUrl) ? $postParams->imageUrl : null; |
| 30 | + |
| 31 | + // VALIDATIONS |
| 32 | + // ----------- |
| 33 | + $errorMessages = []; |
| 34 | + |
| 35 | + // deviceTokens |
| 36 | + if (!$deviceTokens || count($deviceTokens) === 0) { |
| 37 | + $errorMessages['deviceTokens'] = 'At least one device token must be given'; |
| 38 | + } |
| 39 | + |
| 40 | + // notificationTitle |
| 41 | + if (!$notificationTitle) { |
| 42 | + $errorMessages['title'] = 'Notification title must be given'; |
| 43 | + } |
| 44 | + |
| 45 | + // RETURN ERROR RESPONSE |
| 46 | + // --------------------- |
| 47 | + if (count($errorMessages) > 0) { |
| 48 | + API::response(false, null, $errorMessages); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // SEND NOTIFICATION |
| 53 | + // ----------------- |
| 54 | + $factory = (new Factory)->withServiceAccount(FIREBASE_SERVICE_ACCOUNT_KEY); |
| 55 | + $messaging = $factory->createMessaging(); |
| 56 | + |
| 57 | + // $messageObject = CloudMessage::new(); |
| 58 | + // $message = $messageObject->withNotification([ |
| 59 | + // "title" => $notificationTitle, |
| 60 | + // "body" => $notificationText, |
| 61 | + // "image" => $notificationimageUrl, |
| 62 | + // ]); |
| 63 | + |
| 64 | + // API: https://firebase-php.readthedocs.io/en/5.9.0/cloud-messaging.html#sending-a-fully-configured-raw-message |
| 65 | + $message = new RawMessageFromArray([ |
| 66 | + "notification" => [ |
| 67 | + "title" => $notificationTitle, |
| 68 | + "body" => $notificationText, |
| 69 | + "image" => $notificationimageUrl, |
| 70 | + ], |
| 71 | + ]); |
| 72 | + $report = $messaging->sendMulticast($message, $deviceTokens); |
| 73 | + |
| 74 | + // Handle error |
| 75 | + $successCount = $report->successes()->count(); |
| 76 | + $errorCount = $report->failures()->count(); |
| 77 | + |
| 78 | + $errorMessages = []; |
| 79 | + if ($report->hasFailures()) { |
| 80 | + foreach ($report->failures()->getItems() as $failure) { |
| 81 | + $errorMessages[] = $failure->error()->getMessage(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (count($errorMessages) > 0) { |
| 86 | + API::response( |
| 87 | + false, |
| 88 | + null, |
| 89 | + $errorMessages |
| 90 | + ); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // SUCCESS RESPONSE |
| 95 | + API::response( |
| 96 | + true |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments