public function testDataProperty() { $Data = new CustomPayloadData(['key1' => 'val1', 'key2' => 54]); $Message1 = new PayloadMessage(); $Message1->setRegistrationIds('Reg15TRaTi0N_1d1'); $Message1->setType(PayloadMessage::TYPE_PLAIN); $Message1->setData($Data); $this->assertEquals(['registration_id' => 'Reg15TRaTi0N_1d1', 'data.key1' => 'val1', 'data.key2' => 54, 'delay_while_idle' => false, 'dry_run' => false], $Message1->export()); $Message2 = new PayloadMessage(); $Message2->setType(PayloadMessage::TYPE_JSON); $Message2->setData($Data); $this->assertEquals(['delay_while_idle' => false, 'dry_run' => false, 'data' => ['key1' => 'val1', 'key2' => 54]], $Message2->export()); }
/** * Send GCM message method * @param PayloadMessage $Message GCM message instance * @return Response sent response instance * @throws GCMFormatException when request or response format was incorrect * @throws GCMUnauthorizedException when was incorrect authorization key * @throws GCMServerError when something wrong on the GCM server * @throws GCMUnrecoverableError when GCM server is not available */ public function send(PayloadMessage $Message) { switch ($Message->getType()) { case PayloadMessage::TYPE_PLAIN: $this->getRequest()->setContentTypeCode(Request::CONTENT_TYPE_UNDEFINED); $this->getRequest()->addHeader('Content-Type', self::CONTENT_TYPE_PLAIN_TEXT); break; case PayloadMessage::TYPE_JSON: $this->getRequest()->setContentTypeCode(Request::CONTENT_TYPE_JSON); $this->getRequest()->addHeader('Content-Type', self::CONTENT_TYPE_JSON); break; default: throw new GCMFormatException('unsupported request format code \'' . $Message->getType() . '\''); } $this->getRequest()->setPostData($Message->export()); try { $result = $this->getRequest()->send(); Response::$type = $Message->getType(); return Response::initializeByString($result); } catch (HttpClientErrorCodeException $Ex) { switch ($Ex->getCode()) { case '400': throw new GCMFormatException('invalid JSON request with message \'' . $Ex->getMessage() . '\''); case '401': throw new GCMUnauthorizedException('invalid authorization key \'' . $this->getAuthorizationKey() . '\''); default: throw $Ex; } } catch (HttpServerErrorCodeException $Ex) { switch ($Ex->getCode()) { case '500': throw new GCMUnrecoverableError('unrecoverable GCM server error'); default: $headers = $this->getRequest()->getResponseHeaders(); throw new GCMServerError(@$headers['Retry-After']); } } }