苹果推送消息 [英] Apple Push Message

查看:23
本文介绍了苹果推送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将推送提供程序服务器与来自谷歌代码的代码 (php-apns) 集成在一起.除了每条消息的字节数外,一切似乎都很好.

I am integrating a Push Provider Server with the codes (php-apns) from google codes. Everything seems to be fine except the number of bytes per message.

每个有效负载的字节数最多应为 256 个字符.

The number of bytes per payload should be maximum 256 characters.

如果发送了一些中文字符或 UTF8 字符.在 JSON_enode 之后,每个字符将占用 6 个字节.我说得对吗?

If some Chinse characters or UTF8 characters are sent. After JSON_enode, each character would occupy 6 bytes. Am I right ?

所以每条推送消息的最大UTF8字符数在38左右.

So the maximum number of UTF8 characters in each push message is around 38.

但是……Whatsapp(iPhone 应用程序)也使用 PUSH,但它可以推送更多汉字……在一个推送消息中?

But ... Whatsapp (iPhone application) uses PUSH too, but it can push more Chinese characters ... in one push message ?

有什么提示吗?

推荐答案

这里是您问题的解决方案:

Here is the solution to your problem:

去~/APNS/Message.php

go to ~/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 消息.

Tada! now you will be able to receive long utf-8 message without problems.

来源

这篇关于苹果推送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆