3

我正在发送推送通知,当消息包含外来字符(在我的情况下为土耳其语)时,如 İ、ş、ç、ğ... 消息未到达设备。

这是我的代码:

$message = 'THİS is push';
$passphrase = 'mypass';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'MyPemFile.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to Apple service. ' . PHP_EOL;

// Encode the payload as JSON
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );
$payload = json_encode($body);

$result = 'Start'.PHP_EOL;
$tokenArray = array('mytoken');
foreach ($tokenArray as $item)
{
// Build the binary notification
$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 (!$result)
    echo 'Failed message'.PHP_EOL;
else
    echo 'Successful message'.PHP_EOL;
}

// Close the connection to the server
fclose($fp);

我尝试使用 utf8_encode() 对 $message 变量进行编码,但收到的消息为“THÝS is push”。像 iconv() 之类的其他方式对我不起作用,其中一些裁剪了土耳其字符,有些根本没有收到。

我也有

header('content-type: text/html; charset: utf-8');

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

在我的页面中。我认为在设置值时不会出现问题,但可能是使用 pack() 函数。

有什么想法可以解决这个问题而不用英文替换字符?

4

2 回答 2

3

我所要做的就是用以下脚本替换土耳其字符:

function tr_to_utf($text) {   
    $text = trim($text);    
    $search = array('Ü','Ş','Ğ','Ç','İ','Ö','ü','ş','ğ','ç','ı','ö');  
    $replace = array('Ãœ','Å','&#286;','Ç','Ä°','Ö','ü','ÅŸ','ÄŸ','ç','ı','ö');
    $new_text = str_replace($search,$replace,$text);    
    return $new_text;  
}

现在它可以正常工作了。

这就是源头

于 2012-10-03T09:36:29.900 回答
0

“n”参数意味着,您打包为无符号短(始终为 16 位,大端字节序)。我不确定 Apple CPU 硬件如何处理指令以及如何转换它们,但肯定与 PC 不同。尝试切换字节顺序,使用 "v" unsigned short(总是 16 位,小端字节序)。

于 2012-10-03T08:43:08.897 回答