2015-10-16 78 views
0

作爲參數傳遞的消息是一個數組。但推送沒有得到發送,而如果我發送一個字符串推送成功發送到IOS。對於IOS推送如何發送一個數組消息

public function sendIosPush($registatoin_ids, $message) { 

    set_time_limit(0); 

    $this->logger->write("INFO :","message for IOS PUSH".$message); 

    header('content-type: text/html; charset: utf-8'); 
    $passphrase = '******'; 
    $deviceIds=$registatoin_ids; 
    $body['aps'] = array('alert' => $message); 
    //$payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}'; 
    //$result = 'Start' . '<br />'; 

    $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

    foreach ($deviceIds as $item) { 
     //sleep(1); 
     $fp = stream_socket_client($this->iosServer, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); 

     // Build the binary notification 
     $payload = json_encode($body); 

     $this->logger->write("INFO :","message for IOS PUSH".$payload); 

     $msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload; 

     // Send it to the server 
     $result = fwrite($fp, $msg, strlen($msg)); 
     if ($fp) { 
      fclose($fp); 
     } 
    } 
    //set_time_limit(30); 

} 

回答

0

你可以使用JSON用於傳輸消息....

獲取消息後,可以解碼JSON,並將其解析到數組或任何你想要的東西。

如果你不想JSON,你也可以使用XML ...

的轉換在PHP中JSON可以使用json_encdoe功能。

更新: 這應該是你想要什麼:

public function sendIosPush($registatoin_ids, $message) { 
    if(!is_array($message)) 
     $message=array($message); 
    set_time_limit(0); 

    $this->logger->write("INFO :","message for IOS PUSH".$message); 

    header('content-type: text/html; charset: utf-8'); 
    $passphrase = '******'; 
    $deviceIds=$registatoin_ids; 
    $body['aps'] = array(
    'alert' => json_encode($message), 
    'sound'=>"default"); 

    $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

    foreach ($deviceIds as $item) { 

     $fp = stream_socket_client($this->iosServer, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); 

     // Build the binary notification 
     $payload = json_encode($body); 

     $this->logger->write("INFO :","message for IOS PUSH".$payload); 

     $msg = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload; 

     // Send it to the server 
     $result = fwrite($fp, $msg, strlen($msg)); 
     if ($fp) { 
      fclose($fp); 
     } 
    } 
    //set_time_limit(30); 

} 
+0

我已經做了類似於你所說的,但它不工作。你能幫助我解決任何工作示例嗎? – Rajeshwar

2
function sendPushiOS($deviceToken, $pushMessage, $urlLink, $alertType) 
{ 
    $iosServer='ssl://gateway.sandbox.push.apple.com:2195'; 
    $deviceTokens = $deviceToken; 
    $passphrase = '****'; 
    $urlis = $urlLink; 
    $pushId = uniqid(); 

     $result = htmlspecialchars_decode($pushMessage) . "\r\n" . htmlspecialchars_decode($urlis); 
    $message = $result; 
    $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

    $body['aps'] = array(
     'alert' => $message, 
     'sound' => 'busy.wav', 
     'URL' => $urlis, 
     'pushMsgId' => $pushId 
    ); 

    foreach ($deviceTokens as $udid) 
    { 

     $fp = stream_socket_client($iosServer, $err,$errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); 
     $payload = json_encode($body); 
      if (!$fp) 
      { 
      exit("Failed to connect: $err $errstr" . PHP_EOL); 
      } 
      $msg = chr(0) . pack('n', 32) . pack('H*', $udid) . pack('n', strlen($payload)) . $payload; 
      $result = fwrite($fp, $msg, strlen($msg)); 
      if ($fp) 
      { 
       fclose($fp); 
      } 
      if (!$result) 
      { 
       echo "<br/>" . 'Message not delivered' . PHP_EOL; 
      } 
    } 
} 

這裏$ deviceTokens是設備令牌的陣列。