Tuesday, 15 April 2014

Push Notification (iOS) not recived/sent with PHP code -



Push Notification (iOS) not recived/sent with PHP code -

i'm trying implement apns (push notification). i've created certificates, etc.. created iv'e done many many times. cant error apple apns server , seems ok still not getting force in devices. php code i'm using create magic :) ensure ports open in hosting.

basically i'm getting user token db , sending force text default sound.

can please , tell me if can see problem in code or allow me know how can real result of process / response apns server?

<?php error_reporting(0); header("content-type: application/json; charset=utf-8"); include "dbconn.php"; $data = array(); $json= array(); $users = array(); $opponent_id = $_request['opponent_id']; $sender_id = $_request['sender_id']; $message = $_request['message']; $sql = "select * `registration` `chat_id` ='$opponent_id'"; $result = mysql_query($sql); if (!$result) { echo "could not run query ($sql) db: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "no rows found, nil print exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { $token = $row['token']; } echo "\n$token"; $passphrase = 'certificate_password'; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); //stream_context_set_option($ctx, 'ssl', 'local_cert','push/some_prod_certificate.pem'); stream_context_set_option($ctx, 'ssl', 'local_cert','push/some_dev_certificate.pem'); stream_context_set_option($ctx, 'ssl', 'cafile', 'push/entrust_root_certification_authority.pem'); # open connection apns server $fp = stream_socket_client(//'ssl://gateway.push.apple.com:2195', $err, 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, stream_client_connect|stream_client_persistent, $ctx); if (!$fp){ echo "error: ".$err; exit; } echo "\n".'connected apns force notification' . php_eol; $body['aps'] = array( 'alert' => $message, 'sound' => 'default' ); $payload = json_encode($body); echo "\n$payload"; // build binary notification $msg = chr(0) . pack('n', 32) . pack('h*', $token) . pack('n', strlen($payload)) . $payload; // send server $result = fwrite($fp, $msg, strlen($msg)); //set blocking stream_set_blocking($fp,0); //check response checkappleerrorresponse($fp); // close connection server fclose($fp); function checkappleerrorresponse($fp) { $apple_error_response = fread($fp, 6); //byte1=always 8, byte2=statuscode, bytes3,4,5,6=identifier(rowid). should homecoming nil if ok. //note: create sure set stream_set_blocking($fp, 0) or else fread pause script , wait forever when there no response sent. if ($apple_error_response) { $error_response = unpack('ccommand/cstatus_code/nidentifier', $apple_error_response); //unpack error response (first byte 'command" should 8) 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>&nbsp;&nbsp;&nbsp;identifier:<b>' . $error_response['identifier'] . '</b>&nbsp;&nbsp;&nbsp;status:<b>' . $error_response['status_code'] . '</b><br>'; echo 'identifier rowid (index) in database caused problem, , apple disconnect server. go on sending force notifications, start @ next rowid after identifier.<br>'; homecoming true; } echo "\npush respnse ok"; homecoming false; } ?>

you can't error responses apple because using simple binary format doesn't homecoming error responses :

$msg = chr(0) . pack('n', 32) . pack('h*', $token) . pack('n', strlen($payload)) . $payload;

if want format can returns error response, can utilize 1 of enhanced formats.

for example, format starts 1 :

the msg start chr(1), followed 4 bytes of message id, 4 bytes of expiration time , rest of message same have pack('n', 32) . pack('h*', $token) . pack('n', strlen($payload)) . $payload

it should :

$msg = chr(1) . pack("n", $msg_id) . pack("n", $expiry) . pack('n', 32) . pack('h*', $token) . pack('n', strlen($payload)) . $payload;

php ios push-notification apple-push-notifications apns-php

No comments:

Post a Comment