0

I tried to send Push notification for iPhone. I didn't put password because certificate is without it. I convert device token njInRSuzfRLxdCiv8dG9JqRZLvxxTK95HRVYCvPGAUw= from Base64 to HEX But notification didn't come. What can be the reason? I also receive message that i connected to APNS and success. as told my iPhone developer the certificates is correct because previously they was used on PARSE service Here is my code

$deviceToken = '9E3227452BB37D12F17428AFF1D1BD26A4592EFC714CAF791D15580AF3C6014C';
$message = 'TEST!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,  
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 
if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );
$payload = json_encode($body);
$msg =chr(0).pack('n',32).pack('H*', $deviceToken) . pack('n',strlen($payload)).$payload;
$result = fwrite($fp, $msg);
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

socket_close($fp);
fclose($fp);
  • Missing a lot of information. Can you connect to the APNS? Did you get success message? Might have been answered here: http://stackoverflow.com/questions/16140055/not-receiving-any-push-notification-in-iphone – realtimez Oct 16 '13 at 10:43
  • Yes sure, i receive connection and success. – Vladimir Shabuniayeu Oct 16 '13 at 10:48

1 Answers1

0

This is my code and it work well, maybe try to pass your passphrase blank instead ignore it. Other solution can be to put a passphrase to your cert, i've never tried without it, it can be a security issue.

    <?php

// Put your device token here (without spaces):
$deviceToken = '';

// Put your private key's passphrase here:
$passphrase = '';

// Put your alert message here:
$message = 'TEST!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default',
    'badge' => '1'
    );


// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Messaggio consegnato!' . PHP_EOL;

// Close the connection to the server
fclose($fp);

?>
Firegab
  • 313
  • 1
  • 9