我正在尝试构建支持本地化的iOS推送通知服务器。
我将ios项目的消息字符串添加到我的本地化,并使用NSLocalizedString
进行检查。
我使用此代码:
<?PHP
if(isset($_GET['message']) && isset($_GET['sender']) ){
$deviceToken="5cca98f1 4e20c5e4 5267d4da 23437439 81959bd0 b75b7573 42a2f9d7 ceed8f0f";
$message = stripslashes($_GET['message']);
$sender = stripslashes($_GET['sender']);
$payload = '{
"aps":{
"alert":{
"loc-key":"'.$message.'",
"loc-args":["'.$sender.'"]
},
"badge":1,
"sound":"bingbong.aiff"
}
}';
print $payload;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'password');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if(!$fp){
print "Failed to connect $err $errstrn";
return;
} else {
print "Notifications sent!";
}
$devArray = array();
array_push($devArray,$deviceToken);
foreach($devArray as $deviceToken){
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack ("n",strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
}
fclose($fp);
print "This is the Device Token Sent Each Time".strlen($msg)."\n";
}
但未向设备发送通知。 当我尝试没有本地化并发送带有此负载的消息时:
$payload = '{
"aps":{
"alert":"'.$message.'",
"badge":1,
"sound":"bingbong.aiff"
}
}';
设备收到通知。
我使用了苹果的this link。
我做错了什么?
答案 0 :(得分:1)
好像你错过了, 固定代码:
$payload = '{
"aps" :
{
"alert" :{
"loc-key":"'.$message.'",
"loc-args":["'.$sender.'"]
},//<=================
"badge" : 1,
"sound" : "bingbong.aiff"
}
}';