2016-03-11 3 views

ответ

0
# build uri 
    $uri = $this->endpoint . $this->hubPath . "/registrations" . NotificationHub::API_NEW_VERSION; 
    $ch = curl_init(); 

    $token = $this->generateSasToken($uri); 

    $headers = [ 
     'Authorization: '. $token, 
     'Content-Type: application/xml', 
     'x-ms-version: 2015-01' 
    ]; 

    $request_body = self::requestBodyRegistration($device_type, $tagsOrTagExpression, $device_code); 

    if(is_null($request_body)) 
    { 
     return null; 
    } 

    curl_setopt_array($ch, array(
     CURLOPT_URL => $uri, 
     CURLOPT_POST => TRUE, 
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_SSL_VERIFYPEER => FALSE, 
     CURLOPT_HTTPHEADER => $headers, 
     CURLOPT_POSTFIELDS => $request_body 
    )); 

    // Send the request 
    $response = curl_exec($ch); 

    // Check for errors 
    if($response === FALSE){ 
     throw new Exception(curl_error($ch)); 
    } 

    $info = curl_getinfo($ch); 
    curl_close($ch); 

private function requestBodyRegistration($device_type, $tagsOrTagExpression, $device_code) 
{ 
    switch ($device_type) { 
     case 'apple': 
      return '<?xml version="1.0" encoding="utf-8"?> 
         <entry xmlns="http://www.w3.org/2005/Atom"> 
          <content type="application/xml"> 
           <AppleRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"> 
            <Tags>'. $tagsOrTagExpression .'</Tags> 
            <DeviceToken>'. $device_code .'</DeviceToken> 
           </AppleRegistrationDescription> 
          </content> 
         </entry>'; 
     case 'gcm': 
      return '<?xml version="1.0" encoding="utf-8"?> 
           <entry xmlns="http://www.w3.org/2005/Atom"> 
            <content type="application/xml"> 
             <GcmRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"> 
               <Tags>'. $tagsOrTagExpression .'</Tags> 
               <GcmRegistrationId>'. $device_code .'</GcmRegistrationId> 
             </GcmRegistrationDescription> 
            </content> 
           </entry>'; 
     default: 
      return null; 
    } 
} 
0

Вообще говоря, есть 3 основные шаги, необходимые для доступа к Notification концентраторы REST конечными точками:

  1. Обработать строку соединения
  2. Сформировать разрешение маркера
  3. Perform HTTP-вызов,

Вы можете обратиться к https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-php-backend-how-to/ для более подробной информации.

Между тем, вы можете напрямую использовать these PHP sample, предоставленный Azure Team, в своем приложении, которое может легко реализовать ваши требования.

+0

Thank you @Gary Liu !. – jrodriguez

Смежные вопросы