iphone推送通知不会发送到所有手机,反馈也不会返回php

iphone推送通知不会发送到所有手机,反馈也不会返回php,php,ios,iphone,push-notification,Php,Ios,Iphone,Push Notification,我正在向iphone设备发送推送通知。 我的数据库表中存储了近2500台设备。但在这种情况下,只有270个设备收到通知,一些设备可能会被我的应用程序卸载。某些工作设备未收到通知。主要是最新的设备没有得到支持。 我也不会因为从我的数据库中删除没有使用我的应用程序的设备而收到反馈 这是我正在使用的代码 function send_iphone_notification($message) { $sent=0; $res=mysql_query("SELECT * FROM App where D

我正在向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通知($message)
{
$sent=0;
$res=mysql\u query(“从设备所在的应用程序中选择*”)或die(mysql\u error());
$num\u rows=mysql\u num\u rows($res);
如果($num_行)
{ 
$passphrase='passphrase';
$badge=1;
$ck=realpath(“ck.pem”);
////////////////////////////////////////////////////////////////////////////////
///将ck.pem文件集成到APN
$ctx=stream_context_create();
stream_context_set_选项($ctx,'ssl','local_cert',$ck);
stream_context_set_选项($ctx,'ssl','passphrase',$passphrase);
//打开与APNS服务器的连接
$fp=流\u套接字\u客户端(
'ssl://gateway.push.apple.com:2195“,$err,
$errstr,60,流式_客户端_连接|流式_客户端_持久,$ctx);
如果(!$fp)
{
返回“iOS连接失败:$err$errstr”;
//退出(“连接失败:$err$errstr.PHP_EOL”);
}
$date=日期(“Y-m-d”);
$sno=0;
while($row=mysql\u fetch\u assoc($res))
{
$sno++;
$deviceToken=$row['Device_token'];
$deviceId=$row['Device_Id'];
$message=$message;
如果($message)
{ 
//创建有效负载主体
$body['aps']=array('alert'=>$message,'sound'=>'default');
//将有效负载编码为JSON
$payload=json_encode($body);
//$deviceToken=$row['Device_token'];///iPod设备token
//构建二进制通知
$msg=chr(0).pack('n',32).pack('H*',$deviceToken).pack('n',strlen($payload))。$payload;
//将其发送到服务器
$result=fwrite($fp,$msg,strlen($msg));
}
如果(!$result){
$f+=1;
}
其他的
{
mysql_query(“更新应用程序集已发送_date=NOW(),其中设备_令牌='$deviceToken'和设备_Id='$deviceId'和设备_type='iOS'))或die(mysql_error());
$s+=1;
}
//关闭与服务器的连接
}
//运行反馈服务
stream_context_set_选项($ctx,'ssl','local_cert',$ck);
stream_context_set_选项($ctx,'ssl','passphrase',$passphrase);
$apns=流\u套接字\u客户端('ssl://feedback.push.apple.com:2196“,$error,$errorString,2,STREAM\u CLIENT\u CONNECT,$ctx);
如果(!$apns)
{
//echo“ERROR$errcode:$errstr\n”;
返回;
}
//否则echo的反馈连接已建立…
; $feedback_tokens=array(); $count=0; $feedback_tokens=array(); //并读取连接上的数据: 而(!feof($apns)){ $data=fread($apns,38); if(斯特伦(数据)){ $feedback_tokens[]=unpack(“N1timestamp/n1length/H*devtoken”,$data); } } fclose($apns); foreach($ft形式的反馈_代币) { mysql_查询(“从应用程序中删除设备_令牌,如“.$ft['devtoken'.]”); } //反馈服务关闭 如果($s>=1) return“通知已成功发送到“%$s.”iPhone设备中的“%$num_”行; 其他的 返回“$num\u行中的”通知未发送到“$f.”iPhone设备; } 其他的 返回“未找到Iphone设备”; fclose($fp); } $message='200个作业已更新'; $send=send\u iphone\u通知($message);

这是我的密码。我认为没有错。但为什么通知不适用于所有iphone设备呢。请帮帮我。如果没有设备,也不会有反馈。

也可以考虑以下部分:

Apple推送通知服务包括一个默认的服务质量(QoS)组件,该组件执行存储转发功能

如果APNs试图发送通知,但设备处于脱机状态,则通知将存储一段有限的时间,并在可用时发送给设备

仅存储特定应用程序的一个最近通知。如果在设备脱机时发送多个通知,则每个新通知都会导致放弃先前的通知。这种仅保留最新通知的行为称为合并通知


如果设备长时间处于脱机状态,则为其存储的任何通知都将被丢弃

代码似乎没有问题,请使用静态设备令牌进行检查。.通知将发送到某些设备给我有关代码更改和获取反馈的建议