我有一个PHP脚本,可以将推送通知发送到Apple APNS服务器。
它应该回显响应代码$payload = array(
'aps' => array(
'alert' => $message,
'T_ID' => $data["trip_id"],
'S' => $state,
'Api' => 2,
'sound' => 'default'
));
$body = array();
$body['aps'] = array('alert' => "request trip");
$body['aps']['notifurl'] = $payload;
$body['aps']['badge'] = 2;
$payload = json_encode($body);
$ret = array(
"error" => 0
);
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', dirname(BASEPATH) . "/uploads/" . $this->_apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $this->_passphrase);
@$apns = stream_socket_client('tls://' . $this->_apnsHost . ':' . $this->_apnsPort, $error, $errorString, 200, STREAM_CLIENT_CONNECT, $streamContext);
if (!$apns) {
//die('Error creating ssl socket ' . $error . ' ' . $errorString);
$ret["error"] = 1;
$ret["details"] = 'Error creating ssl socket ' . $error . ' ' . $errorString;
}
$apnsMessage = // Command "1"
chr(1)
// Identifier "88"
. pack('N', 88)
// Expiry "tomorrow"
. pack('N', time() + 86400)
// Token length
. chr(0) . chr(32)
// Device token
. pack('H*', str_replace(' ', '', $deviceToken))
// Payload length
. chr(0) . chr(strlen($payload))
// Actual payload
. $payload . $payload;
echo fwrite($apns, $apnsMessage);
fclose($apns);
,它表明消息已成功发送。但是,当我运行代码时,它每次都会显示不同的数字代码,并且不会发送消息。
以下是代码:
A = [7, 15, 21]
B = [b for i in A for b in (i, i + 1, i + 2)]
# [7, 8, 9, 15, 16, 17, 21, 22, 23]
为什么这不按预期工作?
答案 0 :(得分:4)
我在代码中看到至少2个错误:
1)使用$payload
对邮件正文进行编码时,json_encode($body)
中的原始值会被覆盖,尽管这可能不是最终的罪魁祸首
2)当你应该从流中读取响应并echo
时,你从fwrite中echo
得到// FUNCTION to check if there is an error response from Apple
// Returns TRUE if there was and FALSE if there was not
function checkAppleErrorResponse($fp) {
//byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID).
// Should return nothing if OK.
//NOTE: Make sure you set stream_set_blocking($fp, 0) or else fread will pause your script and wait
// forever when there is no response to be sent.
$apple_error_response = fread($fp, 6);
if ($apple_error_response) {
// unpack the error response (first byte 'command" should always be 8)
$error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response);
if ($error_response['status_code'] == '0') {
$error_response['status_code'] = '0-No errors encountered';
} else if ($error_response['status_code'] == '1') {
$error_response['status_code'] = '1-Processing error';
} else if ($error_response['status_code'] == '2') {
$error_response['status_code'] = '2-Missing device token';
} else if ($error_response['status_code'] == '3') {
$error_response['status_code'] = '3-Missing topic';
} else if ($error_response['status_code'] == '4') {
$error_response['status_code'] = '4-Missing payload';
} else if ($error_response['status_code'] == '5') {
$error_response['status_code'] = '5-Invalid token size';
} else if ($error_response['status_code'] == '6') {
$error_response['status_code'] = '6-Invalid topic size';
} else if ($error_response['status_code'] == '7') {
$error_response['status_code'] = '7-Invalid payload size';
} else if ($error_response['status_code'] == '8') {
$error_response['status_code'] = '8-Invalid token';
} else if ($error_response['status_code'] == '255') {
$error_response['status_code'] = '255-None (unknown)';
} else {
$error_response['status_code'] = $error_response['status_code'].'-Not listed';
}
echo '<br><b>+ + + + + + ERROR</b> Response Command:<b>' . $error_response['command'] . '</b> Identifier:<b>' . $error_response['identifier'] . '</b> Status:<b>' . $error_response['status_code'] . '</b><br>';
echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.<br>';
return true;
}
return false;
}
。
请参阅Working with Apple Push Notification处的示例,了解如何处理返回值。以下代码来自那里:
stream_set_blocking($fp, 0)
请注意,该示例建议您在读取之前确保使用UIAnimation
将流设置为非阻塞模式,但只有在您想要进行任何临时错误检查时才需要这样做;你的代码示例不会这样做,因此对你来说可能不是问题。