【发布时间】:2015-03-17 07:20:44
【问题描述】:
我正在向 iPhone 设备发送推送通知。 我的数据库表中存储了近 2500 台设备。但是只有 270 台设备收到通知,并且某些设备可能会被我的应用程序卸载。一些工作设备没有收到通知。主要是最新的设备没有得到。 我也没有收到关于从我的数据库中删除未使用我的应用程序的设备的反馈。
这是我正在使用的代码
function send_iphone_notification($message)
{
$sent=0;
$res=mysql_query("SELECT * FROM App where Device_type='iOS' ") or die(mysql_error());
$num_rows=mysql_num_rows($res);
if($num_rows)
{
$passphrase = 'passphrase';
$badge = 1;
$ck=realpath("ck.pem");
////////////////////////////////////////////////////////////////////////////////
///integrating the ck.pem file to APN's
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert',$ck);
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)
{
return "Failed to iOS connect: $err $errstr";
//exit("Failed to connect: $err $errstr" . PHP_EOL);
}
$date=date("Y-m-d");
$sno=0;
while($row=mysql_fetch_assoc($res))
{
$sno++;
$deviceToken = $row['Device_token'];
$deviceId=$row['Device_Id'];
$message=$message;
if($message)
{
// Create the payload body
$body['aps'] = array('alert' => $message,'sound' => 'default');
// Encode the payload as JSON
$payload = json_encode($body);
//$deviceToken = $row['Device_token']; ////iPod Device Token
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
}
if(!$result) {
$f+=1;
}
else
{
mysql_query("update App set sent_date=NOW() where Device_token='$deviceToken' and Device_Id='$deviceId' and Device_type='iOS' ") or die(mysql_error());
$s+=1;
}
// Close the connection to the server
}
//running feedback service
stream_context_set_option($ctx, 'ssl', 'local_cert', $ck);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$apns = stream_socket_client('ssl://feedback.push.apple.com:2196', $error, $errorString, 2, STREAM_CLIENT_CONNECT, $ctx);
if(!$apns)
{
//echo "ERROR $errcode: $errstr\n";
return;
}
//else echo 'APNS FEEDBACK CONNECTION ESTABLISHED...<br/>';
$feedback_tokens = array();
$count = 0;
$feedback_tokens = array();
//and read the data on the connection:
while(!feof($apns)) {
$data = fread($apns, 38);
if(strlen($data)) {
$feedback_tokens[] = unpack("N1timestamp/n1length/H*devtoken", $data);
}
}
fclose($apns);
foreach($feedback_tokens as $ft)
{
mysql_query("DELETE FROM App WHERE Device_token LIKE '".$ft['devtoken']."'");
}
//feedback service close
if($s>=1)
return 'Notification sent successfully to '.$s.' iPhone Devices out of '.$num_rows;
else
return 'Notification not sent to '.$f.' iPhone Devices out of '.$num_rows;
}
else
return 'No Iphone Devices found';
fclose($fp);
}
$message='200 jobs have been updated';
$send=send_iphone_notification($message);
这是我的代码。我认为没有错误。但为什么通知不会发送到所有 iPhone 设备。请帮我。如果设备不存在,反馈也不会出现。
【问题讨论】:
-
代码看起来不错,用静态设备令牌检查..
-
通知将发送到某些设备
标签: php ios iphone push-notification