【发布时间】:2016-08-16 13:43:39
【问题描述】:
我们有一个内置 PHP 的应用程序,可以向 Android 和 iOS 发送推送通知。我们的问题是,iOS 的一些设备 ID 似乎完全停止了我们脚本中所有其他 iOS 推送通知的发送,他们甚至说它们已被正确发送,但之后它会停止循环中的所有通知。
如果我们随后从数据库中删除违规设备 ID,则发送脚本适用于违规设备之后的所有设备。这很奇怪,似乎无法弄清楚为什么。
有人有这方面的经验吗?是否与发送到不再存在的设备 ID 会阻止苹果在该特定连接上完成我们的脚本有关?
这是我们用 PHP 编写的发送脚本(这适用于除了奇怪的恶意设备 ID 之外的所有设备):
$tHost = 'gateway.push.apple.com';
$tPort = 2195;
$tCert = "path_to_our_cert.pem";
$tPassphrase = 'passphrase';
$tAlert = $this->title; //assign a title
$tBadge = 8;
$tSound = 'default';
$tPayload = $this->body_plain; //assign a body
// Create the message content that is to be sent to the device.
$tBody['aps'] = array ('alert' => $tAlert,'badge' => $tBadge,'sound' => $tSound,);
$tBody ['payload'] = $tPayload;
$tBody = json_encode ($tBody);
$tContext = stream_context_create ();
stream_context_set_option ($tContext, 'ssl', 'local_cert', $tCert);
stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase);
$tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);
if (!$tSocket) exit ("APNS Connection Failed: $error $errstr" . PHP_EOL);
//Loop through all devices to send
foreach($this->device->devices as $item){
if($item->os != "iOS") continue;
if(session::getAdministratorStaffSchoolID() != $item->school_id) continue;
$tToken = $item->device_id;//assign the device id
$tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $tToken) . pack ('n', strlen ($tBody)) . $tBody;
$tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));
}
fclose ($tSocket);
有人对此有任何想法吗?
非常感谢
【问题讨论】:
标签: php ios iphone push-notification apple-push-notifications