Apple Push Notifications With Foreign Accent Characters Not Receiving

Posted by confeng on Stack Overflow See other posts from Stack Overflow or by confeng
Published on 2012-10-03T08:31:05Z Indexed on 2012/10/03 9:37 UTC
Read the original article Hit count: 352

Filed under:
|
|
|
|

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

Here's my code:

$message = 'THIS 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);

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.

I also have

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

and

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

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about iphone