Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 79 |
| FCMNotificationService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 5 |
306.00 | |
0.00% |
0 / 79 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| __clone | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| getInstance | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
| sendFCMNotificationToSingleUser | |
0.00% |
0 / 1 |
156.00 | |
0.00% |
0 / 65 |
|||
| sendPushNotification | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 5 |
|||
| <?php | |
| /** | |
| * User: jithinvijayan | |
| * Date: 15/03/18 | |
| * Time: 3:23 PM | |
| */ | |
| namespace com\linways\core\ams\professional\service\notification; | |
| use com\linways\core\ams\professional\dto\mobile\MobileLoginUser; | |
| use com\linways\core\ams\professional\dto\notification\FCMNotification; | |
| use com\linways\core\ams\professional\exception\mobile\MobileException; | |
| use com\linways\core\ams\professional\service\BaseService; | |
| use com\linways\core\ams\professional\service\mobile\MobileUserService; | |
| use Unirest; | |
| class FCMNotificationService extends BaseService | |
| { | |
| private static $_instance = null; | |
| // /Condition 2 - Locked down the constructor | |
| private function __construct() | |
| { | |
| } | |
| // Prevent any oustide instantiation of this class | |
| // /Condition 3 - Prevent any object or instance of that class to be cloned | |
| private function __clone() | |
| { | |
| } | |
| // Prevent any copy of this object | |
| // /Condition 4 - Have a single globally accessible static method | |
| public static function getInstance() | |
| { | |
| if (!is_object(self::$_instance)) // or if( is_null(self::$_instance) ) or if( self::$_instance == null ) | |
| self::$_instance = new self(); | |
| return self::$_instance; | |
| } | |
| /** | |
| * create notification structure and fetching fcm tokens from database | |
| * @param FCMNotification $fcmNotification | |
| * @return \stdClass | |
| * @throws MobileException | |
| */ | |
| public function sendFCMNotificationToSingleUser(FCMNotification $fcmNotification) | |
| { | |
| $response = new \stdClass(); | |
| $failed = 0; | |
| $devices = 0; | |
| $status = false; | |
| $fcmNotification = $this->realEscapeObject($fcmNotification); | |
| $userDetails = $fcmNotification->mobileUser; | |
| if (empty($userDetails->userId)) { | |
| throw new MobileException("Invalid User Id", MobileException::INVALID_USER_ID); | |
| } | |
| if (empty($userDetails->userType)) { | |
| throw new MobileException("Invalid user type", MobileException::INVALID_USER_TYPE); | |
| } | |
| try { | |
| $fcmTokens = MobileUserService::getInstance()->getFCMTokenByUserIDandUserType($userDetails->userId, $userDetails->userType); | |
| if (!empty($fcmTokens)) { | |
| $nucleusConfigPath = getenv('NUCLEUS_CONF'); | |
| if (empty($nucleusConfigPath)) { | |
| throw new MobileException("Nucleus configuration missing", MobileException::NUCLEUS_CONFIGURATIONS_MISSING); | |
| } | |
| require_once $nucleusConfigPath; | |
| global $FCM_SERVER_KEY; | |
| if (empty($FCM_SERVER_KEY)) { | |
| throw new MobileException("FCM Authorization key missing", MobileException::INVALID_AUTHORIZATION_KEY); | |
| } | |
| $authorizationKey = $FCM_SERVER_KEY; | |
| $mainContent = array( | |
| "notificationId" => $fcmNotification->id, | |
| "message" => $fcmNotification->body, | |
| "title" => $fcmNotification->title, | |
| "sound" => "default", | |
| "notificationURL" => $fcmNotification->url, | |
| "channelId" => $fcmNotification->channelId, | |
| "label" => $fcmNotification->label, | |
| "category" => $fcmNotification->category, | |
| "priority" => $fcmNotification->priority, | |
| "groupKey" => $fcmNotification->groupKey | |
| ); | |
| $headers = array(); | |
| $headers["Content-Type"] = 'application/json'; | |
| $headers["Authorization"] = 'key=' . $authorizationKey; | |
| $devices = sizeof($fcmTokens); | |
| $postData = array(); | |
| $registrationIds = []; | |
| foreach ($fcmTokens as $fcmToken) { | |
| array_push($registrationIds, $fcmToken->fcm_token); | |
| } | |
| $postData['registration_ids'] = $registrationIds; | |
| $postData['data'] = $mainContent; | |
| $unirestResponse = $this->sendPushNotification($fcmNotification->fcmSendingURL, $postData, $headers); | |
| if ($unirestResponse->code != 200 || $unirestResponse->body->failure) | |
| ++$failed; | |
| $status = true; | |
| } | |
| } catch (\Exception $e) { | |
| throw new MobileException($e->getMessage(), $e->getCode()); | |
| } | |
| if ($failed == $devices || !$status) { | |
| $response->status = "failed"; | |
| } else { | |
| $response->status = "success"; | |
| $response->failed = $failed; | |
| } | |
| return $response; | |
| } | |
| /** | |
| * Send notification to google cloud messaging system | |
| * @param string $url url for notification to be send | |
| * @param array $data notification data part | |
| * @param array $header notification header part | |
| * @return Unirest\Response response code unirest response | |
| * @throws Unirest\Exception | |
| */ | |
| private function sendPushNotification($url, $data, $header) | |
| { | |
| $postData = Unirest\Request\Body::Json($data); | |
| $response = Unirest\Request::post($url, $header, $postData); | |
| return $response; | |
| } | |
| } |