2
votes

I am trying to use push notification using Api and I am not getting any error message neither I am getting any response.

I have checked Apple Push Notification Service with PHP Script

and applied changes in my code accordingly but still not working.

I am not able to get how to get serverId that I have to use in

$device = 'fbb5a9c71066794d57fee33b4005a89f1bb8941a68660fd6e91f466be1299ab6'; // My iphone deviceToken
$payload['aps'] = array(
    'alert' => 'This is the alert text',
    'badge' => 1,
    'sound' => 'default'
);

$payload['server'] = array(
    'serverId' => 1,
    'name' => 'keyss.in'
);

$payload = json_encode($payload);

$apnsCert = 'apple_push_notification_production.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

//socket_close($apns); seems to be wrong here ...
fclose($apns);
    

Getting the errors:

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out)

Warning: fwrite() expects parameter 1 to be resource, boolean given

Warning: fclose() expects parameter 1 to be resource, boolean given

1

1 Answers

6
votes

You are not getting any response because you are using the old binary notification format :

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($payload)) . $payload;

In order to get responses (responses are returned only in case of an error), use the enhanced format :

$apnsMessage = pack("C", 1) . pack("N", $apple_identifier) . pack("N", $apple_expiry) . pack("n", 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload;

You can see sample code here.