2011-04-25 105 views
3

我正在整合推送提供商服務器與代碼(php-apns)從谷歌代碼。除了每條消息的字節數外,一切看起來都很好。蘋果推送消息

每個有效負載的字節數應該最多爲256個字符。

如果發送了一些Chinse字符或UTF8字符。在JSON_enode之後,每個字符將佔用6個字節。我對嗎 ?

所以在每個推送消息UTF8字符的最大數目大約是38

但是...... WhatsApp的(iPhone應用程序)使用PUSH過,但它可以推動更多的中國字......在一個推送消息?

任何提示?

回答

5

這裏是解決問題的方法:

到〜/ APNS/Message.php

和替換該功能:

public function getPayload() {...} 

與此:

/** 
* Convert the message in a JSON-encoded payload. 
* 
* @throws ApnsPHP_Message_Exception if payload is longer than maximum allowed 
*   size and AutoAdjustLongPayload is disabled. 
* @return @type string JSON-encoded payload. 
*/ 
public function getPayload() 
{ 
    $sJSONString = preg_replace_callback('/\\\u([0-9a-f]{4})/i', 
         function($matches) { 
          if (function_exists('mb_convert_encoding')) { 
           return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16'); 
          } else { 
           //Slower conversion from UTF-16 to UTF-8 (BMP Only) 
           //See: http://www.cl.cam.ac.uk/~mgk25/unicode.html 
           $decimal_code = hexdec($matches[1]); 
           $character = ""; 
           if ((0x7F & $decimal_code) == $decimal_code) { 
            //UTF-8 1-byte aka ASCII 
            $first_byte  = 0x7F & $decimal_code; 
            $character  = chr($first_byte); 
           } elseif ((0x7FF & $decimal_code) == $decimal_code) { 
            //UTF-8 2-bytes 
            $first_byte  = 0xC0 | (($decimal_code >> 6) & 0x1F); 
            $second_byte = 0x80 | ($decimal_code & 0x3F); 
            $character  = chr($first_byte) . chr($second_byte); 
           } elseif ((0xFFFF & $decimal_code) == $decimal_code) { 
            //UTF-8 3-bytes 
            $first_byte  = 0xE0 | (($decimal_code >> 12) & 0x0F); 
            $second_byte = 0x80 | (($decimal_code >> 6) & 0x3F); 
            $third_byte  = 0x80 | ($decimal_code & 0x3F); 
            $character  = chr($first_byte) . chr($second_byte) . chr($third_byte); 
           } 
           return $character; 
          } 
         }, 
         json_encode($this->_getPayload())); 
    $sJSONPayload = str_replace(
     '"' . self::APPLE_RESERVED_NAMESPACE . '":[]', 
     '"' . self::APPLE_RESERVED_NAMESPACE . '":{}', 
     $sJSONString 
    ); 
    $nJSONPayloadLen = strlen($sJSONPayload); 

    if ($nJSONPayloadLen > self::PAYLOAD_MAXIMUM_SIZE) { 
     if ($this->_bAutoAdjustLongPayload) { 
      $nMaxTextLen = $nTextLen = strlen($this->_sText) - ($nJSONPayloadLen - self::PAYLOAD_MAXIMUM_SIZE); 
      if ($nMaxTextLen > 0) { 
       while (strlen($this->_sText = mb_substr($this->_sText, 0, --$nTextLen, 'UTF-8')) > $nMaxTextLen); 
       return $this->getPayload(); 
      } else { 
       throw new ApnsPHP_Message_Exception(
        "JSON Payload is too long: {$nJSONPayloadLen} bytes. Maximum size is " . 
        self::PAYLOAD_MAXIMUM_SIZE . " bytes. The message text can not be auto-adjusted." 
       ); 
      } 
     } else { 
      throw new ApnsPHP_Message_Exception(
       "JSON Payload is too long: {$nJSONPayloadLen} bytes. Maximum size is " . 
       self::PAYLOAD_MAXIMUM_SIZE . " bytes" 
      ); 
     } 
    } 

    return $sJSONPayload; 
} 

田田! 現在您將能夠毫無問題地收到長久的utf-8消息。

source

1

我發現了。

如果這些UTF8中文字符是JSON_encoded,那麼它將被轉換爲6個字符。

因此,我需要修改PHP-APNS,以確保這些UTF8字符將被放入JSON_encoded串,以節省空間