未收到外国口音字符的 Apple 推送通知 [英] Apple Push Notifications With Foreign Accent Characters Not Receiving

查看:29
本文介绍了未收到外国口音字符的 Apple 推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm sending push notifications and when the message contains foreign characters (Turkish in my case) like İ, ş, ç, ğ... The message does not arrive to devices.

这是我的代码:

$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 是推送".和 iconv() 之类的其他方法对我不起作用,其中一些裁剪了土耳其语字符,一些根本没有收到.

I have tried encoding $message variable with utf8_encode() but the message received as "THÝS is push". And other ways like iconv() didn't work for me, some of them cropped Turkish characters, some didn't receive at all.

我也有

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

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

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

in my page. I don't think the problem appears while I set the value but maybe with pack() function.

有什么想法可以在不用英文替换字符的情况下解决这个问题?

Any ideas to solve this without replacing characters with English?

推荐答案

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

All I had to do was replacing the Turkish characters with following script:

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

现在它可以正常工作了.

Now it is working with no problems.

这是来源.

这篇关于未收到外国口音字符的 Apple 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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