我正在使用PHP将推送通知发送给iOS设备。目前,我的代码已经运行良好,以下是我所使用的代码:
$passphrase = '';
$badge = 1;
$path = base_path('path/to/certificate.pem');
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $path);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx
);
if (!$fp) {
self::SavePush($device_token, $message, $device_id, $device_type, $user_id, "pending", "normal", null);
}
//echo 'Connected to APNS' . PHP_EOL;
$body['aps'] = array(
'alert' => $message,
'badge' => $badge,
'sound' => 'default'
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $device_token) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
//print_r($result);exit;
if (!$result) {
self::SavePush($device_token, $message, $device_id, $device_type, $user_id, "pending", "normal", null, $result);
} else {
self::SavePush($device_token, $message, $device_id, $device_type, $user_id, "sent", "normal", null, $result);
}
fclose($fp);
现在我面临的问题是,我无法确定通知是否失败,因为$result
在每种情况下都包含一个整数,无论成功还是失败。我传递了一个随机数字作为令牌,它返回像115或65这样的整数,并且每次都会更改。所以!$result
不能工作。我如何知道通知是否失败?