我在php中有一个类,它通过web向应用程序的android / IOS用户发送推送通知。现在我需要在推送通知有效负载的标题/正文中添加表情符号,但没有运气这样做。
我已经尝试将编码转换为UTF-8,我尝试使用this库,我尝试将\\替换为\(json_encode函数添加\\而不是\例如\ xC2 \ xA9变为\\ xC2 \\ xA9)。
这是我的代码:
public function sendPush($payload)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->base_url . "/push");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-AUTH-TOKEN: 94h4f5RCDSEYKYqXDHTk';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
该推送功能,以及我从网站获取数据的.tpl。
<?php
include(SITE_ROOT . '/core/admin/logo.class.php');
if (isset($_POST['push-button'])) {
if (
isset($_POST['push-title'])
&& isset($_POST['push-body'])
&& isset($_POST['push-city'])
&& isset($_POST['push-url'])
&& isset($_POST['push-platform'])
) {
$push = new Push();
$title = $_POST['push-title'];
$body = $_POST['push-body'];
$url = $_POST['push-url'];
$platform = $_POST['push-platform'];
$cities = $_POST['push-city'];
$payload = [
'title' => $title,
'body' => $body,
'url' => $url,
'cities' => [
$cities
]
];
if ($platform === 'all') {
$responses_ios = json_decode($push->sendPush($payload), true);
$responses_android = json_decode($push->sendPushAndroid($payload), true);
} elseif ($platform === 'ios') {
$responses_ios = json_decode($push->sendPush($payload), true);
} elseif ($platform === 'android') {
$responses_android = json_decode($push->sendPushAndroid($payload), true);
}
// $devices = '';
// var_dump($responses);
// foreach ($responses_ios as $response) {
// $devices .= $response . "<br>";
// }
$message = '<span class="message"><strong>PUSH-notiser har skickats</strong></span>';
unset($_SESSION['message']);
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>PUSH - Administration</title>
<?php include(SITE_ROOT . '/views/admin/meta.tpl'); ?>
</head>
<body class="page-admin">
<?php include(SITE_ROOT . '/views/admin/header.tpl'); ?>
<?php include(SITE_ROOT . '/views/admin/navigation.tpl'); ?>
<p class="shoutout p-45 text-center">Skicka PUSH-meddelanden till användare i en stad</p>
<?php echo $message; ?>
<section class="wrapper">
<section class="container centered">
<section class="col-3-5">
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<section class="row">
<span class="input-title">Välj stad</span>
<span class="input-field">
<select name="push-city">
<option value="all">Alla</option>
<?php
$cities = $app->get_page_list('city');
foreach ($cities as $city) {
echo '<option value=' . $city['page_name'] . ' />' . $city['page_name'] . '</option>';
}
?>
</select>
</span>
</section>
<section class="row">
<span class="input-title">Titel</span>
<span class="input-field">
<input type="text" name="push-title" class="slug" placeholder="Titel på meddelande"
required/>
</span>
</section>
<section class="row">
<span class="input-title">Meddelande</span>
<span class="input-field">
<textarea name="push-body" class="slug" placeholder="Meddelande" required></textarea>
</span>
</section>
<section class="row">
<span class="input-title">URL</span>
<span class="input-field">
<input type="text" name="push-url" class="slug" placeholder="URL att skicka med" required/>
</span>
</section>
<section class="row">
<span class="input-title">Plattform</span>
<span class="input-field">
<input type="radio" name="push-platform" class="slug"
placeholder="Välj plattform att skicka till"
value="all" checked required/>Alla
<input type="radio" name="push-platform" class="slug"
placeholder="Välj plattform att skicka till"
value="ios" required/>iOS
<input type="radio" name="push-platform" class="slug"
placeholder="Välj plattform att skicka till"
value="android" required/>Android
</span>
</section>
<section class="row" style="margin-top: 2rem;">
<input type="submit" class="button-large-2 button-no-g" name="push-button"
value="Skicka PUSH-notis"/>
</section>
</form>
</section>
</section>
</section>
<?php include(SITE_ROOT . '/views/admin/emoji.tpl'); ?>
<?php include(SITE_ROOT . '/views/admin/footer.tpl'); ?>
</body>
</html>
这是我从title / body
上的echo / var_dump得到的 string(13) "test \xC2\xA9" string(13) "test \xC2\xA9"
那些2 \ xC2 \ xA9应该是版权标志。