Tôi gửi thông báo đẩy từ php tới ios. Nó được làm việc tốt và đây là mã của tôi:PHP: Gửi thông báo đẩy tới 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);
Bây giờ vấn đề mà tôi đang phải đối mặt là, tôi không thể xác định xem một thông báo lỗi như $result
chứa một số nguyên trong mọi trường hợp, một trong hai, thành công hay thất bại. Tôi đã chuyển một chữ số ngẫu nhiên làm mã thông báo và nó trả về số nguyên như 115 hoặc 65 và nó thay đổi, mỗi lần. Vì vậy, !$result
sẽ không hoạt động. Làm cách nào để biết liệu thông báo có bị lỗi không?
có thể trùng lặp với https://stackoverflow.com/questions/25830597/how-to-know-push-notification-delivery-status – arunjos007