/** * Send all notification messages starting from the given ID * * @param int $firstMessageId * @param string $apnURL * @throws \RuntimeException * @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException * @return int */ protected function sendMessages($firstMessageId, $apnURL) { $errors = array(); // Loop through all messages starting from the given ID for ($currentMessageId = $firstMessageId; $currentMessageId < count($this->messages); $currentMessageId++) { // Send the message $result = $this->writeApnStream($apnURL, $this->messages[$currentMessageId]); $errors = array(); // Check if there is an error result if (is_array($result)) { // Close the apn stream in case of Shutdown status code. if ($result['status'] === self::APNS_SHUTDOWN_CODE) { $this->closeApnStream($apnURL); } $this->responses[] = $result; // Resend all messages that were sent after the failed message $this->sendMessages($result['identifier'] + 1, $apnURL); $errors[] = $result; if ($this->logger) { $this->logger->err(json_encode($result)); } } else { $this->responses[] = true; } } return $errors; }
public function send(MessageInterface $message) { if (!$message instanceof WindowsphoneMessage) { throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by MPNS", get_class($message))); } $headers = array('Content-Type: text/xml', 'X-WindowsPhone-Target: ' . $message->getTarget(), 'X-NotificationClass: ' . $message->getNotificationClass()); $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><wp:Notification xmlns:wp="WPNotification" />'); $msgBody = $message->getMessageBody(); if ($message->getTarget() == WindowsphoneMessage::TYPE_TOAST) { $toast = $xml->addChild('wp:Toast'); $toast->addChild('wp:Text1', htmlspecialchars($msgBody['text1'], ENT_XML1 | ENT_QUOTES)); $toast->addChild('wp:Text2', htmlspecialchars($msgBody['text2'], ENT_XML1 | ENT_QUOTES)); } $response = $this->browser->post($message->getDeviceIdentifier(), $headers, $xml->asXML()); if (!$response->isSuccessful()) { $this->logger->err($response->getStatusCode() . ' : ' . $response->getReasonPhrase()); } return $response->isSuccessful(); }
/** * Sends the data to the given registration IDs via the GCM server * * @param \RMS\PushNotificationsBundle\Message\MessageInterface $message * @throws \RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException * @return bool */ public function send(MessageInterface $message) { if (!$message instanceof AndroidMessage) { throw new InvalidMessageTypeException(sprintf("Message type '%s' not supported by GCM", get_class($message))); } if (!$message->isGCM()) { throw new InvalidMessageTypeException("Non-GCM messages not supported by the Android GCM sender"); } $headers = array("Authorization: key=" . $this->apiKey, "Content-Type: application/json"); $data = array_merge($message->getGCMOptions(), array("data" => $message->getData())); // Chunk number of registration IDs according to the maximum allowed by GCM $chunks = array_chunk($message->getGCMIdentifiers(), $this->registrationIdMaxCount); // Perform the calls (in parallel) $this->responses = array(); foreach ($chunks as $registrationIDs) { $data["registration_ids"] = $registrationIDs; $this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data)); } // If we're using multiple concurrent connections via MultiCurl // then we should flush all requests if ($this->browser->getClient() instanceof MultiCurl) { $this->browser->getClient()->flush(); } // Determine success foreach ($this->responses as $response) { $message = json_decode($response->getContent()); if ($message === null || $message->success == 0 || $message->failure > 0) { if ($message == null) { $this->logger->err($response->getContent()); } else { $this->logger->err($message->failure); } return false; } } return true; }