【问题标题】:iphone push notifications not going to all mobiles and feedback also not coming back in phpiphone推送通知不会发送到所有手机,反馈也不会在php中返回
【发布时间】: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


【解决方案1】:

考虑official documentation的这部分也是有效的:

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

如果 APNs 尝试发送通知但设备处于离线状态,则通知会存储一段有限的时间,并在可用时发送到设备。

仅存储特定应用的一个最近通知。如果在设备离线时发送了多个通知,则每个新通知都会导致之前的通知被丢弃。这种只保留最新通知的行为称为合并通知。

如果设备长时间处于离线状态,则为它存储的所有通知都会被丢弃

【讨论】:

  • 给我关于代码更改的建议并获得反馈
猜你喜欢
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多