private function addEndpoint(Model\AbstractEndpoint $endpoint) { try { $result = $this->sns->createPlatformEndpoint(['PlatformApplicationArn' => $this->getPlatformArn($endpoint), 'Token' => $endpoint->getToken(), 'CustomUserData' => $endpoint->getUser()->getId()]); } catch (\Aws\Sns\Exception\InvalidParameterException $e) { if (preg_match('/Endpoint (.*) already exists/', $e->getResponse()->getMessage(), $matches)) { $this->sns->deleteEndpoint(array('EndpointArn' => $matches[1])); $result = $this->sns->createPlatformEndpoint(['PlatformApplicationArn' => $this->getPlatformArn($endpoint), 'Token' => $endpoint->getToken(), 'CustomUserData' => $endpoint->getUser()->getId()]); } else { return; } } $endpoint->setArn($result['EndpointArn']); $this->em->persist($endpoint); $this->em->flush($endpoint); }
/** * @param $token * @param PushMessage $message * * @return mixed */ private function sendOne($token, $message) { $client = new SnsClient(['credentials' => new Credentials($this->accessKey, $this->secretKey), 'region' => $this->region, 'version' => 'latest']); $endpoint = $client->createPlatformEndpoint(['PlatformApplicationArn' => $this->appArn, 'Token' => $token]); $published = $client->publish(['MessageStructure' => 'json', 'Message' => Json::encode(['default' => $message->getBody(), $message->getService() => $this->formatMessage($message)]), 'TargetArn' => $endpoint['EndpointArn']]); return $published['MessageId']; }
/** * Register a device token * * @param string $deviceId device token * @param string $platform platform on which to register * @return string the endpoint ARN for this device * @throws PlatformNotConfiguredException */ public function registerDevice($deviceId, $platform) { if (!isset($this->arns[$platform])) { throw new PlatformNotConfiguredException("There is no configured ARN for {$platform}"); } try { $res = $this->sns->createPlatformEndpoint(['PlatformApplicationArn' => $this->arns[$platform], 'Token' => $deviceId, 'Attributes' => ['Enabled' => 'true']]); } catch (InvalidParameterException $e) { preg_match('/Endpoint (.+?) already/', $e->getMessage(), $matches); if (isset($matches[1])) { $this->sns->setEndpointAttributes(['EndpointArn' => $matches[1], 'Attributes' => ['Enabled' => 'true']]); return $matches[1]; } else { throw $e; } } return $res['EndpointArn']; }